我正在研究谷歌瀏覽器擴展程序,其中應包括Facebook的sdk.js,但問題是我無法使應用程序工作,因爲內容安全策略..這是我的一部分代碼:谷歌瀏覽器擴展程序內容安全政策
的manifest.json
{
"manifest_version": 2,
"name": "<app-name>",
"description": "<description>",
"version": "0.1",
"permissions": [
"tabs","<all_urls>"
],
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": ["intercept_connections.js"]
}
],
"content_security_policy": "script-src 'self' https://connect.facebook.net/en_US/ 'unsafe-eval'; object-src 'self'",
"background": {
"scripts" : ["background.js"]
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
popup.html
<html>
<head>
<title>Facebook Mass Sharer</title>
<script type="text/javascript" src="popup.js"></script>
<script type="text/javascript" src="FbApi.js"></script>
</head>
<body>
<body>
<h1>Post to Facebook</h1>
<button id="postTest">TestMe</button>
</body>
</body>
</html>
popup.js
個var handleClick = function() {
postToGroup(<groupID>);
}
var initialize = function() {
var test = document.getElementById('postTest');
test.addEventListener("click",handleClick);
}
window.addEventListener("load", initialize);
FbApi.js(我做的)
window.onload = function() {
window.fbAsyncInit = function() {
FB.init({
appId : '<appID>',
xfbml : true,
version : 'v2.1',
oauth : true
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
}
var postToGroup = function(groupID) {
FB.api(
"/"+groupID+"/feed",
"POST",
{
"message": "This is a test message"
},
function (response) {
if (response && !response.error) {
/* handle the result */
}
}
);
}
事情是我不斷收到此錯誤:
Refused to execute JavaScript URL because it violates the following Content Security Policy directive: "script-src https:// 'unsafe-eval'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.
我試圖改變"content_security_policy": "script-src 'self' https://connect.facebook.net/en_US/ 'unsafe-eval'; object-src 'self'"
到"content_security_policy": "script-src 'self'; object-src 'self'"
或 「content_security_policy」:"script-src 'self' https://connect.facebook.net/en_US/ 'inline-eval'; object-src 'self'"
爲在錯誤中建議,但沒有運氣。我應該嘗試什麼?
你能看到什麼是拋出該錯誤?這聽起來像Facebook可能在某處使用eval。 – jmort253 2014-08-31 20:34:21
在Facebook的API使用eval的一次有......看看[https://connect.facebook.net/en_US/sdk.js](here)..Just搜索EVAL,你會發現它一個time..But我正在使用'unsafe-eval',所以我不知道我的錯誤在哪裏 – csanonymus 2014-08-31 20:42:53
我很確定Google認爲eval不安全。我不認爲你可以重寫它。您需要做的是使用該內容的iframe,以便無法訪問任何Chrome API。然後,您需要使用postMessage在iframe和擴展的主要部分之間進行通信。 – jmort253 2014-08-31 20:44:39