在我的chrome扩展程序中,当用户单击浏览器操作符号时,会打开一个新的(弹出)窗口,加载Google Map。要打开窗口,请使用以下代码段
chrome.browserAction.onClicked.addListener(function() {
chrome.windows.create({'url': 'popup.html', 'width': 500, 'height': 400 });
});
该popup.html
如下外观:
<!doctype html>
<html>
<head>
<title>Popup</title>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script src="popup.js"></script>
</head>
<body>
<div id="map-canvas" style="width:100%; height:100%"></div>
</body>
</html>
我自然会遇到内容安全策略(CSP)问题。作为解决方案,我找到了这篇Stackoverflow文章。但是,它开箱即用。我得到了进一步的“拒绝加载脚本...” CSP错误消息。只有通过用所有必需的脚本扩展CSP行,我才最终使用以下命令运行它:
"content_security_policy": "script-src 'self' 'unsafe-eval' https://maps.googleapis.com https://maps.gstatic.com https://mts0.googleapis.com https://mts1.googleapis.com; object-src 'self';"
现在,我想知道这是否是正确的方法。我怎么知道所需的https URL列表不会随着时间变化?我尝试使用通配符(https://*/*
),但这完全被炸掉了。或者,也许我完全使用了绞拧方法。
无法保证Google Maps将从哪个域加载脚本。如果您需要使用更宽松的CSP规则,则如下所示:
script-src 'self' 'unsafe-eval' https:;
https:
表示可以从任何域加载,但必须通过SSL加载。这不是理想的CSP规则集,因为它更容易受到JavaScript安全问题的影响。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句