This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathcontentScript.js
73 lines (66 loc) · 1.99 KB
/
contentScript.js
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
// Use `text` instead of `src` to get around Chromium bug of execution order
// when using `src`, resulting in a race condition.
// https://bugs.chromium.org/p/chromium/issues/detail?id=634381#c3
const script = document.createElement('script');
script.text = `
(() => {
/**
* This script injected by the installed three.js developer
* tools extension.
* https://github.com/threejs/three-devtools
*/
const $devtoolsReady = Symbol('devtoolsReady');
const $backlog = Symbol('backlog');
// The __THREE_DEVTOOLS__ target is small and light-weight, and collects
// events triggered until the devtools panel is ready, which is when
// the events are flushed.
const target = new class ThreeDevToolsTarget extends EventTarget {
constructor() {
super();
this[$devtoolsReady] = false;
this[$backlog] = [];
this.addEventListener('devtools-ready', e => {
this[$devtoolsReady] = true;
for (let event of this[$backlog]) {
this.dispatchEvent(event);
}
}, { once: true });
}
dispatchEvent(event) {
if (this[$devtoolsReady] || event.type === 'devtools-ready') {
super.dispatchEvent(event);
} else {
this[$backlog].push(event);
}
}
}
Object.defineProperty(window, '__THREE_DEVTOOLS__', {
value: target,
});
})();
`;
script.onload = () => {
script.parentNode.removeChild(script);
}
(document.head || document.documentElement).appendChild(script);
window.addEventListener('message', e => {
if (e.source !== window ||
typeof e.data !== 'object' ||
e.data.id !== 'three-devtools') {
return;
}
// Don't bring in the 35kb polyfill on every page
// for a single command that doesn't matter if its callback
// promise; handle this manually.
const extRoot = globalThis.chrome ? globalThis.chrome : globalThis.browser;
try {
extRoot.runtime.sendMessage(e.data);
} catch (error) {
console.error(error);
extRoot.runtime.sendMessage({
type: 'error',
id: 'three-devtools',
data: error.toString(),
});
}
});