2

我想在我的扩展中有几个 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
}

背景.html:

<!DOCTYPE html>
<html>
<head>
    <script src="background.js"></script>
    <script src='someWindow.js'></script>
</head>
<body>
</body>
</html>  

背景.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(){});
}
});

一些Window.js:

function hide_win()
{
    chrome.extension.sendMessage({close_win: close}, function(response) {});
}

一个.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>

单击上下文菜单时会打开窗口,但单击取消时,它不会关闭。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 页面。

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

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

4

1 回答 1

5

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

于 2013-05-13T21:45:02.793 回答