如何从Firefox插件读取配置文件首选项? [英] How to read profile preferences from Firefox add-on?
问题描述
我正在这样做(Firefox 66.0.5,MacOS):
I'm doing this (Firefox 66.0.5, MacOS):
var prefs = Components.classes['@mozilla.org/preferences-service;1']
.getService(Components.interfaces.nsIPrefBranch);
var x = pref.getCharPref('extensions.foo.x');
我得到这个:
JavaScript error: moz-extension://9390a488-d749-d140-91b6-fb1b2a86053f/foo.js,
line 1: TypeError: Components.classes is undefined
阅读个人资料偏好设置的正确方法是什么?
What is the right way to read the preferences of the profile?
我尝试过:
var prefs = Components.classesByID['@mozilla.org/preferences-service;1']
.getService(Components.interfaces.nsIPrefBranch);
得到了:
JavaScript error: moz-extension://ebac8f96-717d-0c4b-b2d3-a7d6f3515843/auth.js,
line 1: TypeError: Components.classesByID is undefined
推荐答案
WebExtensions在更改用户首选项方面的能力受到很大限制.目前有14种可以通过 proxy
),但不直接进行任何操作.不幸的是,没有直接的方法可以从WebExtension中读取或设置用户首选项.直接调整用户首选项的功能仅适用于较旧样式的加载项(自Firefox 57起已全部删除).
WebExtensions are very limited in their ability to make changes to user preferences. There are a few, currently 14, that you can affect through the browserSettings
API. There are others which are effectively changed through other WebExtension APIs (e.g. proxy
), but nothing directly. Unfortunately, there's no direct way to read or set user preferences from a WebExtension. The capability to adjust user preferences directly was only available to the older styles of add-ons (all removed as of Firefox 57).
关于您对Components
的使用以及遇到的特定错误,请参阅我对无法在其中使用组件的回答WebExtensions:获取"ReferenceError:未定义Cu" 以获得更多详细信息.在WebExtension中无法使用Components
.
As to your use of Components
and the specific errors which you are getting, please see my answer to Unable to use Components in WebExtensions: get "ReferenceError: Cu is not defined" for more detail. The use of Components
is just not possible in a WebExtension.
我想到的唯一可能在Firefox发行版中调整首选项的方法是使用
The only way that I can think of that it might be possible to adjust preferences in a release version of Firefox is to use Native messaging to communicate to a native application which you also install. After Firefox exits/stops/is shutdown, the native application could read and change the configuration files in the profile directory for the Firefox profile being run. It could then restart Firefox.
在Firefox的Nightly和Developer Edition版本中,仍然应该可以使用
In the Nightly and Developer Edition versions of Firefox, it should still be possible to use a WebExtensions Experiment, which would permit you to make the preference changes you desire using the Components
interface. However, that's not a viable solution if this is an extension you want to distribute publicly.
这篇关于如何从Firefox插件读取配置文件首选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!