-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-module.js
82 lines (82 loc) · 2.6 KB
/
no-module.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
74
75
76
77
78
79
80
81
82
import('css-observe/css-observe.js');
export class NoModule extends HTMLElement {
static cache = {};
connectedCallback() {
addListener(this.getRootNode());
}
}
customElements.define('no-module', NoModule);
Array.from(document.querySelectorAll('script[nomodule="ish"]')).forEach((scriptTag) => {
const st = scriptTag;
st.dataset.found = 'true';
loadScript(st);
});
function addListener(node) {
if (node === document)
return;
const cssObserve = document.createElement('css-observe');
cssObserve.observe = true;
cssObserve.selector = 'script[nomodule="ish"]';
cssObserve.addEventListener('latest-match-changed', (e) => {
const st = e.detail.value;
if (st.dataset.found === 'true')
return;
st.dataset.found = 'true';
loadScript(st);
});
cssObserve.customStyles = /* css */ `
script[nomodule="ish"]{
display:block;
}
script[nomodule="ish"][data-found]{
display:none;
}
`;
node.appendChild(cssObserve);
}
addListener(document.head);
async function loadScript(scriptElement) {
const key = (new Date()).valueOf().toString() + Math.random();
window[key] = scriptElement;
scriptElement._modExport = {};
let innerText;
if (scriptElement.src) {
if (NoModule.cache[scriptElement.src] === undefined) {
const resp = await fetch(scriptElement.src);
const text = await resp.text();
NoModule.cache[scriptElement.src] = text;
}
innerText = NoModule.cache[scriptElement.src];
}
else {
innerText = scriptElement.innerText;
}
innerText = innerText.replace(/selfish/g, `window['${key}']`);
const splitText = innerText.split('export const ');
let iPos = 0;
const winKey = `window['${key}']`;
for (let i = 1, ii = splitText.length; i < ii; i++) {
const token = splitText[i];
const iPosOfEq = token.indexOf('=');
const lhs = token.substr(0, iPosOfEq).trim();
splitText[i] = `const ${lhs} = ${winKey}._modExport.${lhs} = ${token.substr(iPosOfEq + 1)};`;
}
let modifiedText = splitText.join('');
modifiedText = /* js */ `
try{
${modifiedText}
}catch(err){
window['${key}'].dispatchEvent(new CustomEvent('err', {
detail: {
message: err
}
}))
}
window['${key}'].dispatchEvent(new Event('loaded'));
window['${key}'].dataset.loaded = 'true';
`;
const scriptTag = document.createElement('script');
scriptTag.type = 'module';
scriptTag.innerHTML = modifiedText;
document.head.appendChild(scriptTag);
}