27

我正在尝试制作一个元素荧光笔 chrome 扩展。工作流程: - 点击浏览器图标 - 点击页面 - 突出显示点击的元素

我在使用 manifest_version:2 在浏览器操作时运行内容脚本时遇到问题当我检查出现的弹出窗口时,它说:

拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src 'self' chrome-extension-resource:” (popup.html:5)。

这是 popup.html 中的内联脚本所在的位置,并且该脚本不起作用

我有:

清单.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>
  <head>
  </head>
  <body>
    <script>
      chrome.tabs.executeScript(null,{
        code:"document.body.style.backgroundColor='red'"
      });
    </script>
    <div id='msg' style="width:300px">...</div>
  </body>
</html>

任何帮助将不胜感激

4

2 回答 2

45

原来我无法正确阅读错误,直到我在这里看到它

Apparently manifest v2 does not allow you to have inline scripts, so you just need to

src="path_to_the_file.js"
于 2012-07-18T16:17:16.453 回答
1

In extension to @tak3r's answer and @Doug's comment:

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

Include the following in the <head></head> of your HTML

<script type="text/javascript" src="main.js"></script>
于 2014-08-28T10:22:59.677 回答