-
Notifications
You must be signed in to change notification settings - Fork 8
/
pageinit.back
82 lines (72 loc) · 2.41 KB
/
pageinit.back
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
//Just for backup to refer later
this.page.addInitScript(`{
const _promiseConstructor = window.Promise.constructor;
const _timeoutIds = new Set();
const _setTimeout = window.setTimeout;
const _clearTimeout = window.clearTimeout;
new MutationObserver(() => {
window.__pwBusy__("dom++");
requestAnimationFrame(() => { window.__pwBusy__("dom--"); });
}).observe(document, { attributes: true, childList: true, subtree: true });
// Patch Promise constructor
window.Promise.constructor = async (resolve, reject) => {
window.__pwBusy__("promises++");
const res = resolve && (async () => {
let val;
try {
val = await resolve();
} catch(err) {
throw err;
} finally {
window.__pwBusy__("promises--");
}
return val;
});
const rej = reject && (async () => {
let val;
try {
val = await reject();
} catch(err) {
throw err;
} finally {
window.__pwBusy__("promises--");
}
return val;
});
return _promiseConstructor.call(this, res, rej);
};
// Path window.clearTimeout
window.clearTimeout = (id) => {
_clearTimeout(id);
if (_timeoutIds.has(id)) {
_timeoutIds.delete(id);
window.__pwBusy__("timeouts--");
}
};
// Patch window.setTimeout in the near future
window.setTimeout = (...args) => {
const ms = args[1];
const isInNearFuture = ms < 1000 * 5;
if (isInNearFuture) {
window.__pwBusy__("timeouts++");
const fn = args[0];
if (typeof(fn) === "function") {
args[0] = () => {
try {
fn();
} catch(err) {
} finally {
window.__pwBusy__("timeouts--");
}
};
} else {
args[0] = "try{" + args[0] + "; }catch(err){};window.__pwBusy__('timeouts--');";
}
}
const timeoutId = _setTimeout.apply(this, args);
if (isInNearFuture) {
_timeoutIds.add(timeoutId);
}
return timeoutId;
};
}`);