summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar yakov116 <16872793+yakov116@users.noreply.github.com> 2020-10-20 21:12:46 -0400
committerGravatar GitHub <noreply@github.com> 2020-10-20 20:12:46 -0500
commit42ce2dffb59dff541232ed60fcddb8ece9a1e91c (patch)
tree1278981e4bc54604673d86b21ba4669b848f295d
parent42dfa16cd5366de26b475de94f8225003df07364 (diff)
downloadrefined-github-42ce2dffb59dff541232ed60fcddb8ece9a1e91c.tar.gz
refined-github-42ce2dffb59dff541232ed60fcddb8ece9a1e91c.tar.zst
refined-github-42ce2dffb59dff541232ed60fcddb8ece9a1e91c.zip
Fetch widget for `view-markdown-source` instead of maintaining a copy of it (#3670)
Co-authored-by: Federico <me@fregante.com>
-rw-r--r--source/background.ts6
-rw-r--r--source/features/embed-gist-inline.tsx2
-rw-r--r--source/features/view-markdown-source.tsx79
3 files changed, 19 insertions, 68 deletions
diff --git a/source/background.ts b/source/background.ts
index e189e0ae..1e182660 100644
--- a/source/background.ts
+++ b/source/background.ts
@@ -45,6 +45,8 @@ 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(({request}): Promise<Response> | void => {
- return fetch(request).then(async response => response.json());
+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 0bdb0e63..d6a83b1e 100644
--- a/source/features/embed-gist-inline.tsx
+++ b/source/features/embed-gist-inline.tsx
@@ -22,7 +22,7 @@ async function embedGist(link: HTMLAnchorElement): Promise<void> {
// 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(gistData.div)!;
+ const files = domify.one(JSON.parse(gistData).div)!;
const fileCount = files.children.length;
if (fileCount > 1) {
diff --git a/source/features/view-markdown-source.tsx b/source/features/view-markdown-source.tsx
index 44847a77..21926086 100644
--- a/source/features/view-markdown-source.tsx
+++ b/source/features/view-markdown-source.tsx
@@ -1,5 +1,6 @@
import './view-markdown-source.css';
import React from 'dom-chef';
+import domify from 'doma';
import select from 'select-dom';
import onetime from 'onetime';
import delegate from 'delegate-it';
@@ -7,73 +8,21 @@ import * as pageDetect from 'github-url-detection';
import FileIcon from 'octicon/file.svg';
import CodeIcon from 'octicon/code.svg';
-import KebabHorizontalIcon from 'octicon/kebab-horizontal.svg';
import features from '.';
import fetchDom from '../helpers/fetch-dom';
import GitHubURL from '../github-helpers/github-url';
import {buildRepoURL} from '../github-helpers';
-const lineActions = onetime(() => (
- <details
- className="details-reset details-overlay BlobToolbar position-absolute js-file-line-actions dropdown d-none"
- aria-hidden="true"
- >
- <summary
- className="btn-octicon ml-0 px-2 p-0 bg-white border border-gray-dark rounded-1"
- aria-label="Inline file action toolbar"
- aria-haspopup="menu"
- role="button"
- >
- <KebabHorizontalIcon/>
- </summary>
- <details-menu role="menu">
- <ul
- className="BlobToolbar-dropdown dropdown-menu dropdown-menu-se mt-2"
- style={{width: '185px'}}
- >
- <li>
- <clipboard-copy
- role="menuitem"
- className="dropdown-item zeroclipboard-link"
- id="js-copy-lines"
- >
- Copy line
- </clipboard-copy>
- </li>
- <li>
- <clipboard-copy
- role="menuitem"
- className="dropdown-item zeroclipboard-link"
- id="js-copy-permalink"
- >
- Copy permalink
- </clipboard-copy>
- </li>
- <li>
- <a
- className="dropdown-item js-update-url-with-hash"
- id="js-view-git-blame"
- role="menuitem"
- href={new GitHubURL(location.href).assign({route: 'blame'}).href}
- >
- View git blame
- </a>
- </li>
- <li>
- <a
- className="dropdown-item"
- id="js-new-issue"
- role="menuitem"
- href={buildRepoURL('issues/new')}
- >
- Reference in new issue
- </a>
- </li>
- </ul>
- </details-menu>
- </details>
-));
+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});
+ const blobToolbar = domify(html).querySelector('.BlobToolbar')!;
+ select<HTMLAnchorElement>('#js-view-git-blame', blobToolbar)!.href = new GitHubURL(location.href).assign({route: 'blame'}).href;
+ select<HTMLAnchorElement>('#js-new-issue', blobToolbar)!.href = buildRepoURL('issues/new');
+ return blobToolbar;
+});
const buttonBodyMap = new WeakMap<Element, Element | Promise<Element>>();
@@ -119,9 +68,9 @@ async function showSource(): Promise<void> {
sourceButton.classList.add('selected');
renderedButton.classList.remove('selected');
blurButton(sourceButton);
- (await source).before(lineActions());
-
dispatchEvent(sourceButton, 'rgh:view-markdown-source');
+
+ (await source).before(await lineActions());
}
async function showRendered(): Promise<void> {
@@ -137,9 +86,9 @@ async function showRendered(): Promise<void> {
sourceButton.classList.remove('selected');
renderedButton.classList.add('selected');
blurButton(renderedButton);
- lineActions().remove();
-
dispatchEvent(sourceButton, 'rgh:view-markdown-rendered');
+
+ (await lineActions()).remove();
}
async function init(): Promise<void> {