简体   繁体   English

Chrome扩展程序-加载扩展程序添加的javascript文件

[英]Chrome extensions - loading javascript files added by the extension

My chrome extension adds several references to js files (eg <script src="javascript_file_1.js"></script> ). 我的chrome扩展程序添加了对js文件的多个引用(例如<script src="javascript_file_1.js"></script> )。 Using chrome's 'Inspect element' feature I can see that all references are added correctly, however they do not seem to be 'loaded'. 使用chrome的“检查元素”功能,我可以看到所有引用均已正确添加,但是它们似乎没有被“加载”。 Is there any way to 're-execute' the page so these new js files are loaded? 有什么办法可以“重新执行”页面,以便加载这些新的js文件?

Here is the code I use: 这是我使用的代码:

var srcArray = ["javascript_file_1.js",
        "javascript_file_2.js",
        "javascript_file_3.js",
        "javascript_file_4.js",
        "javascript_file_5.js",
        "javascript_file_6.js",
        "javascript_file_7.js"];

function AddScript(value)
{
    var entry = document.createElement("SCRIPT")
    entry.src = value;
    document.head.appendChild(entry);

}

They seem to be added after the page got rendered, hence their content was not used while rendering the page. 它们似乎是在页面呈现后添加的,因此呈现页面时未使用其内容。

Many thanks, Luke 非常感谢,卢克

Inject your content script with "run_at" : "document_start" , scripts injected with this option are injected right after css injection , but before DOM is constructed or any other script is run. 使用"run_at" : "document_start"注入内容脚本,使用css injection ,构造DOM或运行任何其他脚本之前立即注入使用此选项注入的脚本。

{
  "name": "My extension",
  ...
  "content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "css": ["mystyles.css"],
      "js": ["jquery.js", "myscript.js"],
      "run_at": "document_start"
    }
  ],
  ...
}

With this option your code is used while rendering the page. 使用此选项时,呈现页面时将使用您的代码。

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

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