【问题标题】:How do I make a content script load before another content script (that runs on the same event) in a Firefox extension?如何在 Firefox 扩展中的另一个内容脚本(在同一事件上运行)之前加载内容脚本?
【发布时间】:2022-01-04 19:27:44
【问题描述】:

这是我的 manifest.json 文件的一部分:

  "content_scripts": [ {
"js": [ "common.js", "generation.js" ],
"matches": [ "*://www.site.com/*" ],
"run_at": "document_start"
},
{
"js": [ "page_init.js" ],
"matches": [ "*://www.site.com/*" ],
"run_at": "document_start"
},

在这里,我想在“page_init.js”之前运行“common.js”,因为 common.js 包含许多其他内容脚本需要运行的共享代码和全局变量和函数。我在清单中的 page_init.js 之前定义了 common.js,并且在 Chrome 中,这样做会导致 common.js 在所有其他内容脚本之前运行,这正是我所需要的。但是,在 Firefox 中,它似乎没有这样做。

但是,当我在 Firefox 中运行此扩展时,page_init 会抛出错误:

ReferenceError: ReferenceError: href is not defined

"href" 是 common.js 中定义的变量。

我是否需要让 page_init.js 在 document_end 运行才能使 common.js 完全运行,还是有其他方法?我需要它们都在 document_start 运行。

更新:

我设法通过将清单内容更改为以下内容来解决问题:

 "content_scripts": [ 
{
"js": [ "common.js", "generation.js", "page_init.js" ],
"matches": [ "*://www.site.com/*" ],
"run_at": "document_start"
},

【问题讨论】:

    标签: javascript firefox firefox-addon


    【解决方案1】:

    来自firefox documentation

    js

    路径数组,相对于 manifest.json,引用 JavaScript 将注入匹配页面的文件。

    文件按给定的顺序注入。这意味着,例如, 如果您在此处包含 jQuery,后跟另一个内容脚本,例如 这……

    这可能是 firefox 的错误,您可能需要报告它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 2013-11-24
      • 2011-11-25
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      相关资源
      最近更新 更多