Content script not loading for startup page

Hi all,

I am writing an addon that let a web page to interact with a hardware device using system executable files. In index.js I use the standard form:

var MySystemObj = {
    method1: function(p) {
        // do some system calls
        p.emit('method1Return', returnValue);
    },

    method2: function(port) {
        // do some system calls
        p.emit('method2Return', returnValue);
    }
};

var pageMod = require('sdk/page-mod');

pageMod.PageMod({
    include: '*.myhost.com',
    contentScriptFile: data.url('content-script.js'),
    contentScriptWhen: 'ready',
    onAttach: startListening
});

function startListening(worker) {
    worker.port.on('executeMethod1', function(d) {
        MySystemObj.method1(worker.port);
    });
    worker.port.on('executeMethod2', function(d) {
        MySystemObj.method2(worker.port);
    });
}

And in data/content-script.js I write the event listener to communicate with the web page:

var thePort = self.port;

var MyEventHandler = {
    handleEvent: function(e) {
        if (e.target.getAttribute('method') == '1')
            MyEventHandler.method1(e);
        else if (e.target.getAttribute('method') == '2')
            MyEventHandler.method2(e);
    },

    method1: function(e) {
        thePort.on('method1Return', function(v) {
            e.target.setAttribute('ret', v);
            var e2 = document.createEvent('Events');
            e2.initEvent('MyReturnEvent', true, false);
            e.target.dispatchEvent(e2);
        });
        thePort.emit('executeMethod1');
    },

    method2: function(e) {
        thePort.on('method2Return', function(v) {
            e.target.setAttribute('ret', v);
            var e2 = document.createEvent('Events');
            e2.initEvent('MyReturnEvent', true, false);
            e.target.dispatchEvent(e2);
        });
        thePort.emit('executeMethod2');
    }
};

var ev = document.createEvent('Events');
ev.initEvent('MyEvent', true, true);
document.addEventListener('MyEvent', MyEventHandler, false, true);

On the web page I invoke the methods “method1” and “method2” using standard custom events:

var element = document.getElementById('MyEventElement');
var e = document.createEvent('Events');
e.initEvent('MyEvent', true, false);
document.addEventListener('MyReturnEvent', handleReturnEvent, false, true);
element.dispatchEvent(e);

function handleReturnEvent(e) {
    var returnValue = e.target.getAttribute('ret');
    alert(returnValue);
}

This addon runs ok both on Mozilla Firefox and on Mozilla Firefox Developer Edition, but when I put the page “myhost.com” as the startup page, in Mozilla Firefox the function “startListening” in the index.js add-on code some times is not called (in Mozilla Firefox Developer Edition is called always). If I put “myhost.com” a the startup page and the “startListening” method is not invoked, if I press Ctrl+F5 or open another tab with the same URL, the “startListening” function is invoked and the plugin runs ok.

I am using Mozilla Firefox 49 on Windows 7 (but the problem also appears on other windows versions).

¿Any suggestion?

Thank you in advance!

You have to attach to existing pages in order to get attached to the startup pages, as the tab may be loaded before your extension. See the documentation of the attachTo constructor option (you’ll have to click on the text for it to display, the MDN stylesheet seems a bit non-obvious at the moment) https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/page-mod#PageMod(options)