Chrome扩展程序:将脚本注入到新创建的Windows中

时间:2018-07-17 00:28:48

标签: javascript google-chrome-extension permissions

我目前正在尝试构建chrome扩展程序,其中一部分使用以下方法创建一个新的“弹出”窗口:

chrome.windows.create({
            url: chrome.runtime.getURL("myHTML.html"),
            type: "popup"
        }, function (window) {
            // Trace tab id which is created with this query 
            myTabID = window.tabs[0].id;
            });

从我的html文件加载新窗口并存储创建的窗口的标签ID。

但是我要做的是一旦完成以下操作,就将Jquery和“ myScript.js”注入“ myHTML.html”中:

//Listen for vocab list window creation
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
    // Inject script into chosen tab after it is loaded completely
    if (tabId == myTabID && changeInfo.status == "complete") {
        // Inject jQuery in current tab
        chrome.tabs.executeScript(tabId, {
            "file": "jquery-3.2.1.min.js"
        }, function () {
            console.log("Injected jquery into page");
        });
        // Inject script in current tab
        chrome.tabs.executeScript(tabId, {
            "file": "myScript.js"
        }, function () {
            console.log("Injected myScript into page");
        });
    }
});

但是当我尝试这样做时,它会给我一个错误,指出:

Unchecked runtime.lastError while running tabs.executeScript:
Cannot access contents of url "chrome-extension://<my extension id>
/myHTML.html". Extension manifest must request permission
to access this host.

我当前的manifest.json权限如下:

"permissions": [
    "activeTab",
    "storage",
    "tabs",
    "<all_urls>"
  ]

我尝试寻找答案,但似乎找不到真正能解决我问题的任何答案。任何帮助将不胜感激!

0 个答案:

没有答案