content scripts that want to hijack Javascript functions in the main browser page

929 views
Skip to first unread message

PeterFacey

unread,
Feb 4, 2010, 4:34:25 PM2/4/10
to Chromium-extensions
Some greasemonkey scripts achieve their aim by "hijacking" (I'm not
sure if this is the correct term) a pre-existing Javascript function
within the main browser page. For example, the script says

original_func = window.their_func;
window.their_func = my_func;

function my_func() {
// do something before
original_func();
//do something after
}

This technique is dangerous but has the advantage that one can
guarantee that the "do somethings" occur immediately before and/or
after the original function is executed.

I gather this technique is deliberately prevented in Chrome. I worked
around this when converting my greasemonkey script for Chrome by
noticing that their_func() was actually called mainly as a result of
onevent mousemove. So, by doing addEventListener('mousemove',my_func)
I at least secured that my_func was executed almost whenever
their_func was.

But there is a problem with this workaround: there is no guarantee of
the order in which their_func and my_func are executed. As my_func
wants to manipulate the results of their_func this is a problem.

Can the content script modify the Javascript in the main broswer
window? In that case, I could append to their_func a
document.createEvent() which the content script could listen for.

Alternatively, is there a recommended general solution for this need?

Mark Wubben

unread,
Feb 4, 2010, 6:34:31 PM2/4/10
to PeterFacey, Chromium-extensions
You can write `<script>` tags into the main document, containing your own JavaScript code.

I tend to do something like this:

var hijack = function(){
var original_func = window.their_func;
window.their_func = function(){


// do something before
original_func();
// do something after

};
};

var script = document.createElement("script").
script.textContent = hijack.toString();
document.body.appendChild(script).


I write the hijack method as an actual function, and then use toString() to get its source, which is then injected into the main document.

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

--
Mark Wubben

http://supercollider.dk

PeterFacey

unread,
Feb 5, 2010, 4:27:31 AM2/5/10
to Chromium-extensions
Thanks. That's neat. So then hijack() is going to execute within the
environment of the main page and, although it still cannot call a
function defined in the content script, the "do something after" could
raise an event that the content script is listening for.

But I wonder how hijack() gets executed. Can one control exactly where
it is injected? Or should one get ones content script executed before
the main page has rendered?

Mark Wubben

unread,
Feb 5, 2010, 8:47:04 AM2/5/10
to PeterFacey, Chromium-extensions
On Feb 5, 2010, at 10:27 , PeterFacey wrote:
> But I wonder how hijack() gets executed. Can one control exactly where
> it is injected? Or should one get ones content script executed before
> the main page has rendered?

Actually the code sample should have ended with the following to make sure it executed straight away:

var script = document.createElement("script");
script.textContent = "(" + hijack.toString() + ")()";
document.body.appendChild(script);

You can use Chrome's content script run_at config to control when it gets inserted, or hook up your own DOM events to detect DOM Content loaded or page load. In any case the function you want to hijack needs to be there. I've had success using DOMContentLoaded.

Ron

unread,
Mar 29, 2010, 4:26:51 PM3/29/10
to Chromium-extensions
Hi, I was reading this post because I am interested in doing something
similar. After reading the Chrome Extension Dev Guide, I see that I
can inject script using the content scripts, but it was isolated from
the main browser page JS objects and code. But, from what I am seeing
here, are you saying that it is possible to inject your own script
into the browser page and access its JS objects, functions, etc.?

If so, am I still able to have contact my parent extension and
exchange messages, etc. with it?

fyi, this is for a legit extension that adds content to existing web
pages, but sometimes requires interaction with the page JS layer.

Thanks,
Ron

On Feb 5, 8:47 am, Mark Wubben <m...@novemberborn.net> wrote:
> On Feb 5, 2010, at 10:27 , PeterFacey wrote:
>
> > But I wonder how hijack() gets executed. Can one control exactly where

> > it is injected? Or should one get onescontentscriptexecuted before


> > the main page has rendered?
>
> Actually the code sample should have ended with the following to make sure it executed straight away:
>

>     varscript= document.createElement("script");


>    script.textContent = "(" + hijack.toString() + ")()";
>     document.body.appendChild(script);
>

> You can use Chrome'scontentscriptrun_at config to control when it gets inserted, or hook up your own DOM events to detect DOMContentloaded or page load. In any case the function you want to hijack needs to be there. I've had success using DOMContentLoaded.

Reply all
Reply to author
Forward
0 new messages