Services.wm is undefined (Firefox SDK Extension)

两盒软妹~` 提交于 2019-12-20 07:22:21

问题


I get an error TypeError: Services.wm is undefined, when I use Firefox Addon SDK (JPM), and the following code in 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));
}

Any suggestions would be of a great help, thank you.


回答1:


Cu.import doesn't work as you think it does. Its return value is the global object of the imported module.

Normally the exported symbols of the module are imported as properties of the second object if specified or into the current global if not, which would define Services, which you then immediately replace with the return value.

Simply useing Cu.import("resource://gre/modules/Services.jsm", this);, without its return value, will work and import all exported symbols from that module.

The following form using destructuring assignment works, but is discouraged because it reaches into the target module's global and gets the constants instead of only accessing the exported symbols:

const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});

The SDK way of doing this properly is

const {Services} = require("resource://gre/modules/Services.jsm");


来源:https://stackoverflow.com/questions/36567318/services-wm-is-undefined-firefox-sdk-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!