Javascript chrome 扩展在浏览器操作上插入内容脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11545743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
chrome extension insert content script on browser action
提问by Stefan
I am trying to make basically an element highlighter chrome extension. Workflow: - click on browser icon - click on the page - hightlight the element clicked
我正在尝试制作一个基本的元素荧光笔 chrome 扩展。工作流程: - 单击浏览器图标 - 单击页面 - 突出显示单击的元素
I am having troubles in running content scripts upon browser action using manifest_version:2 When I inspect the popup that appears it says:
我在使用 manifest_version:2 在浏览器操作时运行内容脚本时遇到问题,当我检查出现的弹出窗口时,它说:
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:" (popup.html:5).
拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src 'self' chrome-extension-resource:”(popup.html:5)。
Which is where the inline script in popup.html is and the script does not work
这是 popup.html 中的内联脚本所在的位置,该脚本不起作用
I have:
我有:
manifest.json:
清单.json:
{
"browser_action": {
"default_icon": "images/icon.gif",
"default_popup": "popup.html"
},
"manifest_version": 2,
"description": "MEH!",
"name": "My First Extension",
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"version": "0.1"
}
popup.html:
弹出窗口.html:
<html>
<head>
</head>
<body>
<script>
chrome.tabs.executeScript(null,{
code:"document.body.style.backgroundColor='red'"
});
</script>
<div id='msg' style="width:300px">...</div>
</body>
</html>
Any help would be very much appreciated
任何帮助将不胜感激
回答by Stefan
Turns out I could not read the error properly until I saw it in here
原来我无法正确读取错误,直到我在这里看到它
Apparently manifest v2 does not allow you to have inline scripts, so you just need to
显然 manifest v2 不允许你有内联脚本,所以你只需要
src="path_to_the_file.js"
回答by Peter Butcher
In extension to @tak3r's answer and @Doug's comment:
对@tak3r 的回答和@Doug 的评论的扩展:
Inline scripts need to be changed to external scripts.
内联脚本需要更改为外部脚本。
Move:
移动:
<script>
chrome.tabs.executeScript(null,{
code:"document.body.style.backgroundColor='red'"
});
</script>
To a new file called main.js
and remove the <script></script>
tags
到一个名为的新文件main.js
并删除<script></script>
标签
Include the following in the <head></head>
of your HTML
在<head></head>
您的 HTML 中包含以下内容
<script type="text/javascript" src="main.js"></script>