0

我正在尝试侦听 Firefox 的 about:config 中设置的更改,当我的插件运行时,用户可能已经更改了这些设置。有问题的设置是浏览器的一部分,不是由我的插件创建的。

当用户使用“首选项/服务”模块没有问题地使用我的插件时,我可以手动读取和设置它们,但是如果用户更改了关于配置中的设置,我希望能够在我的插件中进行适当的更改独立于我的插件。

“simple-prefs”模块提供了一个监听器,但这仅适用于特定于您的应用程序的设置,例如“extension.myaddon.mypreference”,而我需要查看的设置类似于“network.someoptionhere”

如果有人能指出我将如何去做的正确方向,我将不胜感激。

4

1 回答 1

1

您将需要使用一些 XPCOM,即nsIPrefService/ nsIPrefBranch(例如 via Services.jsm)。这是preferences/servicesimple-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);
};
于 2013-11-07T16:48:24.277 回答