我在 chrome 的开发者工具控制台中多次遇到错误,用于 maps.googleapis.com 的 common.js 文件-
common.js:15
Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self' https://fonts.googleapis.com https://s3.amazonaws.com https://maxcdn.bootstrapcdn.com". Either the 'unsafe-inline' keyword, a hash ('sha256-mmA4m52ZWPKWAzDvKQbF7Qhx9VHCZ2pcEdC0f9Xn/Po='), or a nonce ('nonce-...') is required to enable inline execution.
我需要使用严格的 CSP 政策,所以不能使用 unsafe-inline 或 unsafe-eval 来放松政策。为了支持严格的 CSP 策略,不允许使用内联样式和脚本。并且似乎在 google map api 的 common.js 中使用了内联样式,因此我收到了上述错误。
~ 目前谷歌地图要求您在 CSP 中为
unsafe-inline
提供style-src
。对于script-src
,它仍然有效。~~~~
对上述CSPlite.com granty的测试进行了调整;在撰写此答案时 the test says
我找到的临时解决方案是将 Google Maps 中的所有样式添加到我们的第三方 CSS 代码中。这样,您仍然会在控制台中遇到错误,但是由于
style-src
阻止来自 Google 地图的内联样式而导致的视觉错误将会消失。我们使用的方法是复制谷歌地图添加的所有内联样式。~
~如果你找到了这个答案,并且还希望谷歌地图再次支持将 nonce 值分配到它们的内联样式块, please leave a comment here 。~
2021 年 6 月 22 日编辑:
GMap 还没有正式支持随机数。最近发生了一项更改,改进了 Maps JS 处理脚本和样式的单独随机数的能力。现在,如果一个站点在
<style>
或<link rel="stylesheet">
上没有随机数,这将导致没有随机数应用于 Maps JS 样式表。作为一种解决方法,您的站点可以包含一个空的
<style>
和提供的随机数,GMaps JS 会选择它。