Services.wm 未定义(Firefox SDK 扩展)
Services.wm is undefined (Firefox SDK Extension)
当我使用 Firefox Addon SDK (JPM) 时出现错误 TypeError: Services.wm is undefined
,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 并不像您想象的那样有效。它的return值是导入模块的全局对象。
通常情况下,模块的导出符号作为第二个对象的属性导入(如果指定)或导入当前全局(如果未指定),这将定义 Services
,然后您立即将其替换为 return值。
只需使用 Cu.import("resource://gre/modules/Services.jsm", this);
,而不使用其 return 值,就可以工作并从该模块导入所有导出的符号。
以下使用 destructuring assignment 的形式有效,但不鼓励使用,因为它进入目标模块的全局并获取常量而不是仅访问导出的符号:
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
正确执行此操作的 SDK 方法是
const {Services} = require("resource://gre/modules/Services.jsm");
当我使用 Firefox Addon SDK (JPM) 时出现错误 TypeError: Services.wm is undefined
,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 并不像您想象的那样有效。它的return值是导入模块的全局对象。
通常情况下,模块的导出符号作为第二个对象的属性导入(如果指定)或导入当前全局(如果未指定),这将定义 Services
,然后您立即将其替换为 return值。
只需使用 Cu.import("resource://gre/modules/Services.jsm", this);
,而不使用其 return 值,就可以工作并从该模块导入所有导出的符号。
以下使用 destructuring assignment 的形式有效,但不鼓励使用,因为它进入目标模块的全局并获取常量而不是仅访问导出的符号:
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
正确执行此操作的 SDK 方法是
const {Services} = require("resource://gre/modules/Services.jsm");