【问题标题】:Firefox SDK : Listen for a change in non addon specific about:config entryFirefox SDK:监听非插件特定 about:config 条目的变化
【发布时间】:2013-11-07 14:21:55
【问题描述】:
我正在尝试监听 Firefox 的 about:config 中设置的更改,当我的插件运行时用户可能已经更改了这些更改。有问题的设置是浏览器的一部分,不是由我的插件创建的。
当用户使用“首选项/服务”模块没有问题地使用我的插件时,我可以手动读取和设置它们,但是如果用户更改了设置,我希望能够在我的插件中进行适当的更改在关于配置独立于我的插件。
“simple-prefs”模块提供了一个监听器,但这仅适用于特定于您的应用程序的设置,例如“extension.myaddon.mypreference”,而我需要查看的设置类似于“network.someoptionhere”
如果有人可以为我指明正确的方向,我将不胜感激。
【问题讨论】:
标签:
javascript
firefox
firefox-addon
listener
firefox-addon-sdk
【解决方案1】:
您需要使用一些 XPCOM,即nsIPrefService/nsIPrefBranch(例如通过Services.jsm)。这与 preferences/service 和 simple-prefs 包装的内容相同。
这是一个完整的例子:
const {Ci, Cu} = require("chrome");
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
function observe(subject, topic, data) {
// instanceof actually also "casts" subject
if (!(subject instanceof Ci.nsIPrefBranch)) {
return;
}
console.error(subject.root, "has a value of", subject.getIntPref(""), "now");
}
var branch = Services.prefs.getBranch("network.http.max-connections")
branch.addObserver("", observe, false);
exports.onUnload = function() {
// Need to remove our observer again! This isn't automatic and will leak
// otherwise.
branch.removeObserver("", observe);
};