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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include.preload.js
===================================================================
--- a/include.preload.js
+++ b/include.preload.js
@@ -70,6 +70,8 @@
}
);
}
+
+ window.collapsing = true;
}
function checkSitekey()
@@ -79,29 +81,16 @@
ext.backgroundPage.sendMessage({type: "add-sitekey", token: attr});
}
-function isFrameWithoutContentScript(element)
+function getContentDocument(element)
{
- var contentDocument;
try
{
- contentDocument = element.contentDocument;
+ return element.contentDocument;
}
catch (e)
{
- // This is a third-party frame. Hence we can't access it.
- // But that's fine, our content script should already run there.
- return false;
+ return null;
}
-
- // The element isn't a <frame>, <iframe> or <object> with "data" attribute.
- if (!contentDocument)
- return false;
-
- // Return true, if the element is a first-party frame which doesn't
- // have this function, hence our content script isn't running there.
- // Those are dynamically created frames as well as frames
- // with "about:blank", "about:srcdoc" and "javascript:" URL.
- return !("isFrameWithoutContentScript" in contentDocument.defaultView);
}
function reinjectRulesWhenRemoved(document, style)
@@ -271,16 +260,29 @@
if (/^i?frame$/.test(element.localName))
checkCollapse(element);
- // prior to Chrome 37, content scripts cannot run on about:blank,
- // about:srcdoc and javascript: URLs. Moreover, as of Chrome 40
- // "load" and "error" events aren't dispatched there. So we have
- // to apply element hiding and collapsing from the parent frame.
- if (/\bChrome\//.test(navigator.userAgent) && isFrameWithoutContentScript(element))
+ if (/\bChrome\//.test(navigator.userAgent))
{
- init(element.contentDocument);
+ var contentDocument = getContentDocument(element);
+ if (contentDocument)
+ {
+ var contentWindow = contentDocument.defaultView;
+ 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
+ {
+ // Prior to Chrome 37, content scripts cannot run in
+ // dynamically created frames. Also on Chrome 37-40
+ // document_start content scripts (like this one) don't
+ // run either in those frames due to https://crbug.com/416907.
+ // So we have to apply element hiding from the parent frame.
+ if (!("init" in contentWindow))
+ init(contentDocument);
- for (var tagName in typeMap)
- Array.prototype.forEach.call(element.contentDocument.getElementsByTagName(tagName), checkCollapse);
+ // Moreover, "load" and "error" events aren't dispatched for elements
+ // in dynamically created frames due to https://crbug.com/442107.
+ // So we also have to apply element collpasing from the parent frame.
+ if (!contentWindow.collapsing)
+ [].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.
+ }
+ }
}
}, true);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld