forked from adblockplus/adblockpluschrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
include.preload.js
445 lines (386 loc) · 11.4 KB
/
include.preload.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-present eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
let {ElemHideEmulation} =
require("./adblockpluscore/lib/content/elemHideEmulation");
let contentFiltering;
let collapsedSelectors = new Set();
function getURLFromElement(element)
{
if (element.localName == "object")
{
if (element.data)
return element.data;
for (let child of element.children)
{
if (child.localName == "param" && child.name == "movie" && child.value)
return new URL(child.value, document.baseURI).href;
}
return null;
}
return element.currentSrc || element.src;
}
function getSelectorForBlockedElement(element)
{
// Setting the "display" CSS property to "none" doesn't have any effect on
// <frame> elements (in framesets). So we have to hide it inline through
// the "visibility" CSS property.
if (element.localName == "frame")
return null;
// If the <video> or <audio> element contains any <source> children,
// we cannot address it in CSS by the source URL; in that case we
// don't "collapse" it using a CSS selector but rather hide it directly by
// setting the style="..." attribute.
if (element.localName == "video" || element.localName == "audio")
{
for (let child of element.children)
{
if (child.localName == "source")
return null;
}
}
let selector = "";
for (let attr of ["src", "srcset"])
{
let value = element.getAttribute(attr);
if (value && attr in element)
selector += "[" + attr + "=" + CSS.escape(value) + "]";
}
return selector ? element.localName + selector : null;
}
function hideElement(element, properties)
{
let {style} = element;
let actualProperties = [];
if (element.localName == "frame")
actualProperties = properties = [["visibility", "hidden"]];
else if (!properties)
actualProperties = properties = [["display", "none"]];
for (let [key, value] of properties)
style.setProperty(key, value, "important");
if (!actualProperties)
{
actualProperties = [];
for (let [key] of properties)
actualProperties.push([key, style.getPropertyValue(key)]);
}
new MutationObserver(() =>
{
for (let [key, value] of actualProperties)
{
if (style.getPropertyValue(key) != value ||
style.getPropertyPriority(key) != "important")
style.setProperty(key, value, "important");
}
}).observe(
element, {
attributes: true,
attributeFilter: ["style"]
}
);
}
function collapseElement(element)
{
let selector = getSelectorForBlockedElement(element);
if (selector)
{
if (!collapsedSelectors.has(selector))
{
contentFiltering.addSelectors([selector], "collapsing", true);
collapsedSelectors.add(selector);
}
}
else
{
hideElement(element);
}
}
function startElementCollapsing()
{
let deferred = null;
browser.runtime.onMessage.addListener((message, sender) =>
{
if (message.type != "filters.collapse")
return;
if (document.readyState == "loading")
{
if (!deferred)
{
deferred = new Map();
document.addEventListener("DOMContentLoaded", () =>
{
for (let [selector, urls] of deferred)
{
for (let element of document.querySelectorAll(selector))
{
if (urls.has(getURLFromElement(element)))
collapseElement(element);
}
}
deferred = null;
});
}
let urls = deferred.get(message.selector) || new Set();
deferred.set(message.selector, urls);
urls.add(message.url);
}
else
{
for (let element of document.querySelectorAll(message.selector))
{
if (getURLFromElement(element) == message.url)
collapseElement(element);
}
}
});
}
function checkSitekey()
{
let attr = document.documentElement.getAttribute("data-adblockkey");
if (attr)
browser.runtime.sendMessage({type: "filters.addKey", token: attr});
}
function ElementHidingTracer(selectors, exceptions)
{
this.selectors = selectors;
this.exceptions = exceptions;
this.changedNodes = [];
this.timeout = null;
this.observer = new MutationObserver(this.observe.bind(this));
this.trace = this.trace.bind(this);
if (document.readyState == "loading")
document.addEventListener("DOMContentLoaded", this.trace);
else
this.trace();
}
ElementHidingTracer.prototype = {
checkNodes(nodes)
{
let effectiveSelectors = [];
let effectiveExceptions = [];
for (let selector of this.selectors)
{
for (let node of nodes)
{
if (node.querySelector(selector))
{
effectiveSelectors.push(selector);
break;
}
}
}
for (let exception of this.exceptions)
{
for (let node of nodes)
{
if (node.querySelector(exception.selector))
{
effectiveExceptions.push(exception.text);
break;
}
}
}
if (effectiveSelectors.length > 0 || effectiveExceptions.length > 0)
{
browser.runtime.sendMessage({
type: "hitLogger.traceElemHide",
selectors: effectiveSelectors,
filters: effectiveExceptions
});
}
},
onTimeout()
{
this.checkNodes(this.changedNodes);
this.changedNodes = [];
this.timeout = null;
},
observe(mutations)
{
// Forget previously changed nodes that are no longer in the DOM.
for (let i = 0; i < this.changedNodes.length; i++)
{
if (!document.contains(this.changedNodes[i]))
this.changedNodes.splice(i--, 1);
}
for (let mutation of mutations)
{
let node = mutation.target;
// Ignore mutations of nodes that aren't in the DOM anymore.
if (!document.contains(node))
continue;
// Since querySelectorAll() doesn't consider the root itself
// and since CSS selectors can also match siblings, we have
// to consider the parent node for attribute mutations.
if (mutation.type == "attributes")
node = node.parentNode;
let addNode = true;
for (let i = 0; i < this.changedNodes.length; i++)
{
let previouslyChangedNode = this.changedNodes[i];
// If we are already going to check an ancestor of this node,
// we can ignore this node, since it will be considered anyway
// when checking one of its ancestors.
if (previouslyChangedNode.contains(node))
{
addNode = false;
break;
}
// If this node is an ancestor of a node that previously changed,
// we can ignore that node, since it will be considered anyway
// when checking one of its ancestors.
if (node.contains(previouslyChangedNode))
this.changedNodes.splice(i--, 1);
}
if (addNode)
this.changedNodes.push(node);
}
// Check only nodes whose descendants have changed, and not more often
// than once a second. Otherwise large pages with a lot of DOM mutations
// (like YouTube) freeze when the devtools panel is active.
if (this.timeout == null)
this.timeout = setTimeout(this.onTimeout.bind(this), 1000);
},
trace()
{
this.checkNodes([document]);
this.observer.observe(
document,
{
childList: true,
attributes: true,
subtree: true
}
);
},
disconnect()
{
document.removeEventListener("DOMContentLoaded", this.trace);
this.observer.disconnect();
clearTimeout(this.timeout);
}
};
function ContentFiltering()
{
this.styles = new Map();
this.tracer = null;
this.cssProperties = null;
this.elemHideEmulation = new ElemHideEmulation(this.hideElements.bind(this));
}
ContentFiltering.prototype = {
addRulesInline(rules, groupName = "standard", appendOnly = false)
{
let style = this.styles.get(groupName);
if (style && !appendOnly)
{
while (style.sheet.cssRules.length > 0)
style.sheet.deleteRule(0);
}
if (rules.length == 0)
return;
if (!style)
{
// Create <style> element lazily, only if we add styles. Add it to
// the <head> or <html> element. If we have injected a style element
// before that has been removed (the sheet property is null), create a
// new one.
style = document.createElement("style");
(document.head || document.documentElement).appendChild(style);
// It can happen that the frame already navigated to a different
// document while we were waiting for the background page to respond.
// In that case the sheet property may stay null, after adding the
// <style> element.
if (!style.sheet)
return;
this.styles.set(groupName, style);
}
for (let rule of rules)
style.sheet.insertRule(rule, style.sheet.cssRules.length);
},
addSelectors(selectors, groupName = "standard", appendOnly = false)
{
browser.runtime.sendMessage({
type: "content.injectSelectors",
selectors,
groupName,
appendOnly
}).then(rules =>
{
if (rules)
{
// Insert the rules inline if we have been instructed by the background
// page to do so. This is rarely the case, except on platforms that do
// not support user stylesheets via the browser.tabs.insertCSS API, i.e.
// Firefox <53 and Chrome <66.
// Once all supported platforms have implemented this API, we can remove
// the code below. See issue #5090.
// Related Chrome and Firefox issues:
// https://bugs.chromium.org/p/chromium/issues/detail?id=632009
// https://bugzilla.mozilla.org/show_bug.cgi?id=1310026
this.addRulesInline(rules, groupName, appendOnly);
}
});
},
hideElements(elements, filters)
{
for (let element of elements)
hideElement(element, this.cssProperties);
if (this.tracer)
{
browser.runtime.sendMessage({
type: "hitLogger.traceElemHide",
selectors: [],
filters
});
}
},
apply(filterTypes)
{
browser.runtime.sendMessage({
type: "content.applyFilters",
filterTypes
}).then(response =>
{
if (this.tracer)
{
this.tracer.disconnect();
this.tracer = null;
}
if (response.inline)
this.addRulesInline(response.rules);
if (response.trace)
{
this.tracer = new ElementHidingTracer(
response.selectors,
response.exceptions
);
}
this.cssProperties = response.cssProperties;
this.elemHideEmulation.apply(response.emulatedPatterns);
});
}
};
if (document instanceof HTMLDocument)
{
checkSitekey();
contentFiltering = new ContentFiltering();
contentFiltering.apply();
startElementCollapsing();
}
window.collapseElement = collapseElement;
window.contentFiltering = contentFiltering;
window.getURLFromElement = getURLFromElement;