在基于 addon-sdk 的扩展中使用 fetch API

Using fetch API in an addon-sdk basaed extension

我想在我的扩展程序中使用新的 fetch API 而不是旧的 XMLHttpReuest,但在扩展程序的上下文中似乎无法使用 fetch。

我什至尝试使用附加到隐藏 window 的提取 API,但出现错误。

var fetch = Cc['@mozilla.org/appshell/appShellService;1']
  .getService(Ci.nsIAppShellService).hiddenDOMWindow
  .fetch;

这是错误消息:TypeError: 'fetch' called on an object that does not implement interface Window.

那么有没有办法在扩展中利用这个新的API?

这对我有用 - 假设新的 jpm 附加 SDK

const { Cu } = require("chrome");
Cu.importGlobalProperties(["fetch"]);

现在您可以像在网页中一样使用它

fetch('http://cross.origin.works.too/huzzah.html')
.then(response => response.json())
.then( //.... etc