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