-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
155 lines (137 loc) · 4.6 KB
/
Copy pathcontent.js
File metadata and controls
155 lines (137 loc) · 4.6 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const ID = 'codecopy-toast';
const FONTS = [
['Vintaface-Regular', chrome.runtime.getURL('assets/Vintaface-Regular.woff2'), 400, '26px'],
];
const SKIP = new Set(['HTML', 'BODY']);
const SHINE_MS = 200; // matches --cc-shine in content.css
const TOAST = {
copied: ['Copied', 600],
failed: ['Copy failed', 600],
off: ['Code Copy Deactivated', 2_000],
on: ['Code Copy Activated', 4_000],
};
const state = { alt: false, mark: null, on: false };
const mk = (tag, props) => Object.assign(document.createElement(tag), props);
const all = (...fs) => (x) => fs.every((f) => f(x));
const when = (node, ok, find = (n) => n) => {
const n = node instanceof Element ? node : null;
return n && ok(n) ? find(n) || null : null;
};
const on = (fn) => (...a) => state.on && fn(...a);
const pulse = (ms) => {
let t = 0;
return (fn) => { clearTimeout(t); t = setTimeout(fn, ms); };
};
const injectFaces = (urls) => {
if (document.getElementById('codecopy-font')) return;
const faces = FONTS.map(([family, , weight], i) =>
`@font-face{font-family:'${family}';src:url("${urls[i]}") format("woff2");font-weight:${weight};font-style:normal;font-display:swap}`,
).join('');
document.documentElement.append(mk('style', { id: 'codecopy-font', textContent: faces }));
};
Promise.all(FONTS.map(([family, src, , size]) =>
fetch(src)
.then((r) => (r.ok ? r.blob() : Promise.reject(new Error(`font ${r.status}`))))
.then((blob) => ({ family, size, url: URL.createObjectURL(blob) })),
))
.then((loaded) => {
injectFaces(loaded.map(({ url }) => url));
return Promise.all(loaded.map(({ family, size }) => document.fonts.load(`${size} '${family}'`)));
})
.catch(console.error);
const toast = (() => {
let el, hideT;
return ([text, ms]) => {
el ??= mk('div', { id: ID, ariaLive: 'polite' });
if (!el.isConnected) document.body.append(el);
el.textContent = text;
el.classList.add('show');
clearTimeout(hideT);
hideT = setTimeout(() => el.classList.remove('show'), ms);
};
})();
const endShine = pulse(SHINE_MS);
const shine = (el) => {
el.classList.remove('codecopy-shine');
void el.offsetWidth;
el.classList.add('codecopy-shine');
endShine(() => el.classList.remove('codecopy-shine'));
};
const own = (el) => el?.closest?.(`#${ID}`);
const hasText = (el) => Boolean(el?.innerText?.trim());
const notOwn = (n) => !own(n);
const barePre = (p) => !p.querySelector('code') && hasText(p);
const pickable = all(notOwn, (n) => !SKIP.has(n.tagName), hasText);
const codeBlock = (n) => when(n.closest('code'), hasText) || when(n.closest('pre'), barePre);
const mod = (e) => state.alt || e.altKey;
const at = (e) => document.elementFromPoint(e.clientX, e.clientY);
const hit = (e) => (mod(e) ? when(at(e), pickable) : when(e.target, notOwn, codeBlock));
const shineIf = (ok, el) => ok && !state.alt && el && shine(el);
const notify = (spec, ok, el) => {
toast(spec);
shineIf(ok, el);
return ok;
};
const copy = (el) => hasText(el)
? navigator.clipboard.writeText(el.innerText)
.then(() => notify(TOAST.copied, true, el))
.catch(() => notify(TOAST.failed, false))
: Promise.resolve(false);
const cls = (el, name, add) => el?.classList[add ? 'add' : 'remove'](name);
const mark = (el) => {
if (state.mark === el) return;
cls(state.mark, 'codecopy-x', false);
state.mark = el;
cls(el, 'codecopy-x', true);
};
const setAlt = (on) => {
state.alt = on;
document.documentElement.classList.toggle('codecopy-alt', on);
if (!on) mark(null);
};
const swallow = (e) => {
e.preventDefault();
e.stopPropagation();
};
const isAlt = (e) => e.key === 'Alt';
const altEv = (down) => on((e) => isAlt(e) && setAlt(down));
const onClick = (e) => {
const el = hit(e);
if (!el || e.defaultPrevented) return;
swallow(e);
copy(el);
};
const listeners = [
['keydown', altEv(true)],
['keyup', altEv(false)],
['blur', on(() => setAlt(false))],
['mousemove', on((e) => mod(e) && mark(hit(e)))],
['click', on(onClick), { capture: true }],
];
const wire = (on) => {
const op = on ? 'addEventListener' : 'removeEventListener';
for (const [name, fn, opts] of listeners) window[op](name, fn, opts ?? false);
};
const setOn = (on) => {
if (state.on === on) return;
state.on = on;
document.documentElement.classList.toggle('codecopy-on', on);
wire(on);
if (!on) setAlt(false);
};
const applyPage = (on, show) => {
setOn(Boolean(on));
if (show) toast(TOAST[on ? 'on' : 'off']);
};
globalThis.__codecopyApply = applyPage;
if (!globalThis.__codecopyReady) {
globalThis.__codecopyReady = true;
try {
chrome.runtime.onMessage.addListener((m) => {
if (m?.type !== 'active') return;
applyPage(m.value, m.toast);
});
} catch (e) {
if (!/invalidated/i.test(String(e))) console.error(e);
}
}