Work-arounds to set global var in page from content script?

6,129 views
Skip to first unread message

Matt Kruse

unread,
Feb 11, 2011, 1:54:29 PM2/11/11
to Chromium-extensions
First, I know that content scripts cannot access the page's global
scope.

I have a content script that loads on facebook.com, and I have a
background page that communicates with it for cross-domain XHR,
postMessage handling, etc. It works well.

I need my content script to set a variable in the global scope of the
containing page as soon as it runs. I've seen mention of this:

window.location="javascript:window.globalVar=val;";

and also inserting <script> tags. But both of those are asynchronous
operations (which I don't want) and seem like hacks. I'm fighting
timing issues. I need the global var set before the rest of my content
script runs!

Is there any other work-around? Can the background page itself set a
global variable every time a url is loaded? Without even coding it in
the content script?

Thanks!

Matt Kruse

Gildas

unread,
Feb 11, 2011, 2:11:56 PM2/11/11
to Chromium-extensions
Personally, I go further in the hack (sic) and I use custom events or
window.postMessage to achieve this.

For example, you can write:

window.location="javascript:window.globalVar=val;window.postMessage('myExtension::injectionDone',
'*')";

and add a listener in your content script:

addEventListener("message", function(event) {
if (event.data == "myExtension::injectionDone") {
// ...
}
});

Matt Kruse

unread,
Feb 13, 2011, 10:03:09 PM2/13/11
to Chromium-extensions


On Feb 11, 1:11 pm, Gildas <gildas.lorm...@gmail.com> wrote:
> Personally, I go further in the hack (sic) and I use custom events or
> window.postMessage to achieve this.
> For example, you can write:
> window.location="javascript:window.globalVar=val;window.postMessage('myExtension::injectionDone',
> '*')";

Unfortunately I've found that using this window.location hack causes
problems in some cases. Running that code can prevent other script
from running. Specifically, a redirect page on Facebook is prevented
from redirecting when a window.location="javascript:..." line is
present.

Is there another work-around?

Matt Kruse

Shadab Ahmed Ansari

unread,
Feb 14, 2011, 1:46:20 AM2/14/11
to Chromium-extensions

Adding chrome-extension in mail
---------- Forwarded message ----------
From: Shadab Ahmed Ansari <shadab...@gmail.com>
Date: Mon, Feb 14, 2011 at 12:15 PM
Subject: Re: [crx] Re: Work-arounds to set global var in page from content script?
To: Matt Kruse <matthew...@gmail.com>


This is what I have done:

1. Added an iframe with no src attribute and an id.

2. When I need to run some script in the page I set this as the iframe src in the context of parent window:
        iframe.setAttribute('src',"javascript: top.document.getElementById(window.name).dataset.return_value = \
                JSON.stringify(top.window.eval.call(top.window,'(function(){"+ script +";})()'))");

  This does not throw any cross domain errors.

3. This runs the function synchronously and I can read the return value by iframe.dataset.return_value

Good luck,
Shadab



Matt Kruse

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.



Matt Kruse

unread,
Feb 16, 2011, 11:08:36 AM2/16/11
to Chromium-extensions
On Feb 14, 12:46 am, Shadab Ahmed Ansari <shadab.ans...@gmail.com>
wrote:
> 2. When I need to run some script in the page I set this as the iframe src
> in the context of parent window:
>         iframe.setAttribute('src',"javascript: top.document.getElementById(
> window.name).dataset.return_value = \
>  JSON.stringify(top.window.eval.call(top.window,'(function(){"+ script
> +";})()'))");

This worked perfectly, thank you! I stripped out the returning of
values, but it works correctly to simply set a global var.

Thanks again!

Matt Kruse

ShadyKiller

unread,
Feb 16, 2011, 11:18:58 AM2/16/11
to Chromium-extensions
You're welcome :)
Reply all
Reply to author
Forward
0 new messages