views:

51

answers:

2

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.

+1  A: 

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

Matthew Flaschen
Yes, thank you.I just wondering how to make an extension without dependent on GreaseMonkey...
ccppjava
+1  A: 

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);
    }
}
ccppjava