Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 } | 56 } |
57 ); | 57 ); |
58 } | 58 } |
59 } | 59 } |
60 | 60 |
61 function checkExceptionKey() | 61 function checkExceptionKey() |
62 { | 62 { |
63 var attr = document.documentElement.getAttribute("data-adblockkey"); | 63 var attr = document.documentElement.getAttribute("data-adblockkey"); |
64 if (attr) | 64 if (attr) |
65 ext.backgroundPage.sendMessage({type: "add-key-exception", token: attr}); | 65 ext.backgroundPage.sendMessage({type: "add-key-exception", token: attr}); |
66 } | 66 } |
Wladimir Palant
2014/06/02 11:20:40
Why read the signature from a tag in Chrome? We re
Sebastian Noack
2014/06/03 06:55:27
This function isn't called for anonymous frames. H
| |
67 | 67 |
68 function hasInlineURL(element, attribute) | |
69 { | |
70 var value = element.getAttribute(attribute); | |
71 return value == null || /^\s*(javascript:|about:|$)/i.test(value); | |
72 } | |
73 | |
74 function isInlineFrame(element) | |
75 { | |
76 switch (element.localName) | |
77 { | |
78 case "iframe": | |
79 return hasInlineURL(element, "src") || element.hasAttribute("srcdoc"); | |
80 case "frame": | |
81 return hasInlineURL(element, "src"); | |
82 case "object": | |
83 return hasInlineURL(element, "data") && element.contentDocument; | |
84 default: | |
85 return false; | |
86 } | |
87 } | |
88 | |
68 // Converts relative to absolute URL | 89 // Converts relative to absolute URL |
69 // e.g.: foo.swf on http://example.com/whatever/bar.html | 90 // e.g.: foo.swf on http://example.com/whatever/bar.html |
70 // -> http://example.com/whatever/foo.swf | 91 // -> http://example.com/whatever/foo.swf |
71 function relativeToAbsoluteUrl(url) | 92 function relativeToAbsoluteUrl(url) |
72 { | 93 { |
73 // If URL is already absolute, don't mess with it | 94 // If URL is already absolute, don't mess with it |
74 if (!url || /^[\w\-]+:/i.test(url)) | 95 if (!url || /^[\w\-]+:/i.test(url)) |
75 return url; | 96 return url; |
76 | 97 |
77 // Leading / means absolute path | 98 // Leading / means absolute path |
78 // Leading // means network path | 99 // Leading // means network path |
79 if (url[0] == '/') | 100 if (url[0] == '/') |
80 { | 101 { |
81 if (url[1] == '/') | 102 if (url[1] == '/') |
82 return document.location.protocol + url; | 103 return document.location.protocol + url; |
83 else | 104 else |
84 return document.location.protocol + "//" + document.location.host + url; | 105 return document.location.protocol + "//" + document.location.host + url; |
85 } | 106 } |
86 | 107 |
87 // Remove filename and add relative URL to it | 108 // Remove filename and add relative URL to it |
88 var base = document.baseURI.match(/.+\//); | 109 var base = document.baseURI.match(/.+\//); |
89 if (!base) | 110 if (!base) |
90 return document.baseURI + "/" + url; | 111 return document.baseURI + "/" + url; |
91 return base[0] + url; | 112 return base[0] + url; |
92 } | 113 } |
93 | 114 |
94 function init(document) | 115 function init(document) |
95 { | 116 { |
96 var canUseShadow = "webkitCreateShadowRoot" in document.documentElement; | 117 var canUseShadow = "webkitCreateShadowRoot" in document.documentElement; |
97 var fixAnonymousFrames = false; | 118 var fixInlineFrames = false; |
98 | 119 |
99 var match = navigator.userAgent.match(/\bChrome\/(\d+)/); | 120 var match = navigator.userAgent.match(/\bChrome\/(\d+)/); |
100 if (match) | 121 if (match) |
101 { | 122 { |
102 var chromeVersion = parseInt(match[1]); | 123 var chromeVersion = parseInt(match[1]); |
103 | 124 |
104 // the <shadow> element is ignored in Chrome 32 (#309). Also Chrome 31-33 | 125 // the <shadow> element is ignored in Chrome 32 (#309). Also Chrome 31-33 |
105 // crashes in some situations on some pages when using shadow DOM (#498). | 126 // crashes in some situations on some pages when using shadow DOM (#498). |
106 // So we must not use Shadow DOM on those versions of Chrome. | 127 // So we must not use Shadow DOM on those versions of Chrome. |
107 if (chromeVersion >= 31 && chromeVersion <= 33) | 128 if (chromeVersion >= 31 && chromeVersion <= 33) |
108 canUseShadow = false; | 129 canUseShadow = false; |
109 | 130 |
110 // prior to Chrome 37, content scripts don't run in anonymous frames. | 131 // prior to Chrome 37, content scripts don't run on about:blank |
111 // So we have to apply element hiding and collapsing from | 132 // and about:srcdoc. So we have to apply element hiding and collapsing |
112 // the parent frame, when an anonymous sub frame loaded. | 133 // from the parent frame, when inline frames are loaded. |
113 if (chromeVersion < 37) | 134 if (chromeVersion < 37) |
114 fixAnonymousFrames = true; | 135 fixInlineFrames = true; |
115 } | 136 } |
116 | 137 |
117 // use Shadow DOM if available to don't mess with web pages that | 138 // use Shadow DOM if available to don't mess with web pages that |
118 // rely on the order of their own <style> tags (#309). However we | 139 // rely on the order of their own <style> tags (#309). However we |
119 // must not create the shadow root in the response callback passed | 140 // must not create the shadow root in the response callback passed |
120 // to sendMessage(), otherwise Chrome breaks some websites (#450). | 141 // to sendMessage(), otherwise Chrome breaks some websites (#450). |
121 if (canUseShadow) | 142 if (canUseShadow) |
122 { | 143 { |
123 var shadow = document.documentElement.webkitCreateShadowRoot(); | 144 var shadow = document.documentElement.webkitCreateShadowRoot(); |
124 shadow.appendChild(document.createElement("shadow")); | 145 shadow.appendChild(document.createElement("shadow")); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 setRules(); | 201 setRules(); |
181 }; | 202 }; |
182 | 203 |
183 document.addEventListener("error", function(event) | 204 document.addEventListener("error", function(event) |
184 { | 205 { |
185 checkCollapse(event.target); | 206 checkCollapse(event.target); |
186 }, true); | 207 }, true); |
187 | 208 |
188 document.addEventListener("load", function(event) | 209 document.addEventListener("load", function(event) |
189 { | 210 { |
190 if (/^i?frame$/.test(event.target.localName)) | 211 var element = event.target; |
191 { | 212 |
192 checkCollapse(event.target); | 213 if (/^i?frame$/.test(element.localName)) |
193 | 214 checkCollapse(element); |
194 if (fixAnonymousFrames && /^(javascript:|$)/.test(event.target.src)) | 215 |
Wladimir Palant
2014/06/02 11:20:40
I think you want to check event.target.contentWind
Sebastian Noack
2014/06/03 06:55:27
I thought it would be simpler to check for the src
Sebastian Noack
2014/06/03 07:40:35
It seems that checking for the document's URL inst
| |
195 { | 216 if (fixInlineFrames && isInlineFrame(element)) |
196 var contentDocument = event.target.contentDocument; | 217 { |
197 | 218 init(element.contentDocument); |
198 if (contentDocument.documentElement instanceof HTMLElement) | 219 |
199 { | 220 for (var tagName in typeMap) |
200 init(contentDocument); | 221 Array.prototype.forEach.call(element.contentDocument.getElementsByTagNam e(tagName), checkCollapse); |
201 | |
202 for (var tagName in typeMap) | |
203 Array.prototype.forEach.call(contentDocument.getElementsByTagName(ta gName), checkCollapse); | |
204 } | |
205 } | |
206 } | 222 } |
207 }, true); | 223 }, true); |
208 | 224 |
209 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); | 225 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); |
Wladimir Palant
2014/06/02 11:20:40
In the background page, who will be the sender of
Sebastian Noack
2014/06/03 06:55:27
Correct.
| |
210 } | 226 } |
211 | 227 |
212 if (document.documentElement instanceof HTMLElement) | 228 if (document.documentElement instanceof HTMLElement) |
213 { | 229 { |
214 checkExceptionKey(); | 230 checkExceptionKey(); |
215 init(document); | 231 init(document); |
216 } | 232 } |
LEFT | RIGHT |