无法从Chrome扩展程序加载Facebook JS SDK

4

我正在尝试在Chrome扩展程序中使用Facebook SJ 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:"。 我在清单文件中设置了权限http://*/。如何从扩展程序中加载SDK?
1个回答

6

这是因为您正尝试从Chrome扩展中加载外部脚本。由于安全原因,以防止跨站脚本攻击,您需要在manifest.json文件中获得特殊权限。

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

请确保使用https://而不是http://。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接