Javascript 将chrome扩展迁移到清单2-脚本注入

Javascript 将chrome扩展迁移到清单2-脚本注入,javascript,google-chrome-extension,Javascript,Google Chrome Extension,我正在尝试将我的Chrome扩展迁移到清单2。当前版本使用的background.html页面基本上是一个大的脚本标记。由于这已经不可能了,我转而使用background.js脚本,在进行了大量搜索和实验后,仍然无法从外部文件中注入脚本。在当前版本中,我只使用document.write插入一个脚本标记,该标记将在浏览器加载时运行,但我现在还没有找到这样做的方法 我知道chrome.tabs.onUpdated.addListener函数以及使用XMLHttpRequest对象为每个选项卡更新注

我正在尝试将我的Chrome扩展迁移到清单2。当前版本使用的background.html页面基本上是一个大的脚本标记。由于这已经不可能了,我转而使用background.js脚本,在进行了大量搜索和实验后,仍然无法从外部文件中注入脚本。在当前版本中,我只使用document.write插入一个脚本标记,该标记将在浏览器加载时运行,但我现在还没有找到这样做的方法

我知道chrome.tabs.onUpdated.addListener函数以及使用XMLHttpRequest对象为每个选项卡更新注入脚本的能力,但是我想要运行的脚本应该只在浏览器加载时才这样做

当前代码(在background.html文件的脚本标记中):

但这似乎只适用于本地脚本文件,而我需要加载一个外部联机文件。

a)
document.write(“”)
background.js
中不起作用,因为您的
文档是后台页面的文档。请参阅和

b)
chrome.extension.getURL(url)检索文件的相对url,您可以改为尝试

我可以用下面的代码向任意页面注入,您的权限设置正确吗

工具书类 (a)

(b)

(c)

(d)

manifest.json
如果这种注入对您不起作用,请告诉我。

您收到的错误是由于清单中的指令内容安全策略造成的。您需要指定脚本的域作为脚本的一部分。以下是jQuery的一个示例:

"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'"
在Background.html页面中

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
这是更新选项卡后何时显示页面操作的示例。在以前的代码中:

chrome.tabs.onUpdated.addListener(displayPageAction);

您可以阅读更多有关Chrome扩展的内容安全策略和有关消息传递的信息。

您可以使用
窗口。onload
或侦听
DOMContentLoad
事件,以便仅在浏览器加载时运行代码,共享您的代码,很容易找出缺少的内容。@Sudarshan感谢您的评论。我在问题中添加了一些代码。问题是浏览器加载时无法运行代码,这已经发生了。问题是从外部联机文件中注入脚本。请检查我的答案,如果需要更多信息,请告诉我。您好,我只是简短的评论:我尚未阅读所有答案,但注意到您在错误消息中提到“http://mydomain…”。。。那可能是打字错误。。。但以防万一。。。据我所知,您无法获取不在SSL下的外部文件,因此它应该是“https://mydomain…”。当我第一次尝试迁移到manifestv2时,我遇到了一条类似的错误消息,这就是为什么我提出这个问题。祝你好运:)@frenetix谢谢!看来这是主要问题
//Creating a Script Tag
var _scriptTag = document.createElement('script');
//Referencing external URL
_scriptTag.src='https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.js';
//Appending child
(document.head||document.documentElement).appendChild(_scriptTag);
"content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'"
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
/**
* Listener for Displaying the Extension Page Action when the Tab is updated.
* @private
* @event displayPageAction
* @param {Number} tabId The tabId given by the tabs listener to know which tab was updated.
* @param {Object} changeInfo The current status of the tab.
* @param {Object} tab The metadata of the tab.
**/
var displayPageAction = function (tabId, changeInfo, tab) {
    var match = regexAIESEC.exec(tab.url); // var regexAIESEC = new RegExp(/http:\/\/www.myaiesec.net\//);
    // We only display the Page Action if we are inside a MyAIESEC Tab.
    if(match && changeInfo.status == 'complete') {
        //We send the proper information to the content script to render our app.
         chrome.tabs.sendMessage(tab.id, {load: true}, function(response) {
            if(response) {
                //After successfully getting the response, we show the Page Action Icon.
                chrome.pageAction.show(tab.id);     
            }
        });
    }
};
chrome.tabs.onUpdated.addListener(displayPageAction);