【问题标题】:Can't load script 'Content-Security-Policy' for chrome newtab extension with variable script address无法为具有可变脚本地址的 chrome newtab 扩展加载脚本“Content-Security-Policy”
【发布时间】:2019-01-07 07:12:19
【问题描述】:

我正在制作一个 chrome newtab 扩展,通过查询雅虎天气来加载用户天气。我收到来自 chrome 的“Content-Security-Policy”违规,说我无法加载脚本:

拒绝执行内联脚本,因为它违反了以下内容 内容安全策略指令:“script-src 'self' blob: 文件系统:chrome-extension-resource:”。要么是“不安全的内联” 关键字,哈希 ('sha256-PeWalvgfJE6xbsZk1lp14cxuyPBUbuIbzFNlAxarXxU='),或随机数 ('nonce-...') 是启用内联执行所必需的。

这是我的清单:

{
    "name": "WeatherTodo",
    "version": "1.0",
    "description": "Extension that shows a weather animation and todolist",
    "manifest_version": 2,
    "permissions": ["storage"],
    "chrome_url_overrides": {
        "newtab": "index.html"
    }
}

问题在于查询是不同的 URL,具体取决于用户所在的位置,因此我不确定是否可以将其作为安全链接添加到清单。请帮忙。

我认为这是导致错误的行,我不确定,因为 chrome 指向具有我的脚本标记的 html 行:

fetch("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%3D%22(" 
                + position.coords.latitude + "%2C" + position.coords.longitude 
                + ")%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")

【问题讨论】:

标签: javascript html google-chrome google-chrome-extension yahoo-api


【解决方案1】:

您不能在 HTML 中使用内联脚本,因为它很容易发生脚本注入,因此 Chrome 对此进行了限制。你可以阅读Content Security Policy (CSP)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-21
    • 2020-04-20
    • 2012-05-25
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-01
    相关资源
    最近更新 更多