Access runtime environment in a chrome extension

5,612 views
Skip to first unread message

Noelia Franco

unread,
Sep 2, 2011, 10:31:57 AM9/2/11
to Chromium-extensions
Hello, 
I'm trying to make an extension to monitor and debug jquery. To do
this I need to access the runtime environment of the page to retrieve
the actual state of the javascript variables.
I know that the content script lives in a different world than the
world of the web page, and that only the DOM is shared, not the
global variables. This is a limitation to continue developing my
extension.
I'd like to know if it's possible to do this from within an
extension. If not, is there any chance that it may be available in
the future?

Thanks in advance!

Regards

Noelia S. Franco

PhistucK

unread,
Sep 2, 2011, 2:46:31 PM9/2/11
to Noelia Franco, Chromium-extensions
You can inject, into the DOM, your code and it will operate within the context/world of the page.
var script = document.createElement("script");
script.src = chrome.extension.getURL("some-script.js");
document.documentElement.appendChild(script);

Note that you have to add the host of the page (in which you want to inject such a script) to the "permissions" key within the manifest.
Either that, or do this -
function someFunction()
{
 alert("");
}
var script = document.createElement("script");
script.textContent = "alert('')"; // Option 1 - inline.
script.textContent = "(" + someFunction.toString() + "())"; // Option 2 - toString of a function from this script.
script.textContent = requestedScript; // Option 3 - load the script using sendRequest (and a background page) or XMLHttpRequest and assign the string to requestedScript, though it sounds like an overkill.
document.documentElement.appendChild(script);

Note that you cannot call extension APIs from the context/world of the page, you will have to contact your content script world using custom events or similar ways.

PhistucK




--
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.


Noelia Franco

unread,
Sep 2, 2011, 2:57:51 PM9/2/11
to Chromium-extensions
Thanks PhistucK, but in this way I have access to the javascript
variables in the page?
I want to show these variables, for example, in a devtools panel.


On 2 sep, 15:46, PhistucK <phist...@gmail.com> wrote:
> You can inject, into the DOM, your code and it will operate within the
> context/world of the page.
> var script = document.createElement("script");
> script.src = chrome.extension.getURL("some-script.js");
> document.documentElement.appendChild(script);
>
> Note that you have to add the host of the page (in which you want to inject
> such a script) to the "permissions" key within the manifest.
> Either that, or do this -
> function someFunction()
> {
>  alert("");}
>
> var script = document.createElement("script");
> script.textContent = "alert('')"; // Option 1 - inline.
> script.textContent = "(" + someFunction.toString() + "())"; // Option 2 -
> toString of a function from this script.
> script.textContent = requestedScript; // Option 3 - load the script using
> sendRequest (and a background page) or XMLHttpRequest and assign the string
> to requestedScript, though it sounds like an overkill.
> document.documentElement.appendChild(script);
>
> Note that you cannot call extension APIs from the context/world of the page,
> you will have to contact your content script world using custom events or
> similar ways.
>
> ☆*PhistucK*
>
>
>
>
>
>
>
> On Fri, Sep 2, 2011 at 17:31, Noelia Franco <noeliasfra...@gmail.com> wrote:
> > Hello,
> >        I'm trying to make an extension to monitor and debug jquery. To do
> > this I need to access the runtime environment of the page to retrieve
> > the  actual state of the javascript variables.
> >        I know that the content script lives in a different world than the
> > world of the web page, and that only the DOM is shared, not the
> > global  variables. This is a limitation to continue developing my
> > extension.
> >        I'd like to know if it's possible to do this from within an
> > extension.  If not, is there any chance that it may be available in
> > the future?
>
> >        Thanks in advance!
>
> > Regards
>
> >          Noelia S. Franco
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org.

timo

unread,
Sep 2, 2011, 3:53:20 PM9/2/11
to Chromium-extensions
Hi Noelia,

unfortunately (or better fortunately) there is no way to get
variables, functions etc. from the existing page. You can set up yor
own JS as PhishtucK explained and use it to manipulate the DOM
including styles,... but existing scripts can't be touched... not
even,... nothing!

So, if I understud it right what you want to do, monitor JS from the
existing page... then it's 'normaly' absolutely impossible...

Well, not quite true! ;o) I guess.
There is Page Speed and Speed Tracer (by Google) extensions and also
the Chrome internal DOM inpector is written in JS. I think they use a
special/experimental API (Speed tracer has to be set up in a special
way to work, so they make an API work by launching chrome with some
extra command line flag and you need to use a the developer channel
chrome version...) to make things work.

So you might want to analyze those extensions to find out. If so, I'm
still not quite shure if you can use them inside an extension you
could publish...
It's just a guess ;o)

Good luck though
Cheers

Scott Fujan

unread,
Sep 2, 2011, 4:03:14 PM9/2/11
to timo, Chromium-extensions
Phistuck demonstrated how to access the page's variables. You can then communicate the variables back to the extension through DOM events.

--
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.

Noelia Franco

unread,
Sep 2, 2011, 4:47:52 PM9/2/11
to Chromium-extensions
Thanks Timo!. This is an excellent data.
I'll get this extension and try to see how it does.
Reply all
Reply to author
Forward
0 new messages