I'm trying to develop extension that works only on specified pages - If page owner adds global variable into their code (for eg. ACCEPT_STATS = true;
) I want to execute specified code.
I've already bind my function to the onload event, i've also found solution how to do that in Firefox:
var win = window.top.getBrowser().selectedBrowser.contentWindow;
if (typeof win.wrappedJSObject.ACCEPT_STATS !== 'undefined') {
// code to run if global variable present
}
but I couldn't make this work under Chrome. Is there any possibility to access document's global variable throw Chrome Extension code?
My extension's code is injected as a content-script.
I'm trying to develop extension that works only on specified pages - If page owner adds global variable into their code (for eg. ACCEPT_STATS = true;
) I want to execute specified code.
I've already bind my function to the onload event, i've also found solution how to do that in Firefox:
var win = window.top.getBrowser().selectedBrowser.contentWindow;
if (typeof win.wrappedJSObject.ACCEPT_STATS !== 'undefined') {
// code to run if global variable present
}
but I couldn't make this work under Chrome. Is there any possibility to access document's global variable throw Chrome Extension code?
My extension's code is injected as a content-script.
Share Improve this question edited Feb 12, 2014 at 15:14 Tomasz Banasiak asked Feb 12, 2014 at 15:06 Tomasz BanasiakTomasz Banasiak 1,5602 gold badges13 silver badges19 bronze badges 6- It sounds like something a content script should do , no ? developer.chrome.com/extensions/content_scripts.html – Joel Blum Commented Feb 12, 2014 at 15:11
- "However, content scripts have some limitations. They cannot: [...] Use variables or functions defined by web pages or by other content scripts" – kmoe Commented Feb 12, 2014 at 15:12
- Content scripts are executed in an isolated world. You would have to inject the code onto the page. – Ross Joo Commented Feb 12, 2014 at 15:13
- Yep, thats the problem. I search for websites with provided global JS variable and want to test if specified page contains it or not. – Tomasz Banasiak Commented Feb 12, 2014 at 15:13
- Well but it's possible to communicate between content script and webpage with window.postMessage , as long as the webpage is listening. but without listening I don't think there's a chance since it violates security policies – Joel Blum Commented Feb 12, 2014 at 15:14
2 Answers
Reset to default 17Yes, including script into the page does run in an isolated context from the pages runtime script.
However, it is possible to work around the isolated worlds issue by pushing inline script into the runtime context via a script tag appended to the document's html. That inline script can then throw a custom event.
The included script in the isolated context can listen for that event and respond to it accordingly.
So code in your included script would look something like this:
// inject code into "the other side" to talk back to this side;
var scr = document.createElement('script');
//appending text to a function to convert it's src to string only works in Chrome
scr.textContent = '(' + function () {
var check = [do your custom code here];
var event = document.createEvent("CustomEvent");
event.initCustomEvent("MyCustomEvent", true, true, {"passback":check});
window.dispatchEvent(event); } + ')();'
//cram that sucker in
(document.head || document.documentElement).appendChild(scr);
//and then hide the evidence as much as possible.
scr.parentNode.removeChild(scr);
//now listen for the message
window.addEventListener("MyCustomEvent", function (e) {
var check = e.detail.passback;
// [do what you need to here].
});
The javascript running on the page is running in a different "isolated world" than the javascript that you inject using content scripts. Google Chrome keeps these two worlds separate for security reasons and therefore you can't just read window.XYZ on any window. More info on how isolated worlds work : http://www.youtube.com/watch?v=laLudeUmXHM
The correct way of implementing this is by communicating with the page is via window.postMessage API. Here're how I would go about it :
- Inject a content script into each tab
- Send a message to the tab via window.postMessage
- If the page understands this message, it responds correctly (again via window.postMessage)
- Content script executes the code that it needed to execute.
HTH