Search code examples
javascriptfirefoxfirefox-addonfirefox-addon-sdk

Why onAttach in page-mod module is not getting executed in Firefox add-on sdk?


I am writing a Firefox add-on which uses context-menu and page-mod modules.

This add-on adds a new menu item to the context menu when the context is input control. When the menu item is clicked, I request to the server to get some data and pushes the result to the content script which page-mod is using.

For some reason, onAttach function is not getting called. There are no errors in the console. I am not sure why it is not getting called. Here is what I am doing.

var data = require("self").data,
    contextMenu = require("context-menu"),
    request = require("request").Request;

var workers = [];
var pageMod = require("page-mod");
pageMod.PageMod({
    include: '*',
    contentScriptWhen: 'ready',
    contentScriptFile: [data.url("jquery-1.8.2.min.js"), data.url("varnam.js")],
    onAttach: function(worker) {
        console.log("onAttach");
        workers.push(worker);
        worker.on("detach", function() {
            var index = workers.indexOf(worker);
            if (index >= 0) workers.splice(index, 1);
        });
    }
});

Any help to fix this issue would be great. Full code is available here.


Solution

  • When the url pattern begins with (or is just) an asterisk, then it is matched against urls with a scheme of http, https and ftp.

    Since your test page is in the data directory its url scheme is resource. And that's why PageMod is not triggered.

    You can add the test page url by doing something like

    var varnam = pageMod.PageMod({ 
    // blah blah
    });
    
    varnam.include.add(data.url("test.html"));