chrome扩展插件在浏览器操作中插入内容脚本

chrome extension insert content script on browser action

本文关键字:插入 脚本 操作 扩展 插件 浏览器 chrome      更新时间:2023-11-21

我正在尝试制作一个元素高亮铬扩展。工作流程:-单击浏览器图标-点击页面-高亮元素点击

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

拒绝执行内联脚本,因为它违反了以下内容内容安全策略指令:"script src'self'chrome扩展资源:"(popup.html:5).

popup.html中的内联脚本在哪里,而该脚本在中不起作用

我有:

manifest.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>

如有任何帮助,我们将不胜感激

在中看到错误之前,我无法正确读取错误

显然manifestv2不允许您拥有内联脚本,所以您只需要

src="path_to_the_file.js"

扩展到@tak3r的回答和@Doug的评论:

内联脚本需要更改为外部脚本。

移动:

<script>
  chrome.tabs.executeScript(null,{
    code:"document.body.style.backgroundColor='red'"
  });
</script>

到一个名为main.js的新文件并删除<script></script>标签

在HTML 的<head></head>中包括以下内容

<script type="text/javascript" src="main.js"></script>