Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: include.preload.js

Issue 5666188336037888: Issue 581 - Fixed element hiding/collapsing in anonymous frames on Chrome (Closed)
Left Patch Set: Created May 30, 2014, 11:32 a.m.
Right Patch Set: Addressed comments Created June 17, 2014, 2:12 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « background.js ('k') | lib/basedomain.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 var SELECTOR_GROUP_SIZE = 20; 18 var SELECTOR_GROUP_SIZE = 20;
19 19
20 // Converts relative to absolute URL
21 // e.g.: foo.swf on http://example.com/whatever/bar.html
22 // -> http://example.com/whatever/foo.swf
23 function relativeToAbsoluteUrl(url)
24 {
25 // If URL is already absolute, don't mess with it
26 if (!url || /^[\w\-]+:/i.test(url))
27 return url;
28
29 // Leading / means absolute path
30 // Leading // means network path
31 if (url[0] == '/')
32 {
33 if (url[1] == '/')
34 return document.location.protocol + url;
35 else
36 return document.location.protocol + "//" + document.location.host + url;
37 }
38
39 // Remove filename and add relative URL to it
40 var base = document.baseURI.match(/.+\//);
41 if (!base)
42 return document.baseURI + "/" + url;
43 return base[0] + url;
44 }
45
46 var typeMap = { 20 var typeMap = {
47 "img": "IMAGE", 21 "img": "IMAGE",
48 "input": "IMAGE", 22 "input": "IMAGE",
49 "audio": "MEDIA", 23 "audio": "MEDIA",
50 "video": "MEDIA", 24 "video": "MEDIA",
51 "frame": "SUBDOCUMENT", 25 "frame": "SUBDOCUMENT",
52 "iframe": "SUBDOCUMENT" 26 "iframe": "SUBDOCUMENT"
53 }; 27 };
54 28
55 function checkCollapse(element) 29 function checkCollapse(element)
Wladimir Palant 2014/05/30 11:45:17 Please move this function and the typeMap declarat
Sebastian Noack 2014/05/30 12:31:11 Done.
56 { 30 {
57 var tag = element.localName; 31 var tag = element.localName;
58 if (tag in typeMap) 32 if (tag in typeMap)
59 { 33 {
60 // This element failed loading, did we block it? 34 // This element failed loading, did we block it?
61 var url = element.src; 35 var url = element.src;
62 if (!url) 36 if (!url)
63 return; 37 return;
64 38
65 ext.backgroundPage.sendMessage( 39 ext.backgroundPage.sendMessage(
(...skipping 11 matching lines...) Expand all
77 if (tag == "frame") 51 if (tag == "frame")
78 element.style.setProperty("visibility", "hidden", "important"); 52 element.style.setProperty("visibility", "hidden", "important");
79 else 53 else
80 element.style.setProperty("display", "none", "important"); 54 element.style.setProperty("display", "none", "important");
81 } 55 }
82 } 56 }
83 ); 57 );
84 } 58 }
85 } 59 }
86 60
87 function checkExcpetionKey() 61 function checkExceptionKey()
Wladimir Palant 2014/05/30 11:45:17 Excpetion => Exception
Sebastian Noack 2014/05/30 12:31:11 Done.
88 { 62 {
89 var attr = document.documentElement.getAttribute("data-adblockkey"); 63 var attr = document.documentElement.getAttribute("data-adblockkey");
90 if (attr) 64 if (attr)
91 ext.backgroundPage.sendMessage({type: "add-key-exception", token: attr}); 65 ext.backgroundPage.sendMessage({type: "add-key-exception", token: attr});
92 } 66 }
93 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
89 // Converts relative to absolute URL
90 // e.g.: foo.swf on http://example.com/whatever/bar.html
91 // -> http://example.com/whatever/foo.swf
92 function relativeToAbsoluteUrl(url)
93 {
94 // If URL is already absolute, don't mess with it
95 if (!url || /^[\w\-]+:/i.test(url))
96 return url;
97
98 // Leading / means absolute path
99 // Leading // means network path
100 if (url[0] == '/')
101 {
102 if (url[1] == '/')
103 return document.location.protocol + url;
104 else
105 return document.location.protocol + "//" + document.location.host + url;
106 }
107
108 // Remove filename and add relative URL to it
109 var base = document.baseURI.match(/.+\//);
110 if (!base)
111 return document.baseURI + "/" + url;
112 return base[0] + url;
113 }
114
94 function init(document) 115 function init(document)
95 { 116 {
117 var canUseShadow = "webkitCreateShadowRoot" in document.documentElement;
118 var fixInlineFrames = false;
119
120 var match = navigator.userAgent.match(/\bChrome\/(\d+)/);
121 if (match)
122 {
123 var chromeVersion = parseInt(match[1]);
124
125 // the <shadow> element is ignored in Chrome 32 (#309). Also Chrome 31-33
126 // crashes in some situations on some pages when using shadow DOM (#498).
127 // So we must not use Shadow DOM on those versions of Chrome.
128 if (chromeVersion >= 31 && chromeVersion <= 33)
129 canUseShadow = false;
130
131 // prior to Chrome 37, content scripts don't run on about:blank
132 // and about:srcdoc. So we have to apply element hiding and collapsing
133 // from the parent frame, when inline frames are loaded.
134 if (chromeVersion < 37)
135 fixInlineFrames = true;
136 }
137
96 // 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
97 // rely on the order of their own <style> tags. However 139 // rely on the order of their own <style> tags (#309). However we
98 // the <shadow> element is broken in some Chrome 32 builds (#309) 140 // must not create the shadow root in the response callback passed
99 // 141 // to sendMessage(), otherwise Chrome breaks some websites (#450).
100 // also Chrome 31-33 crashes in some situations on some pages when using 142 if (canUseShadow)
101 // ShadowDOM, e.g. when pressing tab key on Wikipedia and Facebook (#498) 143 {
102 // 144 var shadow = document.documentElement.webkitCreateShadowRoot();
103 // also we must not create the shadow root in the response callback passed
104 // to sendMessage(), otherwise Chrome breaks some websites (#450)
105 var shadow = null;
106 if ("webkitCreateShadowRoot" in document.documentElement && !/\bChrome\/3[1-3] \b/.test(navigator.userAgent))
107 {
108 shadow = document.documentElement.webkitCreateShadowRoot();
109 shadow.appendChild(document.createElement("shadow")); 145 shadow.appendChild(document.createElement("shadow"));
110 } 146 }
111 147
112 // Sets the currently used CSS rules for elemhide filters 148 // Sets the currently used CSS rules for elemhide filters
113 var setElemhideCSSRules = function(selectors) 149 var setElemhideCSSRules = function(selectors)
114 { 150 {
115 if (selectors.length == 0) 151 if (selectors.length == 0)
116 return; 152 return;
117 153
118 var style = document.createElement("style"); 154 var style = document.createElement("style");
119 style.setAttribute("type", "text/css"); 155 style.setAttribute("type", "text/css");
120 156
121 if (shadow) 157 if (canUseShadow)
122 { 158 {
123 shadow.appendChild(style); 159 shadow.appendChild(style);
124 160
125 try 161 try
126 { 162 {
127 document.querySelector("::content"); 163 document.querySelector("::content");
128 164
129 for (var i = 0; i < selectors.length; i++) 165 for (var i = 0; i < selectors.length; i++)
130 selectors[i] = "::content " + selectors[i]; 166 selectors[i] = "::content " + selectors[i];
131 } 167 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 setRules(); 201 setRules();
166 }; 202 };
167 203
168 document.addEventListener("error", function(event) 204 document.addEventListener("error", function(event)
169 { 205 {
170 checkCollapse(event.target); 206 checkCollapse(event.target);
171 }, true); 207 }, true);
172 208
173 document.addEventListener("load", function(event) 209 document.addEventListener("load", function(event)
174 { 210 {
175 if (/^i?frame$/.test(event.target.localName)) 211 var element = event.target;
176 { 212
177 checkCollapse(event.target); 213 if (/^i?frame$/.test(element.localName))
178 214 checkCollapse(element);
179 // Chrome doesn't run our content script on anonymous frames. 215
180 // So we have to apply element hiding and collapsing from the 216 if (fixInlineFrames && isInlineFrame(element))
181 // parent frame, when an anonymous sub frame loaded. 217 {
182 if (/^(javascript:|$)/.test(event.target.src) && /\bChrome\//.test(navigat or.userAgent)) 218 init(element.contentDocument);
183 { 219
184 var contentDocument = event.target.contentDocument; 220 for (var tagName in typeMap)
185 221 Array.prototype.forEach.call(element.contentDocument.getElementsByTagNam e(tagName), checkCollapse);
186 if (contentDocument.documentElement instanceof HTMLElement)
187 {
188 init(contentDocument);
189
190 for (tagName in typeMap)
191 Array.prototype.forEach.call(contentDocument.getElementsByTagName(ta gName), checkCollapse);
192 }
193 }
194 } 222 }
195 }, true); 223 }, true);
196 224
197 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); 225 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules);
198 } 226 }
199 227
200 if (document.documentElement instanceof HTMLElement) 228 if (document.documentElement instanceof HTMLElement)
Wladimir Palant 2014/05/30 11:45:17 Why did you remove the failsafe? It was added back
Sebastian Noack 2014/05/30 12:31:11 I tested in Chrome 34 and Chrome 30, and it seems
201 { 229 {
202 checkExcpetionKey(); 230 checkExceptionKey();
203 init(document); 231 init(document);
Wladimir Palant 2014/05/30 11:45:17 Why pass in the document which is a global variabl
Sebastian Noack 2014/05/30 12:31:11 It is not always called with the global document.
204 } 232 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld