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

Side by Side Diff: include.preload.js

Issue 4883433876619264: Issue 2217 - Adapt for content scripts running in about:blank frames (Closed)
Patch Set: Created March 25, 2015, 3:15 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 // <input type="image"> elements try to load their image again 63 // <input type="image"> elements try to load their image again
64 // when the "display" CSS property is set. So we have to check 64 // when the "display" CSS property is set. So we have to check
65 // that it isn't already collapsed to avoid an infinite recursion. 65 // that it isn't already collapsed to avoid an infinite recursion.
66 if (element.style.getPropertyValue(property) != value || 66 if (element.style.getPropertyValue(property) != value ||
67 element.style.getPropertyPriority(property) != "important") 67 element.style.getPropertyPriority(property) != "important")
68 element.style.setProperty(property, value, "important"); 68 element.style.setProperty(property, value, "important");
69 } 69 }
70 } 70 }
71 ); 71 );
72 } 72 }
73
74 window.collapsing = true;
73 } 75 }
74 76
75 function checkSitekey() 77 function checkSitekey()
76 { 78 {
77 var attr = document.documentElement.getAttribute("data-adblockkey"); 79 var attr = document.documentElement.getAttribute("data-adblockkey");
78 if (attr) 80 if (attr)
79 ext.backgroundPage.sendMessage({type: "add-sitekey", token: attr}); 81 ext.backgroundPage.sendMessage({type: "add-sitekey", token: attr});
80 } 82 }
81 83
82 function isFrameWithoutContentScript(element) 84 function getContentDocument(element)
83 { 85 {
84 var contentDocument;
85 try 86 try
86 { 87 {
87 contentDocument = element.contentDocument; 88 return element.contentDocument;
88 } 89 }
89 catch (e) 90 catch (e)
90 { 91 {
91 // This is a third-party frame. Hence we can't access it. 92 return null;
92 // But that's fine, our content script should already run there.
93 return false;
94 } 93 }
95
96 // The element isn't a <frame>, <iframe> or <object> with "data" attribute.
97 if (!contentDocument)
98 return false;
99
100 // Return true, if the element is a first-party frame which doesn't
101 // have this function, hence our content script isn't running there.
102 // Those are dynamically created frames as well as frames
103 // with "about:blank", "about:srcdoc" and "javascript:" URL.
104 return !("isFrameWithoutContentScript" in contentDocument.defaultView);
105 } 94 }
106 95
107 function reinjectRulesWhenRemoved(document, style) 96 function reinjectRulesWhenRemoved(document, style)
108 { 97 {
109 var MutationObserver = window.MutationObserver || window.WebKitMutationObserve r; 98 var MutationObserver = window.MutationObserver || window.WebKitMutationObserve r;
110 if (!MutationObserver) 99 if (!MutationObserver)
111 return; 100 return;
112 101
113 var observer = new MutationObserver(function(mutations) 102 var observer = new MutationObserver(function(mutations)
114 { 103 {
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 checkCollapse(event.target); 253 checkCollapse(event.target);
265 }, true); 254 }, true);
266 255
267 document.addEventListener("load", function(event) 256 document.addEventListener("load", function(event)
268 { 257 {
269 var element = event.target; 258 var element = event.target;
270 259
271 if (/^i?frame$/.test(element.localName)) 260 if (/^i?frame$/.test(element.localName))
272 checkCollapse(element); 261 checkCollapse(element);
273 262
274 // prior to Chrome 37, content scripts cannot run on about:blank, 263 if (/\bChrome\//.test(navigator.userAgent))
275 // about:srcdoc and javascript: URLs. Moreover, as of Chrome 40
276 // "load" and "error" events aren't dispatched there. So we have
277 // to apply element hiding and collapsing from the parent frame.
278 if (/\bChrome\//.test(navigator.userAgent) && isFrameWithoutContentScript(el ement))
279 { 264 {
280 init(element.contentDocument); 265 var contentDocument = getContentDocument(element);
266 if (contentDocument)
267 {
268 var contentWindow = contentDocument.defaultView;
269 if (contentDocument instanceof contentWindow.HTMLDocument)
kzar 2015/03/26 12:07:57 Perhaps we should check that contentWindow isn't n
Sebastian Noack 2015/03/26 13:41:19 I think we can assume frames having a document, al
270 {
271 // Prior to Chrome 37, content scripts cannot run in
272 // dynamically created frames. Also on Chrome 37-40
273 // document_start content scripts (like this one) don't
274 // run either in those frames due to https://crbug.com/416907.
275 // So we have to apply element hiding from the parent frame.
276 if (!("init" in contentWindow))
277 init(contentDocument);
281 278
282 for (var tagName in typeMap) 279 // Moreover, "load" and "error" events aren't dispatched for elements
283 Array.prototype.forEach.call(element.contentDocument.getElementsByTagNam e(tagName), checkCollapse); 280 // in dynamically created frames due to https://crbug.com/442107.
281 // So we also have to apply element collpasing from the parent frame.
282 if (!contentWindow.collapsing)
283 [].forEach.call(contentDocument.querySelectorAll(Object.keys(typeMap ).join(",")), checkCollapse);
kzar 2015/03/26 12:07:57 Couldn't we put this forEach loop under the above
Sebastian Noack 2015/03/26 13:41:19 We are dealing two different issues here: 1. The
kzar 2015/03/26 14:14:58 OK, thanks for explaining.
284 }
285 }
284 } 286 }
285 }, true); 287 }, true);
286 288
287 return updateStylesheet; 289 return updateStylesheet;
288 } 290 }
289 291
290 if (document instanceof HTMLDocument) 292 if (document instanceof HTMLDocument)
291 { 293 {
292 checkSitekey(); 294 checkSitekey();
293 window.updateStylesheet = init(document); 295 window.updateStylesheet = init(document);
294 } 296 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld