importScripts() 函数在 Firefox 插件代码中不起作用

importScripts() function not working in Firefox addon code

我正在构建一个附加组件,它有多个与之关联的 .js 文件,其中许多文件需要访问 require() 函数,但是当我在其中使用 require 函数时,我得到了错误要求不是 defined.used importScripts() 来包含 require.js 文件,但导入脚本也会产生错误。

importScripts('resource://gre/modules/workers/require.js');

也用过

self.importScripts('resource://gre/modules/workers/require.js');

产生的错误是

JPM undefined   Message: ReferenceError: importScripts is not defined

JPM undefined   Message: ReferenceError: importScripts is not defined

需要帮助来包含多个可以访问 require()importScripts() 函数的文件。

怎么样:

let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
               .getService(Ci.mozIJSSubScriptLoader);
loader.loadSubScript("chrome://marionette/content/simpletest.js");

您似乎在使用附加 SDK。

您不能对所有 JS 文件使用特权代码,包括 require()。您只能使用 main.js 脚本中的特权代码。然后使用内容script/worker在主脚本和其他脚本之间进行通信。

这是一个在插件中使用 webworker 的好例子,虽然没有提到 importScripts 函数:-(

how to use webworkers in Mozilla addons

您可以尝试在您的工作脚本中使用 importScripts 函数正常添加它们,但为工作脚本提供完整的网址,FF 似乎没有抱怨。