Chrome 扩展:在一个扩展中管理多个 html 页面

Sub*_*way 2 google-chrome-extension

我想在我的扩展中有几个 html 文件,以便我可以根据某些条件或事件打开它们中的每一个。假设我希望在用户选择上下文菜单上的选项时打开 a.html。

我尝试了以下方法:

清单.json:

{
"name":  "My extension",
"version": "1.1",
"background": { "page": ["background.html"] },
"incognito": "split",
"permissions": ["tabs", "<all_urls>", "contextMenus"],
"icons": { "16": "images/16.png" },
"manifest_version": 2
}
Run Code Online (Sandbox Code Playgroud)

背景.html:

<!DOCTYPE html>
<html>
<head>
    <script src="background.js"></script>
    <script src='someWindow.js'></script>
</head>
<body>
</body>
</html>  
Run Code Online (Sandbox Code Playgroud)

背景.js:

var winID;
chrome.contextMenus.onClicked.addListener(function proccess_interested(info, tab){

    chrome.tabs.create({active: false}, function(newTab) {

    // After the tab has been created, open a window to inject the tab into it.
    chrome.windows.create(
        {
            tabId:      newTab.id,
            type:       "popup",
            url:        chrome.extension.getURL('a.html'),
            focused: true
        },function(window){
                 winID = newWindow.id;
          });
    });
})

chrome.extension.onMessage.addListener(function(Msg, sender, sendResponse) {

if(Msg.close_comment_win){
    chrome.windows.remove(winID, function(){});
}
});
Run Code Online (Sandbox Code Playgroud)

someWindow.js:

function hide_win()
{
    chrome.extension.sendMessage({close_win: close}, function(response) {});
}
Run Code Online (Sandbox Code Playgroud)

一个.html:

<!DOCTYPE html>
<html>
<head>

<script src='someWindow.js'></script>

head     //with tags, can't show it here
body
<input type='button' value=' Cancel ' onclick="hide_win()"></input>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

单击上下文菜单时会打开该窗口,但单击取消时不会关闭该窗口。console.log 说: Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".我猜原因是 a.html 不是扩展的一部分,即使触发 sendMessage 的 someWindow.js 是扩展的一部分

通过清单在扩展中包含 a.html 不是一种选择,因为只能包含一个背景 html 页面。

当然,chrome.windows.remove(winID, function(){});hide_win()不使用 sendMessage 的情况下直接放入时,我会得到相同的结果。

任何想法如何完成这项工作?

Bea*_*ist 5

正如错误所说,content security policy在扩展 html 页面中包含任何内联代码是违反 v2 的。只需将该处理程序移动到您的js文件,它应该可以正常工作。