新しいタブでページのすべてのリンクを開くChrome拡張機能を作成しています。最初は私がすべて強調表示したかったGoogle Chromeの拡張機能でContent-Security-Policyエラーが発生する
manifest.jsonを
{
"name": "A browser action which changes its icon when clicked.",
"version": "1.1",
"permissions": [
"tabs", "<all_urls>"
],
"browser_action": {
"default_title": "links", // optional; shown in tooltip
"default_popup": "popup.html" // optional
},
"content_scripts": [
{
"matches": [ "<all_urls>" ],
"js": ["background.js"]
}
],
"manifest_version": 2
}
popup.html
<!doctype html>
<html>
<head>
<title>My Awesome Popup!</title>
<script>
function getPageandSelectedTextIndex()
{
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function (response)
{
console.log(response.farewell);
});
});
}
chrome.browserAction.onClicked.addListener(function(tab) {
getPageandSelectedTextIndex();
});
</script>
</head>
<body>
<button onclick="getPageandSelectedTextIndex()">
</button>
</body>
</html>
background.js
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
updateIcon();
});
function updateIcon() {
var allLinks = document.links;
for (var i=0; i<allLinks.length; i++) {
alllinks[i].style.backgroundColor='#ffff00';
}
}
:ここ
は私のコードファイルですtのリンク彼は何らかの方法でページングしたりマークしたりする。 「コンテンツセキュリティポリシーのためにインラインスクリプトを実行するのが拒否されました」というエラーが表示されます。
ポップアップ内のボタンを押すと、私はこのエラーを受け取ります:Refused to execute inline event handler because of Content-Security-Policy
。
私はこれらのエラーを解決するために、私のクロムエクステンションを使用して新しいタブですべてのリンクを開くことができます。
ちょうど私にそれを打つ。パーフェクトな答えは、あなたがする必要があるものです。 – Alasdair
kありがとう..iそれを試してみます – Saad
これはとても馬鹿です。誰がこのアイデアを思いついたのですか?今度はすべてのインラインonclicksを変更して、それらを外部のjsファイルに入れなければなりません。そして、私はそれらを含むファイルの束を持っています。 –