diff options
author | 2022-09-25 16:36:04 +0700 | |
---|---|---|
committer | 2022-09-25 16:36:04 +0700 | |
commit | da89c8ee20aa2d9ce8ac4fb5fc7225265539a8ab (patch) | |
tree | cd3ed5eea516a54ffbae7caf64095f8b00bd52d9 /source/features/embed-gist-inline.tsx | |
parent | 74785717376b427ad7177e5783af145cd0f8d861 (diff) | |
download | refined-github-da89c8ee20aa2d9ce8ac4fb5fc7225265539a8ab.tar.gz refined-github-da89c8ee20aa2d9ce8ac4fb5fc7225265539a8ab.tar.zst refined-github-da89c8ee20aa2d9ce8ac4fb5fc7225265539a8ab.zip |
Warp speed 🚀 (#6006)
Diffstat (limited to 'source/features/embed-gist-inline.tsx')
-rw-r--r-- | source/features/embed-gist-inline.tsx | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/source/features/embed-gist-inline.tsx b/source/features/embed-gist-inline.tsx index b9e06955..b0fdc7a2 100644 --- a/source/features/embed-gist-inline.tsx +++ b/source/features/embed-gist-inline.tsx @@ -1,10 +1,23 @@ import React from 'dom-chef'; import domify from 'doma'; -import select from 'select-dom'; import * as pageDetect from 'github-url-detection'; +import mem from 'mem'; import features from '../feature-manager'; import {getCleanPathname} from '../github-helpers'; +import observe from '../helpers/selector-observer'; + +type GistData = { + div: string; + files: unknown[]; + stylesheet: string; +}; + +// Fetch via background.js due to CORB policies. Also memoize to avoid multiple requests. +const fetchGist = mem( + async (url: string): Promise<GistData> => + browser.runtime.sendMessage({fetchJSON: `${url}.json`}), +); function parseGistLink(link: HTMLAnchorElement): string | undefined { if (link.host === 'gist.github.com') { @@ -18,6 +31,7 @@ function parseGistLink(link: HTMLAnchorElement): string | undefined { return undefined; } +// TODO: Replace with updated github-url-detection: isGistFile(link) function isGist(link: HTMLAnchorElement): boolean { return parseGistLink(link)?.replace(/[^/]/g, '').length === 1; // Exclude user links and file links } @@ -29,8 +43,7 @@ async function embedGist(link: HTMLAnchorElement): Promise<void> { link.after(info); try { - // Fetch via background.js due to CORB policies - const gistData = await browser.runtime.sendMessage({fetchJSON: `${link.href}.json`}); + const gistData = await fetchGist(link.href); if (gistData.div.length > 10_000) { info.textContent = ' (too large to embed)'; return; @@ -60,18 +73,18 @@ async function embedGist(link: HTMLAnchorElement): Promise<void> { } } -function init(): void { - for (const link of select.all('.js-comment-body p a:only-child')) { +function init(signal: AbortSignal): void { + observe('.js-comment-body p a:only-child', link => { if (isGist(link) && isOnlyChild(link)) { void embedGist(link); } - } + }, {signal}); } void features.add(import.meta.url, { include: [ pageDetect.hasComments, ], - deduplicate: 'has-rgh', + awaitDomReady: false, init, }); |