Search code examples
javascripttabsfirefox-addonfirefox-addon-sdkdom-events

Call a page-specific function on an opened page


In a Firefox extension I am currently developing using the addon builder, I open a page in a new tab and I would like to call a JS function defined in a script on this page. For this I use this code:

var toOpenTab = require("tabs");
toOpenTab.on('open', function(tab){
    toOpenTab.on('ready', function(tab){
        tab.attach({
            contentScript:
                "function showFile(){PageSpecificFunction()};window.onload=showFile();"
        });

    });

});

I implement the window.onload event to be sure that the script containing the PageSpecificFunction() definition is loaded in the page, even though I don't think it is necessary (because I use the toOpenTab.onReady event).

The problem is: PageSpecificFunction() is not defined. I know the function is correctly defined and works fine because I can call it in the firebug console and it works perfectly.

Is there a way to make my extension call this function once my page is opened ?


Solution

  • You need to use the global unsafeWindow object:

    unsafeWindow.PageSpecificFunction();

    However this is a security risk