-
Notifications
You must be signed in to change notification settings - Fork 2
/
nagfree-loader.js
70 lines (55 loc) · 1.62 KB
/
nagfree-loader.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
import { $, waitForSelector } from './utils.js';
// The voodoo bootstrap to import the actual extension code
const script = $('script[src*="nagfree-loader.js"]');
const URL_CHANGE_TIMEOUT = 300;
const WAIT_FOR_SELECTOR_TIMEOUT = 300;
function onUrlChange(callback) {
// This is pretty horrible
let oldUrl = window.location.href;
function check() {
let newUrl = window.location.href;
if (newUrl !== oldUrl) {
callback();
oldUrl = newUrl;
}
window.requestIdleCallback(check, { timeout : URL_CHANGE_TIMEOUT });
}
check();
}
function runJs(js) {
// If we have a function, run immediately, otherwise look at the
// options
if (typeof js === 'function') {
js();
} else {
function run() {
if (js.waitForSelector) {
waitForSelector(js.waitForSelector, WAIT_FOR_SELECTOR_TIMEOUT).then(js.run);
} else {
js.run();
}
}
if (js.runOnUrlChange) {
// Run every time the URL changes and one time at load
onUrlChange(() => {
run();
});
run();
} else {
run();
}
}
}
async function loadModules() {
let then = Date.now();
let scripts = JSON.parse(script.dataset.scripts);
scripts = scripts.map(async (src) => {
const module = await import(src);
if (module.default && module.default.js) {
runJs(module.default.js);
}
});
await Promise.all(scripts);
// console.log(`Running took ${Date.now() - then}ms`);
}
loadModules();