1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import React from 'dom-chef';
import domify from 'doma';
import * as pageDetect from 'github-url-detection';
import mem from 'mem';
import features from '../feature-manager.js';
import {getCleanPathname} from '../github-helpers/index.js';
import observe from '../helpers/selector-observer.js';
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') {
return getCleanPathname(link);
}
if (link.host === location.host && link.pathname.startsWith('gist/')) {
return link.pathname.replace('/gist', '').replace(/\/$/, '');
}
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
}
const isOnlyChild = (link: HTMLAnchorElement): boolean => link.textContent.trim() === link.parentElement!.textContent.trim();
async function embedGist(link: HTMLAnchorElement): Promise<void> {
const info = <em> (loading)</em>;
link.after(info);
try {
const gistData = await fetchGist(link.href);
if (gistData.div.length > 10_000) {
info.textContent = ' (too large to embed)';
return;
}
const fileCount: number = gistData.files.length;
if (fileCount > 1) {
info.textContent = ` (${fileCount} files)`;
} else {
const container = <div/>;
container.attachShadow({mode: 'open'}).append(
<style>{`
.gist .gist-data {
max-height: 16em;
overflow-y: auto;
}
`}
</style>,
<link rel="stylesheet" href={gistData.stylesheet}/>,
domify.one(gistData.div)!,
);
link.parentElement!.after(container);
info.remove();
}
} catch {
info.remove();
}
}
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,
],
init,
});
|