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: Fixed accidental global variable Created May 30, 2014, 1:05 p.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
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 }
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 {
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 (var 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)
201 { 229 {
202 checkExceptionKey(); 230 checkExceptionKey();
203 init(document); 231 init(document);
204 } 232 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld