0

我有一个带有一些基本偏好的 Firefox 扩展。这些偏好之一,通话记录,具有布尔值​​。以下是 xul 文件的相关部分:

<preference id="pref_history" name="extensions.myext.history" type="bool"/>

...

<label control="history" value="Enable History:"/>
   <radiogroup id="history" preference="pref_history" orient="horizontal">
      <radio value="true" label="Yes"/>
      <radio value="false" label="No"/>
   </radiogroup>

在 .js 中,我们实例化了首选项服务。

var prefs = Components.classes["@mozilla.org/preferences-service;1"]
            .getService(Components.interfaces.nsIPrefService);
prefs = prefs.getBranch("extensions.myext.");
var history = prefs.getBoolPref("history");

如果历史记录值设置为true我将向网站发送请求。几乎一切正常,并且根据首选项设置配置了正确的值。问题是我无法检查偏好历史记录的值是否为truefalse。我试过使用:

if(history == 'true'){
// never sends the request
}

if(history == true){
// lock the script
}

if(history){
// lock the script but seems the most obvious path to follow
}

if(prefs.getPrefType('extensions.myext.history')){
// always sends the request
}

它看起来很简单,但让我头疼!

==编辑==

我尝试在 try catch 中获取历史偏好的值。当首选项不存在时(我们第一次运行它时),它应该抛出异常,但它不起作用。

try{
  var history = prefs.getBoolPref("history");
}
catch (err){
  prefs.setBoolPref('history', true);
}
4

0 回答 0