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: Rely on document's URL instead frame's src attribute Created June 2, 2014, 10:28 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 getContentDocumentIfAboutBlank(frame) 68 function hasInlineURL(element, attribute)
69 { 69 {
70 try 70 var value = element.getAttribute(attribute);
71 { 71 return value == null || /^\s*(javascript:|about:|$)/i.test(value);
72 var contentDocument = frame.contentDocument; 72 }
73 } 73
74 catch (e) 74 function isInlineFrame(element)
75 { 75 {
76 return null; 76 switch (element.localName)
77 } 77 {
78 78 case "iframe":
79 if (contentDocument.location.href == 'about:blank') 79 return hasInlineURL(element, "src") || element.hasAttribute("srcdoc");
80 return contentDocument; 80 case "frame":
81 if (contentDocument.location.href == 'about:srcdoc') 81 return hasInlineURL(element, "src");
82 return contentDocument; 82 case "object":
83 83 return hasInlineURL(element, "data") && element.contentDocument;
84 return null; 84 default:
85 return false;
86 }
85 } 87 }
86 88
87 // Converts relative to absolute URL 89 // Converts relative to absolute URL
88 // e.g.: foo.swf on http://example.com/whatever/bar.html 90 // e.g.: foo.swf on http://example.com/whatever/bar.html
89 // -> http://example.com/whatever/foo.swf 91 // -> http://example.com/whatever/foo.swf
90 function relativeToAbsoluteUrl(url) 92 function relativeToAbsoluteUrl(url)
91 { 93 {
92 // If URL is already absolute, don't mess with it 94 // If URL is already absolute, don't mess with it
93 if (!url || /^[\w\-]+:/i.test(url)) 95 if (!url || /^[\w\-]+:/i.test(url))
94 return url; 96 return url;
(...skipping 11 matching lines...) Expand all
106 // Remove filename and add relative URL to it 108 // Remove filename and add relative URL to it
107 var base = document.baseURI.match(/.+\//); 109 var base = document.baseURI.match(/.+\//);
108 if (!base) 110 if (!base)
109 return document.baseURI + "/" + url; 111 return document.baseURI + "/" + url;
110 return base[0] + url; 112 return base[0] + url;
111 } 113 }
112 114
113 function init(document) 115 function init(document)
114 { 116 {
115 var canUseShadow = "webkitCreateShadowRoot" in document.documentElement; 117 var canUseShadow = "webkitCreateShadowRoot" in document.documentElement;
116 var runsContentScriptsOnAboutBlank = true; 118 var fixInlineFrames = false;
117 119
118 var match = navigator.userAgent.match(/\bChrome\/(\d+)/); 120 var match = navigator.userAgent.match(/\bChrome\/(\d+)/);
119 if (match) 121 if (match)
120 { 122 {
121 var chromeVersion = parseInt(match[1]); 123 var chromeVersion = parseInt(match[1]);
122 124
123 // 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
124 // 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).
125 // 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.
126 if (chromeVersion >= 31 && chromeVersion <= 33) 128 if (chromeVersion >= 31 && chromeVersion <= 33)
127 canUseShadow = false; 129 canUseShadow = false;
128 130
129 // prior to Chrome 37, content scripts don't run on about:blank 131 // prior to Chrome 37, content scripts don't run on about:blank
130 // and about:srcdoc. So we have to apply element hiding and collapsing 132 // and about:srcdoc. So we have to apply element hiding and collapsing
131 // from the parent frame, when in-line frames are loaded. 133 // from the parent frame, when inline frames are loaded.
132 if (chromeVersion < 37) 134 if (chromeVersion < 37)
133 runsContentScriptsOnAboutBlank = false; 135 fixInlineFrames = true;
134 } 136 }
135 137
136 // 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
137 // 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
138 // must not create the shadow root in the response callback passed 140 // must not create the shadow root in the response callback passed
139 // to sendMessage(), otherwise Chrome breaks some websites (#450). 141 // to sendMessage(), otherwise Chrome breaks some websites (#450).
140 if (canUseShadow) 142 if (canUseShadow)
141 { 143 {
142 var shadow = document.documentElement.webkitCreateShadowRoot(); 144 var shadow = document.documentElement.webkitCreateShadowRoot();
143 shadow.appendChild(document.createElement("shadow")); 145 shadow.appendChild(document.createElement("shadow"));
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 setRules(); 201 setRules();
200 }; 202 };
201 203
202 document.addEventListener("error", function(event) 204 document.addEventListener("error", function(event)
203 { 205 {
204 checkCollapse(event.target); 206 checkCollapse(event.target);
205 }, true); 207 }, true);
206 208
207 document.addEventListener("load", function(event) 209 document.addEventListener("load", function(event)
208 { 210 {
209 if (/^i?frame$/.test(event.target.localName)) 211 var element = event.target;
210 { 212
211 checkCollapse(event.target); 213 if (/^i?frame$/.test(element.localName))
212 214 checkCollapse(element);
213 if (!runsContentScriptsOnAboutBlank) 215
214 { 216 if (fixInlineFrames && isInlineFrame(element))
215 var contentDocument = getContentDocumentIfAboutBlank(event.target); 217 {
216 if (contentDocument) 218 init(element.contentDocument);
217 { 219
218 init(contentDocument); 220 for (var tagName in typeMap)
219 221 Array.prototype.forEach.call(element.contentDocument.getElementsByTagNam e(tagName), checkCollapse);
220 for (var tagName in typeMap)
221 Array.prototype.forEach.call(contentDocument.getElementsByTagName(ta gName), checkCollapse);
222 }
223 }
224 } 222 }
225 }, true); 223 }, true);
226 224
227 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules); 225 ext.backgroundPage.sendMessage({type: "get-selectors"}, setElemhideCSSRules);
228 } 226 }
229 227
230 if (document.documentElement instanceof HTMLElement) 228 if (document.documentElement instanceof HTMLElement)
231 { 229 {
232 checkExceptionKey(); 230 checkExceptionKey();
233 init(document); 231 init(document);
234 } 232 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld