From da89c8ee20aa2d9ce8ac4fb5fc7225265539a8ab Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Sun, 25 Sep 2022 16:36:04 +0700 Subject: Warp speed 🚀 (#6006) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/features/embed-gist-inline.tsx | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'source/features/embed-gist-inline.tsx') 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 => + 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 { 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 { } } -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, }); -- cgit v1.2.3