开发者

How to inject the code into the page using mozrepl?

I'm trying to inject additional scripts to a page using mozrepl.

If I manually run these actions jQuery is successfully loaded and I can use it later

> content.location.url = "..."
> repl.enter(content)
> var s=document.createElement('script')
> s.src='http://code.jquery.com/jquery-1.6.1.min.js'
> document.body.appendChild(s)
> [...more actions using jQuery's $ follow...]

Now I'm trying to put all these actions into a function to load into mozrepl later. And it doesn't work. Looks like it has to do with switching contexts, but I开发者_Go百科'm not sure.

Can someone enlighten me, what's going wrong and how to fix it?


I haven't used mozrepl, but I expect the environment works similarly to other user agents. You have to allow time for the script to be downloaded and executed, you can't expect it to be in the DOM instantly.

The function will not stop executing while that happens, and there is no event to tell you when it occurs.


After some trials and reading XUL documentation I came up with this:

function on_page(url, func) {
    var browser;

    var on_page_load = function(aEvent) {
        var doc = aEvent.originalTarget;

        if (doc.location.href == content.location.href) {
            browser.removeEventListener("load", on_page_load, true);
            func.call(content);
        }
    };

    var goto_page = function() {
        browser = document.getElementById("appcontent"); // browser
        browser.addEventListener("load", on_page_load, true);
        content.location.href = url;
    };

    goto_page();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜