javascript - 将 JS 包含到 Chrome 扩展的 JS 中

标签 javascript google-chrome google-chrome-extension

如何将JS包含到另一个JS文件中以用于chrome扩展?

我有这 3 个文件:

ma​​nifest.json

{
  "name": "Block request",
  "version": "1.0",
  "manifest_version": 2,

  "content_scripts": [{
    "matches": ["<all_urls>"],
       "js": ["content_script.js"]
}],

  "permissions": [
      "webRequest",
      "webRequestBlocking",
      "<all_urls>"
  ]
}



content_script.js

include 'myfile.js';
include 'https://sample.com/sample.js';



myfile.js(在 Chrome 扩展包中)

if (window.location.hostname === "www.sample.com") {
    console.log("Hi console");
}



sample.js(文件存在于我的主机中)

if (window.location.hostname === "www.demo.com") {
    console.log("My message from Space");
}



我知道 content_script.js 不正确;我测试了一些代码,但没有得到任何正确的结果。
我该如何解决这个问题?

最佳答案

https://developer.chrome.com/extensions/content_scripts

The list of JavaScript files to be injected into matching pages. These are injected in the order they appear in this array.

"js": ["content_script.js", "myfile.js", "sample.js"]

关于javascript - 将 JS 包含到 Chrome 扩展的 JS 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54637902/

相关文章:

javascript - 使用 controllerAs 语法从 "this"访问指令的隔离范围

javascript - 替换 Cloudinary 上的图像

google-chrome-extension - Chrome 扩展 browserAction.onClicked 未监听

javascript - 如何用自定义代码替换 window.onbeforeunload ?

javascript - 从 chrome 扩展更改外部 CSS

javascript - chrome.runtime.onConnect.addListener() 在初始 chrome 选项卡上不起作用

javascript - 使用 Javascript 的图形编辑器

javascript - 当 window.onbeforeunload 返回 false 时,Safari 保持 "loading"

jquery - Chrome 10 必填字段问题 (jQuery)

javascript - 在可滚动容器上拖动时防止自动滚动