简体   繁体   English

如何在 Firefox 扩展中的另一个内容脚本(在同一事件上运行)之前加载内容脚本?

[英]How do I make a content script load before another content script (that runs on the same event) in a Firefox extension?

Here's a portion of my manifest.json file:这是我 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"
},

Here, I want to run "common.js" before "page_init.js", as common.js contains much shared code and global variables and functions that the other content scripts need to function.在这里,我想在“page_init.js”之前运行“common.js”,因为 common.js 包含很多共享代码和全局变量和函数,其他内容脚本需要 function。 I have defined common.js before page_init.js in my manifest, and, in Chrome, doing this results in common.js running before all the other content scripts, which is what I need.我在清单中的 page_init.js 之前定义了 common.js,并且在 Chrome 中,这样做会导致 common.js 在所有其他内容脚本之前运行,这正是我所需要的。 However, in Firefox, it doesn't seem to do this.但是,在 Firefox 中,它似乎没有这样做。

When I run this extension in Firefox, however, page_init throws an error:但是,当我在 Firefox 中运行此扩展时,page_init 会引发错误:

ReferenceError: ReferenceError: href is not defined

"href" is a variable that's defined in common.js. “href”是一个在 common.js 中定义的变量。

Do I need to make page_init.js run at document_end in order for common.js to fully run, or is there another way?我是否需要让 page_init.js 在 document_end 运行才能使 common.js 完全运行,还是有其他方法? I need them to both run at document_start.我需要它们都在 document_start 上运行。

From firefox documentation :来自firefox 文档

js js

An array of paths, relative to manifest.json, referencing JavaScript files that will be injected into matching pages.相对于 manifest.json 的路径数组,引用将注入匹配页面的 JavaScript 文件。

Files are injected in the order given .文件按给定的顺序注入 This means that, for example, if you include jQuery here followed by another content script, like this...这意味着,例如,如果您在此处包含 jQuery 后跟另一个内容脚本,如下所示...

It's probably a firefox bug, you may want to report it.这可能是一个 firefox 错误,您可能需要报告它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Firefox Web扩展的内容脚本中使用Wasm? - How do I use Wasm in the content script of a Firefox web extension? 如何从 firefox 扩展的内容脚本发出 GET 请求? - How do I make a GET request from content script of firefox extension? Firefox扩展 - 我们如何检查这是否是内容脚本? - Firefox extension - how do we check if this is a content script? Firefox扩展没有运行内容脚本 - Firefox extension not running content script 相当于Firefox扩展中的内容脚本? - The equivalent to a content script in a Firefox extension? Firefox扩展开发:如何在内容脚本中创建全局函数,以便其他加载的脚本文件可以访问它? - Firefox extension development: How do I create a global function inside a content script, so other loaded script files can access it? 如何将屏幕截图从扩展程序移动到内容脚本? - How do i move a screenshot from an extension to a content script? 如何从内容脚本链接到扩展程序中的页面? - How do I link to a page in my extension from a content script? 如何触发在firefox扩展的内容脚本中引用函数的已注入html标记的触发事件属性? - How can triggered event attributes of injected html tags that reference functions in a content script of a firefox extension be executed? 如何从 chrome 扩展中的内容脚本中获取另一个文件内容 - How to get another file content from content script in chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM