ritcoder
ritcoder

Reputation: 3304

Firefox Addon: Add some functions and object to the window object

I just started using the FireFox Builder to build a simple addon. I realised that I cannot get direct access to the window object.

What I want to do is to get the window object and pollute it with some classes and functions so I can call them from the page itself.

Below is the current code:

// This is an active module of the ritcoder Add-on
require("widget").Widget({
    id: "widgetID1",
    label: "My Mozilla Widget",
    contentURL: "http://www.mozilla.org/favicon.ico",
    onClick: function(evt){
        var tabs = require("tabs");
        var activeTab = tabs.activeTab;

        var notifications = require("notifications");
        notifications.notify({
          title: "Jabberwocky",
          text: "'Twas brillig, and the slithy toves",
          data: "did gyre and gimble in the wabe",
          onClick: function (data) {
            console.log(data);
            // console.log(this.data) would produce the same result.
          }
        });

        activeTab.window.a=20; //this fails
        context.alert('yesx');
    }
});

How do I do this? Inject some code into the active page so that it can be called.

regards,

Upvotes: 2

Views: 451

Answers (1)

Nickolay
Nickolay

Reputation: 32073

You need to use tab.attach() to run a content script in the tab's context, and then use unsafeWindow to add properties the page's script can see. (You should also read the introduction to Content Scripts.)

The Addon SDK doesn't provide a direct access (without the content script) to the page from the add-on's code because it tries to be forward compatible with the plans to make web pages run in processes separate from the browser's and the add-on's process.

Upvotes: 2

Related Questions