在 Chrome 扩展内容脚本中包含 Javascript 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3813694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:21:20  来源:igfitidea点击:

Including Javascript files in a Chrome extension content script

javascriptgoogle-chrome-extensionincludecontent-script

提问by Matt Joiner

I'm writing a Chrome extension, and want to write one JS file, that provides several expected functions that aren't present in another, then load that other file. I'm after behavior similar to requirein Perl, #includein C, or execfilein Python when passing the current modules locals and globals, as though the referenced file was inserted directly into the current script.

我正在编写一个 Chrome 扩展程序,并想编写一个 JS 文件,该文件提供了几个在另一个文件中不存在的预期功能,然后加载该其他文件。在传递当前模块局部变量和全局变量时,我追求类似于requirePerl、#includeC 或execfilePython 中的行为,就好像引用的文件直接插入到当前脚本中一样。

Most existing solutions I can find out there refer to embeddeding these "includes" inside script tags, but I'm not sure this applies (and if it does, an explanation of where exactly my extension is injecting all these script tags in the current page).

我可以在那里找到的大多数现有解决方案是指将这些“包含”嵌入脚本标签中,但我不确定这是否适用(如果适用,请说明我的扩展程序在当前页面中注入所有这些脚本标签的确切位置)。

Update0

更新0

Note that I'm writing a content script. At least on the "user" side, I don't provide the original HTML document.

请注意,我正在编写内容脚本。至少在“用户”方面,我不提供原始 HTML 文档。

回答by Matt Joiner

Well the best solution was Chrome-specific. The javascript files are listed in the order they are loaded in the extension's manifest.json, here's an extract of the relevant field:

最好的解决方案是特定于 Chrome 的。javascript 文件按照它们在扩展程序中加载的顺序列出,这是相关字段manifest.json的摘录:

{
  "content_scripts": [
    {
      "js" : ["contentscript.js", "254195.user.js"]
    }
  ]
}

The javascript files are effectively concatenated in the order given and then executed.

javascript 文件按照给定的顺序有效连接,然后执行。

回答by clifgriffin

function load_script (url, cache) 
{ 

    var httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
    httpRequest.open('GET', url, false); 

    if(!cache)
    {
        httpRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    }

    httpRequest.send(null);
    eval(httpRequest.responseText); 

    var s = httpRequest.responseText.split(/\n/); 
    var r = /^function\s*([a-z_]+)/i; 

    for (var i = 0; i < s.length; i++) 
    { 
        var m = r.exec(s[i]); 
        if (m != null) 
        {
            window[m[1]] = eval(m[1]); 
        }
    }
}

load_script("/script.js",true); 

We use this at our organization to do this. Seems to work well enough.

我们在我们的组织中使用它来做到这一点。似乎工作得很好。

回答by Philippe Carriere

Did you try :

你试过了吗 :

<script language="javascript" src="otherfile.js">

or (I'm not sure which... but I recall one of them working)

或者(我不确定是哪个……但我记得其中一个在工作)

document.write('<script type="text/javascript" src="otherfile.js"></script>');