firefox bootstrap 插件:未执行安装事件

firefox bootstrap addon: install event is not executed

我正在尝试创建一个引导插件,它只是在安装时将新选项卡 url 设置为新值,并在卸载时将其重置为旧值。

这是我的bootstrap.js。我认为 install 函数会抛出异常,因为 require 没有定义,但我不确定调试器是否在正确的范围内执行了我在 Scratchpad 中编写的代码。

我在某处读到 api 与附加 sdk 的自举扩展相同,因此 require 应该没问题。如果不是这种情况,能否将我引导至描述我可以在 bootstrap.js 中使用的代码的页面,我没有找到任何内容:(

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);
}

普通引导加载项不会自动访问 SDK,即没有 require

  1. 要么专门使用非 SDK 内容,例如 nsIPrefBranchServices.jsm
  2. 或者先写一个SDK插件
  3. 或者自己为您的附加组件连接 SDK 加载器。除了 SDK 附加组件本身(嘿),我知道的唯一实例是 Scriptish.

来自: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


你问的是:

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


我回复了:

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 :)