1

我正在为测试目的创建一个非常简单的 Firefox 插件。我想设置默认首选项(例如将 extensions.myextension.bingo 设置为“ http://www.example.com ”),但是我似乎无法让它工作。

这是我到目前为止所做的:

我的 lib/main.js 看起来像这样:

var {Cc, Ci} = require("chrome");  

var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("extensions.myextension.");
var value = prefs.getCharPref("bingo");

console.log("found: " + value);

我的 defaults/preferences/prefs.js 文件如下所示:

pref("extensions.myextension.bingo", "http://www.example.com");

当我在命令行中运行“cfx test”时,我看到了:

(D:\Users\myuser\addon-sdk-1.16) D:\Users\myuser\Projects\ffaddontest>cfx test
Using binary at 'C:\Program Files (x86)\Mozilla Firefox\firefox.exe'.
Using profile at 'd:\users\myuser\appdata\local\temp\tmpd_6h4q.mozrunner'.
Running tests on Firefox 31.0/Gecko 31.0 ({ec8030f7-c20a-464f-9b0e-13a3a9e97384}) under winnt/x86.
Error: [Exception... "Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED) [nsIPrefBranch.getCharPref]"  nsresult: "0x8000ffff (NS_ERROR_UNEXPECTED)"  location: "JS frame :: resource://gre/modules/addons/XPIProvider.jsm -> jar:file:///d:/users/myuser/appdata/local/temp/tmpd_6h4q.mozrunner/extensions/jid1-LtJU5H9I2NHnBA@jetpack.xpi!/bootstrap.js -> resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-ltju5h9i2nhnba-at-jetpack/ffaddontest/lib/main.js :: <TOP_LEVEL> :: line 4"  data: no]
 Traceback (most recent call last):
  File "resource://gre/modules/commonjs/sdk/loader/cuddlefish.js", line 129, in CuddlefishLoader/options<.load
    result = load(loader, module);
  File "resource://jid1-ltju5h9i2nhnba-at-jetpack/ffaddontest/tests/test-main.js", line 1, in
    var main = require("./main");
  File "resource://gre/modules/commonjs/sdk/loader/cuddlefish.js", line 129, in CuddlefishLoader/options<.load
    result = load(loader, module);
  File "undefined", line 4, in
0 of 1 tests passed.
JavaScript strict warning: chrome://browser/content/urlbarBindings.xml, line 692: reference to undefined property this._value
JavaScript error: chrome://browser/content/urlbarBindings.xml, line 674: aUrl is undefined
Total time: 1.966000 seconds
Program terminated unsuccessfully.

显然没有设置首选项(在运行“cfx run”或使用“cfx xpi”创建和加载 xpi 文件时,我在 about:config 中看不到它)。

我做错了什么?

谢谢您的答复。

4

0 回答 0