4

我想在Chrome擴展中使用facebook sj sdk。 Im做這在我的擴展初始化:無法加載從Chrome擴展的Facebook js sdk

window.fbAsyncInit = function() { 
     // init the FB JS SDK 
     FB.init({ 
      appId: 'APP_ID', // App ID from the App Dashboard 
      //channelUrl : '//www.example.com/', // Channel File for x-domain communication 
      status: true, // check the login status upon init? 
      cookie: true, // set sessions cookies to allow your server to access the session? 
      xfbml: true // parse XFBML tags on this page? 
     }); 

     // Additional initialization code such as adding Event Listeners goes here 
     console.log(FB); 

    }; 

    // Load the SDK's source Asynchronously 
    (function(d, debug) { 
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; 
     if (d.getElementById(id)) { 
      return; 
     } 
     js = d.createElement('script'); 
     js.id = id; 
     js.async = true; 
     js.src = "http://connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js"; 
     ref.parentNode.insertBefore(js, ref); 
    }(document, /*debug*/false)); 

我得到這個錯誤:

拒絕加載腳本 'http://connect.facebook.net/en_US/all.js'因爲它違反了以下內容安全策略指令:「script-src」self'chrome-extension-resource:「。

我將清單中的permissiosn設置爲http://*/。我如何從擴展中加載sdk?

回答

6

這是因爲您試圖從Chrome擴展程序加載外部腳本。出於安全原因,爲防止跨站點腳本,您需要在manifest.json文件中獲得特別許可。

"content_security_policy": "script-src 'self' https://connect.facebook.net; object-src 'self'" 

請確保您使用https://而不是http://。

相關問題