admin管理员组

文章数量:1277875

I am developing a Firefox addon. What I want to do is to inject a custom JavaScript function.

i.e.

function foo() {..}

So all the pages can call the foo without define it first.

I have look from other answer such as:

But it requires modification on the web page. What if perhaps I want to inject the function foo into Google? Is it possible to do so?

I can do it with a userscript, but I want to use the extension approach if possible.

I am developing a Firefox addon. What I want to do is to inject a custom JavaScript function.

i.e.

function foo() {..}

So all the pages can call the foo without define it first.

I have look from other answer such as: http://groups.google./group/greasemonkey-users/browse_thread/thread/3d82a2e7322c3fce

But it requires modification on the web page. What if perhaps I want to inject the function foo into Google.? Is it possible to do so?

I can do it with a userscript, but I want to use the extension approach if possible.

Share Improve this question edited Apr 30, 2013 at 22:09 developer 3081 gold badge3 silver badges15 bronze badges asked Jan 15, 2012 at 18:21 HowardHoward 19.8k36 gold badges115 silver badges187 bronze badges 2
  • I haven't build a firefox addon before, but I would assume, if you have access to the window object, that you could add a script to the head of the document and then make calls to it. I'm not sure that the script would be available for their scripts to call your script, but it's worth a shot. You may have to create a flag that is set in your code that would be a sudo document ready call. – Seth Commented Jan 15, 2012 at 18:24
  • @Rook, greasemonkey works, but as I want to create a extension for better deployment to large number of machines. What I think is since gm is able to do this, I should able to hook up some API to do the same. – Howard Commented Jan 16, 2012 at 16:51
Add a ment  | 

3 Answers 3

Reset to default 5

The first thing I thought when reading your question was "this looks like a scam". What are you trying to achieve?

Anyway, here's a Jetpack (Add-on builder) add-on that injects a script in every page loaded:

main.js:

const self = require("self"),
      page_mod = require("page-mod");

exports.main = function() {
    page_mod.PageMod({
        include: "*",
        contentScriptWhen: "ready",
        contentScriptFile: self.data.url("inject.js")
    });
};

inject.js:

unsafeWindow.foo = function() {
    alert('hi');
}

unsafeWindow.foo();

What if you make a simple href with javascript function on the page.
Like bookmarklets work.

Here is a sample code :

function(scriptUrl) {

    var newScript = document.createElement('script');

    // the Math.random() part is for avoiding the cache
    newScript.src = scriptUrl + '?dummy=' + Math.random();

    // append the new script to the dom
    document.body.appendChild(newScript);

    // execute your newly available function
    window.foo();

}('[url of your online script]')


To use it, put your script's url.
It must be only one line of code, url formated, but for code readability I've formated it.

I've never developed a Firefox extension, but for javascript injection that's how I would roll.

Hope it helped.

You can use Sandbox

// Define DOMContentLoaded event listener in the overlay.js
document.getElementById("appcontent").addEventListener("DOMContentLoaded", function(evt) {
    if (!evt.originalTarget instanceof HTMLDocument) {
        return;
    }

    var view = evt.originalTarget.defaultView;
    if (!view) {
        return;
    }

    var sandbox = new Components.utils.Sandbox(view);
    sandbox.unsafeWindow = view.window.wrappedJSObject;
    sandbox.window = view.window;
    sandbox.document = sandbox.window.document;
    sandbox.__proto__ = sandbox.window;

    // Eval your JS in the sandbox
    Components.utils.evalInSandbox("function foo() {..}", sandbox); 
}, false);

本文标签: How to inject a JavaScript function to all web page using Firefox extensionStack Overflow