Search code examples
javascriptfirefoxfirefox-addonfirefox-addon-sdk

firefox bootstrap addon: install event is not executed


I'm trying to create a bootstrapped addon that just sets the new tab url at install to a new value and resets it to the old one when it gets uninstalled.

Here is my bootstrap.js. I think the install function throws an exception because require is not defined, but I'm not sure if the debugger executes the code I write in Scratchpad in the right scope.

I read somewhere that the api is the same for bootstrapped extensions as the with the add-on sdk, so the require should be fine. If this is not the case, could you please direct me to a page that describes the code I can use in the bootstrap.js, I didn't find anything :(

function startup(data, reason){

}

function shutdown(data, reason){

}

function install(data, reason){
    var prev_new_tab_url = require("sdk/preferences/service").get("browser.newtab.url");
    var data = require("sdk/self").data;
    var url = data.url("startpage.html");
    require("sdk/preferences/service").set("browser.newtab.url", url);
    var ss = require("sdk/simple-storage");
    ss.storage.prev_new_tab_url = prev_new_tab_url;
}

function uninstall(data, reason){
    var ss = require("sdk/simple-storage");
    var prev_new_tab_url = ss.storage.prev_new_tab_url;
    require("sdk/preferences/service").set("browser.newtab.url", prev_new_tab_url);
}

Solution

  • from: https://forums.mozilla.org/viewtopic.php?f=7&t=22621&sid=4ea13ebd794f85600d6dcbcf6cc590a7

    in bootstrap you dont have access to sdk stuff like that. im not sure how to access that stuff.

    but i made exactly what you are looking for with localization :D took like 10min :D

    https://github.com/NoitForks/l10n/tree/setpref-install-uninstall

    note: the quirk that localization files are not available during the uninstall procedure. so i had to move this to shutodwn proc while testing for aReason of ADDON_DISABLE. it makes sense that files are not available in uninstall


    you asked:

    How do you know the Services.prefs.getCharPref method?


    i responded:

    I first imported the Services.jsm module then i looked on MDN for what all it had:

    https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Services.jsm?redirectlocale=en-US&redirectslug=JavaScript_code_modules%2FServices.jsm

    then i saw prefs then it linked to nsIPrefBranch and that documented all of it. nsIPrefBranch2 is deprecated so I knew it wasn't that.

    MDN is your friend :)