forked from banocean/ifv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.js
44 lines (39 loc) · 1.24 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
const modules = [];
window.appendModule = (...args) => {
modules.push(...args);
if (
document.querySelectorAll(".injected-script").length === modules.length
) {
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);
execute();
},
});