summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source/background.ts39
-rw-r--r--source/features/embed-gist-inline.tsx15
-rw-r--r--source/features/view-markdown-source.tsx4
3 files changed, 35 insertions, 23 deletions
diff --git a/source/background.ts b/source/background.ts
index 6cca0d7f..f1fa00ec 100644
--- a/source/background.ts
+++ b/source/background.ts
@@ -3,17 +3,37 @@ import cache from 'webext-storage-cache'; // Also needed to regularly clear the
import addDomainPermissionToggle from 'webext-domain-permission-toggle';
import './options-storage';
-browser.runtime.onMessage.addListener((message, {tab}) => {
- if (Array.isArray(message?.openUrls)) {
- for (const [i, url] of (message.openUrls as string[]).entries()) {
+// GitHub Enterprise support
+addDomainPermissionToggle();
+
+const messageHandlers = {
+ openUrls(urls: string[], {tab}: browser.runtime.MessageSender) {
+ for (const [i, url] of urls.entries()) {
void browser.tabs.create({
url,
index: tab!.index + i + 1,
active: false
});
}
- } else if (message?.closeTab) {
+ },
+ closeTab(_: any, {tab}: browser.runtime.MessageSender) {
void browser.tabs.remove(tab!.id!);
+ },
+ async fetch(url: string) {
+ const response = await fetch(url);
+ return response.text();
+ },
+ async fetchJSON(url: string) {
+ const response = await fetch(url);
+ return response.json();
+ }
+};
+
+browser.runtime.onMessage.addListener((message, sender) => {
+ for (const id of Object.keys(message ?? {}) as Array<keyof typeof messageHandlers>) {
+ if (id in messageHandlers) {
+ return messageHandlers[id](message[id], sender);
+ }
}
});
@@ -41,14 +61,3 @@ browser.runtime.onInstalled.addListener(async ({reason}) => {
// Hope that the feature was fixed in this version
await cache.delete('hotfix');
});
-
-// GitHub Enterprise support
-addDomainPermissionToggle();
-
-// `background` fetch required to avoid avoid CORB introduced in Chrome 73 https://chromestatus.com/feature/5629709824032768
-// Don’t turn this into an `async` function https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage#addListener_syntax
-browser.runtime.onMessage.addListener((message): Promise<string> | void => {
- if (message?.request) {
- return fetch(message.request).then(async response => response.text());
- }
-});
diff --git a/source/features/embed-gist-inline.tsx b/source/features/embed-gist-inline.tsx
index 10b9c203..13fc2022 100644
--- a/source/features/embed-gist-inline.tsx
+++ b/source/features/embed-gist-inline.tsx
@@ -19,16 +19,15 @@ async function embedGist(link: HTMLAnchorElement): Promise<void> {
link.after(info);
try {
- // Get the gist via background.js due to CORB policies introduced in Chrome 73
- const gistData = await browser.runtime.sendMessage({request: `${link.href}.json`});
-
- const files = domify.one(JSON.parse(gistData).div)!;
- const fileCount = files.children.length;
+ // Fetch via background.js due to CORB policies
+ const gistData = await browser.runtime.sendMessage({fetchJSON: `${link.href}.json`});
+ const fileCount: number = gistData.files.length;
if (fileCount > 1) {
info.textContent = ` (${fileCount} files)`;
} else {
- link.parentElement!.attachShadow({mode: 'open'}).append(
+ const container = <div/>;
+ container.attachShadow({mode: 'open'}).append(
<style>{`
.gist .gist-data {
max-height: 16em;
@@ -37,8 +36,10 @@ async function embedGist(link: HTMLAnchorElement): Promise<void> {
`}
</style>,
<link rel="stylesheet" href={gistData.stylesheet}/>,
- files
+ domify.one(gistData.div)!
);
+ link.parentElement!.after(container);
+ info.remove();
}
} catch {
info.remove();
diff --git a/source/features/view-markdown-source.tsx b/source/features/view-markdown-source.tsx
index b1aaeab4..20ac64f4 100644
--- a/source/features/view-markdown-source.tsx
+++ b/source/features/view-markdown-source.tsx
@@ -15,7 +15,9 @@ import {buildRepoURL} from '../github-helpers';
const lineActions = onetime(async () => {
// Avoid having to create the entire 60 lines of JSX. The URL is hardcoded to a file we know the DOM exists.
const randomKnownFile = 'https://github.com/sindresorhus/refined-github/blob/b1229bbaeb8cf071f0711bc2ed1b40dd96cd7a05/.editorconfig';
- const html = await browser.runtime.sendMessage({request: randomKnownFile});
+
+ // Fetch background.js due to CORB policies
+ const html = await browser.runtime.sendMessage({fetch: randomKnownFile});
const blobToolbar = domify(html).querySelector('.BlobToolbar')!;
select('a#js-view-git-blame', blobToolbar)!.href = new GitHubURL(location.href).assign({route: 'blame'}).href;
select('a#js-new-issue', blobToolbar)!.href = buildRepoURL('issues/new');