简体   繁体   中英

How to code firefox extension which run javascript code in the page's context like firebug does

I know that for safety reasons that this is not easy to achieve, however there would be a way to do so as firebug does...

Please help, would like to invoke some script in the page's context to achieve some effect...

Basically, I would like to achieve two functionality: 1. add jQuery to any web page automatically if not already exist. 2. when open certain address, call a method of that page to auto notify the server. (an ajax functionality of the page)

I have tried to inject on the body, no luck. tried to get the window object, which however do not have access to call the function.

Will try to change the location to something like: javascript:alert('test inject');

Many thx.

OK, after reading some official documentation and the GreaseMonkey's source, I get the following method which basically works for me.

Hope it will save sb's hour:

var appcontent = document.getElementById("appcontent");   // browser  
    if (appcontent) {
         appcontent.addEventListener("DOMContentLoaded", function (evnt) {
            var doc = evnt.originalTarget; 
            var win = doc.defaultView;
            var unsafeWin = win.wrappedJSObject;

            // vote.up is the function on the page's context
            // which is take from this site as example
            unsafeWin.vote.up(...);
         }, true);
    }
}

Greasemonkey does that. If you are developing your own extension with similar functionality, you can use Components.utils.evalInSandbox .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM