-
Notifications
You must be signed in to change notification settings - Fork 7
/
execute.js
46 lines (39 loc) · 1.36 KB
/
execute.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
const modules = [];
let loadedScripts = 0;
const isEverythingLoaded = () =>
document.querySelectorAll(".injected-script").length === loadedScripts;
window.appendModule = (...args) => {
modules.push(...args);
loadedScripts++;
if (isEverythingLoaded()) execute();
};
const execute = () => {
if (!modules.length)
console.warn(
"Script tried executing before all files loaded or all patches are disabled",
);
const isFirstRun = !window.iWasThere; // :)
window.iWasThere = true;
for (const { onlyOnReloads, doesRunHere, isLoaded, run } of modules) {
if (onlyOnReloads && !isFirstRun) continue;
if (doesRunHere !== undefined && !doesRunHere()) continue;
if (!isLoaded || isLoaded()) run();
else {
const observer = new MutationObserver((mutationsList, observer) => {
if (!isLoaded()) return;
observer.disconnect();
run();
});
observer.observe(document.body, {
subtree: true,
childList: true,
});
}
}
};
window.history.pushState = new Proxy(window.history.pushState, {
apply: (target, a, args) => {
target.apply(a, args);
if (modules.length !== 0 && isEverythingLoaded()) execute();
},
});