我收到错误 TypeError: Services.wm is undefined
,当我使用 Firefox Addon SDK (JPM) 时,以及 index.js
中的以下代码:
var self = require("sdk/self");
const { Cu } = require("chrome");
let Services = Cu.import("resource://gre/modules/Services.jsm");
require("sdk/ui/button/action").ActionButton({
id: "list-tabs",
label: "List Tabs",
icon: "./icon-16.png",
onClick: myTestFunc
});
function myTestFunc() {
var windows = Services.wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements())
iterateWindows(windows.getNext().QueryInterface(Components.interfaces.nsIDOMWindow));
}
任何建议都会有很大帮助,谢谢。
最佳答案
Cu.import并不像你想象的那样工作。它的返回值是导入模块的全局对象。
通常,模块的导出符号将作为第二个对象的属性(如果指定)导入,或者导入到当前全局(如果未指定),这将定义 Services
,然后立即将其替换为返回值。
只需使用 Cu.import("resource://gre/modules/Services.jsm", this);
(没有返回值)即可工作并导入该模块中的所有导出符号。
以下表格使用destructuring assignment有效,但不鼓励,因为它进入目标模块的全局并获取常量,而不是仅访问导出的符号:
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
正确执行此操作的 SDK 方法是
const {Services} = require("resource://gre/modules/Services.jsm");
关于javascript - Services.wm 未定义(Firefox SDK 扩展),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36567318/