-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinclude.preload.js
106 lines (90 loc) · 2.9 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
var SELECTOR_GROUP_SIZE = 20;
var elemhideElt = null;
// Sets the currently used CSS rules for elemhide filters
function setElemhideCSSRules(selectors)
{
if (elemhideElt && elemhideElt.parentNode)
elemhideElt.parentNode.removeChild(elemhideElt);
if (!selectors)
return;
elemhideElt = document.createElement("style");
elemhideElt.setAttribute("type", "text/css");
// Try to insert the style into the <head> tag, inserting directly under the
// document root breaks dev tools functionality:
// http://code.google.com/p/chromium/issues/detail?id=178109
(document.head || document.documentElement).appendChild(elemhideElt);
var elt = elemhideElt; // Use a local variable to avoid racing conditions
function setRules()
{
if (!elt.sheet)
{
// Stylesheet didn't initialize yet, wait a little longer
window.setTimeout(setRules, 0);
return;
}
// WebKit apparently chokes when the selector list in a CSS rule is huge.
// So we split the elemhide selectors into groups.
for (var i = 0, j = 0; i < selectors.length; i += SELECTOR_GROUP_SIZE, j++)
{
var selector = selectors.slice(i, i + SELECTOR_GROUP_SIZE).join(", ");
elt.sheet.insertRule(selector + " { display: none !important; }", j);
}
}
setRules();
}
var typeMap = {
"img": "IMAGE",
"input": "IMAGE",
"audio": "MEDIA",
"video": "MEDIA",
"frame": "SUBDOCUMENT",
"iframe": "SUBDOCUMENT"
};
function checkCollapse(event)
{
var target = event.target;
var tag = target.localName;
var expectedEvent = (tag == "iframe" || tag == "frame" ? "load" : "error");
if (tag in typeMap && event.type == expectedEvent)
{
// This element failed loading, did we block it?
var url = target.src;
if (!url)
return;
ext.backgroundPage.sendMessage(
{
type: "should-collapse",
url: url,
mediatype: typeMap[tag]
},
function(response)
{
if (response && target.parentNode)
{
// <frame> cannot be removed, doing that will mess up the frameset
if (tag == "frame")
target.style.setProperty("visibility", "hidden", "!important");
else
target.parentNode.removeChild(target);
}
}
);
}
}
function init()
{
// Make sure this is really an HTML page, as Chrome runs these scripts on just about everything
if (!(document.documentElement instanceof HTMLElement))
return;
document.addEventListener("error", checkCollapse, true);
document.addEventListener("load", checkCollapse, true);
var attr = document.documentElement.getAttribute("data-adblockkey");
if (attr)
ext.backgroundPage.sendMessage({type: "add-key-exception", token: attr});
ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules);
}
// In Chrome 18 the document might not be initialized yet
if (document.documentElement)
init();
else
window.setTimeout(init, 0);