Javascript google chrome扩展制作中的内容安全策略错误
我正在做一个chrome扩展,可以在新标签页中打开页面上的所有链接 以下是我的代码文件: manifest.jsonJavascript google chrome扩展制作中的内容安全策略错误,javascript,google-chrome,google-chrome-extension,content-security-policy,Javascript,Google Chrome,Google Chrome Extension,Content Security Policy,我正在做一个chrome扩展,可以在新标签页中打开页面上的所有链接 以下是我的代码文件: manifest.json { "name": "A browser action which changes its icon when clicked.", "version": "1.1", "permissions": [ "tabs", "<all_urls>" ], "browser_action": { "default_title"
{
"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
}
{
“名称”:“单击时更改其图标的浏览器操作。”,
“版本”:“1.1”,
“权限”:[
“制表符”
],
“浏览器操作”:{
“默认标题”:“链接”,//可选;显示在工具提示中
“默认弹出窗口”:“popup.html”//可选
},
“内容脚本”:[
{
“匹配项”:[“”],
“js”:[“background.js”]
}
],
“清单版本”: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>
我很棒的弹出窗口!
函数getPageAndSelectedDemidex()
{
chrome.tabs.getSelected(null,函数(tab){
sendRequest(tab.id,{问候语:“你好”},函数(响应)
{
console.log(response.bye);
});
});
}
chrome.browserAction.onClicked.addListener(函数(选项卡){
GetPageAndSelectedDemidex();
});
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';
}
}
chrome.extension.onRequest.addListener(
功能(请求、发送方、发送响应){
console.log(sender.tab?
“来自内容脚本:”+sender.tab.url:
“从延期开始”);
if(request.greeting==“hello”)
updateIcon();
});
函数updateIcon(){
var allLinks=document.links;
对于(var i=0;i“manifest_version”:2
的后果之一是默认启用。Chrome开发人员选择对此严格要求,并且始终不允许内联JavaScript代码-只允许执行放置在外部JavaScript文件中的代码(以防止扩展)。因此,您不应该在popup.html
中定义getPageAndSelectedDemidex()
函数,而应该将其放入popup.js
文件中,并将其包含在popup.html
中:
而且
也必须更改,onclick
属性也是一个内联脚本。您应该指定一个ID属性:
。然后在popup.js
中,您可以将事件处理程序附加到该按钮:
window.addEventListener(“加载”,函数()
{
document.getElementById(“按钮”)
.addEventListener(“单击”,获取页面并选择Destedex,false);
},假);
这比我强。完美的答案,这正是你需要做的。这太愚蠢了。谁想出了这个主意?!现在我必须更改所有内联onclick并将它们放在外部js文件中。我有一堆文件,包括它们。@Derek:从安全角度看,这是一种更好的方法,因为人们都是对innerHTML
之类的东西不够小心。尽管如此,Chrome似乎在一定程度上放宽了当前金丝雀版本中的内容策略要求,不安全内联
现在也可能得到支持。@wladimir-希望他们将来会放宽该策略!