いくつかの条件やイベントに応じてそれぞれを開くことができるように、拡張機能にいくつかの 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
}
background.html:
<!DOCTYPE html>
<html>
<head>
<script src="background.js"></script>
<script src='someWindow.js'></script>
</head>
<body>
</body>
</html>
background.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(){});
}
});
someWindow.js:
function hide_win()
{
chrome.extension.sendMessage({close_win: close}, function(response) {});
}
a.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 は次のように述べています: sendMessage をトリガーする someWindow.js が extension の一部であるにもかかわらず、 a.html がRefused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".
extension の一部ではないことが理由だと思います。
マニフェストを介して拡張子に a.html を含めることはできません。含めることができるバックグラウンド HTML ページは 1 つだけです。
もちろんsendMessagechrome.windows.remove(winID, function(){});
を使わずに直接入れても同じです。hide_win()
この仕事を成し遂げる方法はありますか?