forked from adblockplus/backup-adblockpluschrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
include.preload.js
200 lines (173 loc) · 5.56 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
/*
* This file is part of Adblock Plus <http://adblockplus.org/>,
* Copyright (C) 2006-2014 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/>.
*/
var SELECTOR_GROUP_SIZE = 20;
var typeMap = {
"img": "IMAGE",
"input": "IMAGE",
"audio": "MEDIA",
"video": "MEDIA",
"frame": "SUBDOCUMENT",
"iframe": "SUBDOCUMENT"
};
function checkCollapse(element)
{
var tag = element.localName;
if (tag in typeMap)
{
// This element failed loading, did we block it?
var url = element.src;
if (!url || !/^https?:/i.test(url))
return;
ext.backgroundPage.sendMessage(
{
type: "should-collapse",
url: url,
mediatype: typeMap[tag]
},
function(response)
{
if (response && element.parentNode)
{
var property = "display";
var value = "none";
// <frame> cannot be removed, doing that will mess up the frameset
if (tag == "frame")
{
property = "visibility";
value = "hidden";
}
// <input type="image"> elements try to load their image again
// when the "display" CSS property is set. So we have to check
// that it isn't already collapsed to avoid an infinite recursion.
if (element.style.getPropertyValue(property) != value ||
element.style.getPropertyPriority(property) != "important")
element.style.setProperty(property, value, "important");
}
}
);
}
}
function checkSitekey()
{
var attr = document.documentElement.getAttribute("data-adblockkey");
if (attr)
ext.backgroundPage.sendMessage({type: "add-sitekey", token: attr});
}
function hasInlineURL(element, attribute)
{
var value = element.getAttribute(attribute);
return value == null || /^\s*(javascript:|about:|$)/i.test(value);
}
function isInlineFrame(element)
{
switch (element.localName)
{
case "iframe":
return hasInlineURL(element, "src") || element.hasAttribute("srcdoc");
case "frame":
return hasInlineURL(element, "src");
case "object":
return hasInlineURL(element, "data") && element.contentDocument;
default:
return false;
}
}
function resolveURL(url)
{
var a = document.createElement("a");
a.href = url;
return a.href;
}
function init(document)
{
// prior to Chrome 37, content scripts don't run on about:blank
// and about:srcdoc. So we have to apply element hiding and collapsing
// from the parent frame, when inline frames are loaded.
var match = navigator.userAgent.match(/\bChrome\/(\d+)/);
var fixInlineFrames = match && parseInt(match[1], 10) < 37;
// use Shadow DOM if available to don't mess with web pages that
// rely on the order of their own <style> tags (#309). However we
// must not create the shadow root in the response callback passed
// to sendMessage(), otherwise Chrome breaks some websites (#450).
var shadow = null;
if ("createShadowRoot" in document.documentElement)
{
shadow = document.documentElement.createShadowRoot();
shadow.appendChild(document.createElement("shadow"));
}
// Sets the currently used CSS rules for elemhide filters
var setElemhideCSSRules = function(selectors)
{
if (selectors.length == 0)
return;
var style = document.createElement("style");
style.setAttribute("type", "text/css");
if (shadow)
{
shadow.appendChild(style);
for (var i = 0; i < selectors.length; i++)
selectors[i] = "::content " + selectors[i];
}
else
{
// 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(style);
}
var setRules = function()
{
// The sheet property might not exist yet if the
// <style> element was created for a sub frame
if (!style.sheet)
{
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; selectors.length > 0; i++)
{
var selector = selectors.splice(0, SELECTOR_GROUP_SIZE).join(", ");
style.sheet.insertRule(selector + " { display: none !important; }", i);
}
};
setRules();
};
document.addEventListener("error", function(event)
{
checkCollapse(event.target);
}, true);
document.addEventListener("load", function(event)
{
var element = event.target;
if (/^i?frame$/.test(element.localName))
checkCollapse(element);
if (fixInlineFrames && isInlineFrame(element))
{
init(element.contentDocument);
for (var tagName in typeMap)
Array.prototype.forEach.call(element.contentDocument.getElementsByTagName(tagName), checkCollapse);
}
}, true);
ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules);
}
if (document instanceof HTMLDocument)
{
checkSitekey();
init(document);
}