-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexecute.js
55 lines (46 loc) · 1.79 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
47
48
49
50
51
52
53
54
55
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,
});
}
}
};
let lastLocation = location.pathname;
window.history.pushState = new Proxy(window.history.pushState, {
apply: (target, a, args) => {
const isPathnameChanged = args.length >= 3 ? location.pathname !== args[2].split("#")[0] : true
if(args.length >= 3) lastLocation = args[2].split("#")[0]
target.apply(a, args);
if (modules.length !== 0 && isEverythingLoaded() && isPathnameChanged) execute();
},
});
window.addEventListener("popstate", (event) => {
const isPathnameChanged = location.pathname !== lastLocation
if (modules.length !== 0 && isEverythingLoaded() && isPathnameChanged) execute();
})