-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjected.js
162 lines (145 loc) · 4.3 KB
/
injected.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
(function () {
const nodeList = [];
const blockType = 'javascript/blocked';
const activeType = 'application/javascript';
const _createElement = document.createElement;
const createDummyScript = (...args) => {
const dummy = _createElement.bind(document)(...args);
dummy._activated = false;
dummy._setAttribute = dummy.setAttribute.bind(dummy);
dummy._setAttribute('type', blockType);
Object.defineProperties(dummy, {
'src': {
get() {
if (!dummy._activated) {
return dummy._src;
}
return dummy.getAttribute('src');
},
set(value) {
if (!dummy._activated) {
dummy._src = value;
} else {
dummy._setAttribute('src', value);
}
return true;
}
},
'type': {
set(value) {
const typeValue = this._activated ? value : blockType;
dummy._setAttribute('type', typeValue);
return true
}
}
});
dummy.setAttribute = function (name, value) {
if (name === 'type' || name === 'src') {
dummy[name] = value
} else {
dummy._setAttribute(name, value);
}
}
console.warn('dummy node created');
return dummy;
};
document.createElement = function (...args) {
if (args[0].toLowerCase() === 'script') {
return createDummyScript(...args);
}
return _createElement.bind(document)(...args);
};
const frameCallback = async () => {
return new Promise((resolve) => {
window.requestAnimationFrame((timestamp) => resolve(timestamp));
});
};
const pollForIde = async (timeout) => {
const pollStart = Date.now();
let prev = await frameCallback();
let count = 0;
while (true) {
const now = Date.now();
if ((now - pollStart) >= timeout) {
console.warn('idle with timeout', count);
return;
}
const t = await frameCallback();
if ((t - prev) < 25) {
count++;
} else {
count = 0;
}
prev = t;
if (count >= 70) {
return;
}
}
};
const enableNextScript = async () => {
await pollForIde(8000);
if (nodeList.length) {
const s = nodeList.splice(0, 1)[0];
if (s) {
s.type = activeType;
if (s._src) {
s.src = s._src;
} else {
s.textContent = ' ' + s.textContent;
}
s._activated = true;
console.warn(`enabling script (left: ${nodeList.length})`, s.src);
await enableNextScript();
}
}
};
if (document.readyState === 'complete') {
enableNextScript();
} else {
document.addEventListener('readystatechange', event => {
if (event.target.readyState === 'complete') {
enableNextScript();
}
});
}
const observer = new MutationObserver(mutations => {
mutations.forEach(({ addedNodes }) => {
if (addedNodes) {
addedNodes.forEach(node => {
if (node.nodeType === Node.ELEMENT_NODE && node.tagName === 'SCRIPT') {
if (node._activated) {
return;
}
if (node.type !== blockType) {
if (node.src.indexOf('poweredbyslick') >= 0 || node.src.indexOf('poweredbyslick') >= 0 || node.src.indexOf('googletagmanager') >= 0) {
return;
}
node.type = blockType;
node._src = node.src;
node._activated = false;
node.removeAttribute('src');
}
const beforeScriptExecuteListener = function (event) {
if (node.getAttribute('type') === blockType) {
event.preventDefault();
}
node.removeEventListener('beforescriptexecute', beforeScriptExecuteListener)
}
node.addEventListener('beforescriptexecute', beforeScriptExecuteListener)
if (nodeList.indexOf(node) < 0) {
nodeList.push(node);
console.warn('node blocked', node._src);
if (document.readyState === 'complete' && (nodeList.length === 1)) {
enableNextScript();
}
}
}
});
}
});
});
observer.observe(document.body || document.documentElement || document, {
childList: true,
subtree: true
});
})();