由于内容安全策略,谷歌地图api脚本确实加载

我正在制作谷歌浏览器扩展程序,我想使用谷歌地图。 问题是当我运行我的脚本然后它给了我这个错误

Refused to load script from 'https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXXXXXXX&sensor=false' because of Content-Security-Policy. 

这是我的清单文件

 { "name": "Name", "version": "1.0", "manifest_version": 2, "background": { "scripts": [ "js/script.js" ] }, "description": "Desc", "browser_action": { "default_icon": "images/icon.png", "default_title": "Title", "default_popup": "html/popup.html" }, "permissions": [ "http://*/", "http://*.google.com/", "http://localhost/*" ], "content_security_policy": "script-src 'self' http://google.com; object-src 'self'" 

}

我正在添加这样的脚本

     

为什么我一次又一次地收到这个错误? 请帮忙…

更新一个

我将这两个权限添加到清单文件但仍无效

 "https://maps.google.com/*", "https://maps.googleapis.com/*", 

更新两个

我也使用了这种content_security_policy

 "content_security_policy": "default-src 'none'; style-src 'self'; script-src 'self'; connect-src https://maps.googleapis.com; img-src https://maps.google.com" 

但上面对我来说也不起作用

我认为这里的问题是您没有正确设置Google地图url的内容安全政策。 您应该将清单文件中的“content_security_policy”更改为以下内容:

 "content_security_policy": "script-src 'self' https://maps.googleapis.com https://maps.gstatic.com; object-src 'self'" 

这只是意味着您允许从自我/当前页面以及“https://maps.googleapis.com”运行脚本。

试试这个,看看它是否有帮助。

我遇到了同样的问题,并通过将API从http替换为https版本来解决。

在HTML中:

  

至:

  

然后将https://maps-api-ssl.google.com添加到manifest.json中的CPS

我不知道你是否还需要这个信息。 但我在谷歌上搜索并花了一些时间但找不到直接的答案,所以我在这里写道,希望它能帮助任何人。

内容安全策略可以保护您免受XSS攻击。 但这意味着您需要明确地将外部资源列入白名单 。 您可以通过提供额外的HTTP标头或标签来实现: