Chrome扩展在浏览器动作中插入内容脚本 [英] chrome extension insert content script on browser action

查看:587
本文介绍了Chrome扩展在浏览器动作中插入内容脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使基本元素荧光笔铬扩展。
工作流程:
- 点击浏览器图标
- 点击页面
- 点击元素点击



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


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


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



I有:

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:// * / *
],
版本:0.1
}

弹出。 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>

任何帮助都将非常值得赞赏

解决方案

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



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

  src =path_to_the_file.js


I am trying to make basically an element highlighter chrome extension. Workflow: - click on browser icon - click on the page - hightlight the element clicked

I am having troubles in running content scripts upon browser action using manifest_version:2 When I inspect the popup that appears it says:

Refused to execute inline script because it violates the following Content Security Policy directive: "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

I have:

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>

Any help would be very much appreciated

解决方案

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

src="path_to_the_file.js"

这篇关于Chrome扩展在浏览器动作中插入内容脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆