我正在尝试监听firefox的设置更改:在我的插件运行时用户可能已更改的配置。有问题的设置是浏览器的一部分,而不是我的插件创建的。
当用户使用我的插件时,我可以手动阅读和设置它们,使用“偏好/服务”模块没有问题,但是如果用户更改了设置,我希望能够在我的插件中进行适当的更改在大约配置中独立于我的插件。
“simple-prefs”模块提供了一个监听器,但这只适用于特定于您的应用程序的设置,例如“extension.myaddon.mypreference”,其中我需要注意的设置就像“network.someoptionhere”
如果有人能指出我正确的方向,我会非常感激。
答案 0 :(得分: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);
};