在Chrome扩展中使用远程JavaScript

在Chrome扩展中使用远程JavaScript,javascript,jquery,google-chrome,google-chrome-extension,Javascript,Jquery,Google Chrome,Google Chrome Extension,有没有办法将远程JS文件发送到chrome扩展名 我的manifest.json如下所示: { "name": "My Extension", "version": "0.1", "content_scripts": [ { "matches": ["http://*/*"], "js": ["jquery.js", "main.js"], "run_at": "document_end", "all

有没有办法将远程JS文件发送到chrome扩展名

我的manifest.json如下所示:

{
    "name": "My Extension",
    "version": "0.1",
    "content_scripts": [
    {
        "matches": ["http://*/*"],
        "js": ["jquery.js", "main.js"],
        "run_at": "document_end",
        "all_frames": true
    }
    ]
}
$('head').append('<script src="http://example.com/myApi.js?key=%key%"></script>');
我想使用一个JavaScript API,它受所选域的使用限制,因此我不能像这样简单地将它插入到Chrome中加载的页面中:

{
    "name": "My Extension",
    "version": "0.1",
    "content_scripts": [
    {
        "matches": ["http://*/*"],
        "js": ["jquery.js", "main.js"],
        "run_at": "document_end",
        "all_frames": true
    }
    ]
}
$('head').append('<script src="http://example.com/myApi.js?key=%key%"></script>');
$('head')。追加('');
因为JSAPI提醒我,我正在URL上使用它,我没有给他们

我只想使用这个远程JS的一些函数。幸运的是,可以在本地主机上注册和使用API密钥

但我没有任何线索,如何解决这个问题-将远程JS文件放在哪里才能使用它

我想创建一个,但可能它不是一个解决方案,因为JSAPI代码无法执行


在哪里插入代码?一些背景页面???

尝试以下脚本:

if(window.top == window && !document.getElementById('molseek'))
{
    if (document && document.doctype && document.doctype.name && document.doctype.name.toLowerCase() == 'html') {
        loadToolTip();
    }
    // Weird guest... but if we got an head element, try to validate even if no doctype...
    else if (document && document.getElementsByTagName('head').length) {
        loadToolTip();
    }
    // Weird guest #2... but if we got title element, try to validate even if no doctype nor head...
    else if (document && document.getElementsByTagName('title').length) {
        loadToolTip();
    }
}

function loadToolTip(){

    (function(s){
        if(!window.molseek){
            s.src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
            (document.getElementsByTagName("head").item(0)||document.body).appendChild(s)
        }
    })(document.createElement('script'));
    (function(s){
        if(!window.apture){
            s.src='https://location';
            (document.getElementsByTagName("head").item(0)||document.body).appendChild(s)
        }
    })(document.createElement('script'));

}
您是否尝试过:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://example.com/myApi.js?key=%key%';
document.getElementsByTagName('head')[0].appendChild(script); 

我在我的Google Chrome扩展中使用它,效果很好。

目前无法找到远程托管扩展代码的方法,但仍然允许该代码使用Chrome.*javascript调用

也许一种解决方案是插入my(托管在我的域中的某个地方并在API中注册)页面的iframe,然后从这里调用JS函数?此解决方案可以工作,但它不喜欢依赖于我的域/服务器可用性。我想在Chrome中的一些“沙箱”中执行代码(在本地主机上的API视图中)http://example.com/myApi.js?key=%key%在浏览器中,保存文件,将其命名为
external.js
,并将其放在与
jquery.js
main.js
(也像这样将它添加到清单中:
[“jquery.js”,“main.js”,“external.js”]
)。我无法让它工作,我在background.html页面中使用它,而不是弹出式tho,这可能是为什么?