6

我正在製作一個谷歌瀏覽器擴展,我想要使用谷歌地圖。問題是,當我運行我的腳本,然後它給我這個錯誤谷歌地圖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'" 

}

而且我將我的劇本是這樣

<script src="../js/libs/jquery.js"></script> 
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXXXXX&sensor=false"></script> 
    <script src="../js/plugins/easing.js"></script> 
    <script src="../js/script.js"></script> 

爲什麼我一次又一次地得到那個錯誤?請幫助...

更新一個

我加入這兩個權限清單文件,但仍然沒有工作

"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" 

但上述不工作對我來說

回答

15

我認爲這裏的問題是,您沒有正確設置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」。

試試這個,看看它是否有幫助。

+0

謝謝阿克巴爾。我根據你所說的改變了我的清單文件,但是現在我得到這個錯誤'ed從'https://maps.gstatic.com/intl/en_us/mapfiles/api-3/9/19/main加載腳本。 js',因爲Content-Security-Policy.'。任何想法爲什麼? – 2619

+0

我更新了上述內容安全策略行以包含maps.gstatic.com行。我認爲你可能還需要添加更多的relxation政策。爲此,我建議您通過以下內容: http://developer.chrome.com/extensions/contentSecurityPolicy.html http://dvcs.w3。org/hg/content-security-policy/raw-file/tip/csp-specification.dev.html –

+0

感謝您的回答,您是否有任何解決方案將內容庫作爲內容腳本加載到頁面中?我在[這裏]添加了這個問題(https://stackoverflow.com/questions/48937438/refused-to-load-the-script-of-google-maps-library-inside-content-script-of-chrom) – Mohammad

2

我有一個相同的問題,並通過取代從http到https版本的API URL解決。

在HTML 來源:

<script type='text/javascript' src='http://maps.google.com/maps/api/js?v=3.3&sensor=false'></script> 

要:

<script type='text/javascript' src='https://maps-api-ssl.google.com/maps/api/js?v=3.3&sensor=false'></script> 

然後manifest.json中

添加https://maps-api-ssl.google.com到CPS我不知道你是否還需要這些信息。但我在Google上搜索並花費了一些時間,但找不到直接的答案,所以我在這裏寫信,希望能幫助任何人。