gpt4 book ai didi

javascript - Chrome 扩展内容安全策略指令错误

转载 作者:技术小花猫 更新时间:2023-10-29 12:01:51 24 4
gpt4 key购买 nike

我正在尝试制作 radio 流 chrome 扩展,但出现问题。当我像普通 JS+HTML+CSS 一样在浏览器中运行我的脚本时它可以工作,但是当我尝试像 Chrome 扩展一样运行它时我得到这个错误:

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

之后我将它添加到我的 list 中:

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"

但在那之后我收到错误消息(上面代码的 list 行中的错误)


这是我的 list :

{
"background": {
"scripts": [ "jquery.js", "jquery-ui.js", "plate.js" ]
},

"browser_action": {
"default_icon": "Images/action-normal.png",
"default_popup": "player.html",
"default_title": ""
},
"description": "Chrome Player",
"manifest_version": 2,
"name": "Radio Chrome Player",
"permissions": [ "http://www.radio-station.com/" ],
"version": "1.0"

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"

}

这是主要的 html 文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="jquery.js"></script>
<script src="jquery-ui.js"></script>
<script src="main.js"></script>
<script>$(function(){$("#radioplayere").plate({playlist: [{file:"http://RADIO_STATION_STREAM_URL/;"}], phpGetter: "http://hostingshoutcast.com/stream/plate/php/plate.php"});});</script>
</head>
<body>
<div id="radioplayer">If you are seeing this then an error has occurred!</div>
</body>
</html>

最佳答案

您的问题如下:

  1. Chrome CSP forbids inline code ,这不会被覆盖。您的 'unsafe-eval' 没有解决问题,而 'unsafe-inline' 会有所帮助 will be ignored通过 Chrome。

    您需要摆脱内联代码:

    <script>$(function(){$("#radioplayere").plate({playlist: [{file:"http://RADIO_STATION_STREAM_URL/;"}], phpGetter: "http://hostingshoutcast.com/stream/plate/php/plate.php"});});</script>

    这需要在js文件中移动。

  2. 您的 manifest.json 中有一个拼写错误,您忘记了一个逗号:

    "version": "1.0",

    一般来说,使用 JSON validator可以帮助您捕获这些错误。

关于javascript - Chrome 扩展内容安全策略指令错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23216884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com