简体   繁体   English

将JS纳入JS for Chrome扩展程序

[英]Include JS into JS for Chrome extension

How to include JS into another JS file for chrome extension? 如何将JS包含到另一个用于chrome扩展的JS文件中?

I have these 3 files: 我有以下3个文件:

manifest.json 的manifest.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 content_script.js

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



myfile.js (in chrome extension pack) myfile.js (在Chrome扩展包中)

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



sample.js (file exists in my host) sample.js (文件存在于我的主机中)

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



I know content_script.js is Not correct; 我知道content_script.js不正确; I've tested some code but I didn't get any correct result. 我已经测试了一些代码,但是没有得到正确的结果。
How Can I solve this issue? 我该如何解决这个问题?

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

The list of JavaScript files to be injected into matching pages. 要注入到匹配页面的JavaScript文件列表。 These are injected in the order they appear in this array. 这些以它们在此数组中出现的顺序注入。

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM