1
votes

I began to develop addon for firefox and I had a problem.

var tabs = require('sdk/tabs');

tabs.on('ready', function (tab) {
     tab.attach({
        contentScript: "alert('azaza');",
        onMessage: function(message) {
            console.log("message");
        }
      }); 
})

When I try to execute this code in Firefox nightly 36 it says "TypeError: window is null", but in Nightly 32 it works fine! In last fierfox (not nightly) this code not working too.

I tried to execute this code in nightly's browser debugger console, but the same result (window is null).

I can see, that in sdk/tabs/utils.js browser.contentWindow is null. I think this is my window object, but why it is null?

1
You're better off using a PageMod if you want to affect every page. - willlma
I'm confused - are you running this code in an add-on or in the browser toolbox tool? It will not work correctly in the browser toolbox tool. Works for me in Nightly 36 though. - therealjeffg
This is part of my addon's code. In browser toolbox tool I can see that here throwing exception. - alborozd

1 Answers

0
votes

I was able to reproduce this issue with the following code:

var { ActionButton } = require("sdk/ui/button/action");
var self = require("sdk/self");
var tabs = require('sdk/tabs');

var button = ActionButton({
    icon: self.data.url("icon-16.png"),
    id: "my-button",
    label: "my button",
    onClick: function() {
        tabs.open({
            url: self.data.url("text-entry.html")
        });
        tabs.activeTab.attach({
            contentScript: "alert('azaza');"
        });
    }
});

To fix this issue I had to use onOpen instead of using activeTab:

var button = ActionButton({
    icon: self.data.url("icon-16.png"),
    id: "my-button",
    label: "my button",
    onClick: function() {
        tabs.open({
            url: self.data.url("text-entry.html"),
            onOpen: function() {
                tabs.activeTab.attach({
                    contentScript: "alert('azaza');"
                });
            }
        });
    }
});

Perhaps are you using the attach method when you cannot use it?