拒绝框架 'https://api.xxx.jp/' 因为它违反了以下内容安全策略指令:“frame-src 'self'

Jos*_*pes 5 google-chrome google-chrome-extension content-security-policy manifest.json

我们开始吧:我是谷歌 chrome 扩展开发的新手,所以请耐心等待。

我有一个扩展程序,它给了我以下错误:

拒绝框架 ' https://api.xxx.jp/ ' 因为它违反了以下内容安全策略指令:“frame-src 'self' https://staticxx.facebook.com https://twitter.com https: //*.twimg.com https://5415703.fls.doubleclick.net https://player.vimeo.com https://pay.twitter.com https://www.facebook.com https://ton .twitter.com https://syndication.twitter.com https://vine.co推特:https : //www.youtube.com https://platform.twitter.com https://upload.twitter.com https ://s-static.ak.facebook.com https://4337974.fls.doubleclick.net https://8122179.fls.doubleclick.net https://donate.twitter.com”。

我的manifest.json文件具有以下关于内容安全策略的设置:

{
   "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",
   "manifest_version": 2,
}
Run Code Online (Sandbox Code Playgroud)

在我的content.js文件中,我通过iframe标签内部调用 api :

<iframe src="'+url+'" name="xxxExtensionsFrame" width="380" height="' + (heightBase - 5) + '" border="0" frameborder="0" scrolling="no"></iframe>
Run Code Online (Sandbox Code Playgroud)

api 的url始终为 https 形式。

灰色弹出框 请帮我找到这个问题的解决方案。

这是我在工作中的扩展示例:

{
   "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",
   "manifest_version": 2,
}
Run Code Online (Sandbox Code Playgroud)

欢迎任何帮助。

Usa*_*jaz 5

Twitter 使用Content-Security-Policy标题。您问题的唯一解决方案是使用chrome.webRequest后台脚本中的API修改响应标头。

下面是一个例子:

chrome.webRequest.onHeadersReceived.addListener(info => {
    const headers = info.responseHeaders; // original headers
    for (let i=headers.length-1; i>=0; --i) {
        let header = headers[i].name.toLowerCase();
        if (header === "content-security-policy") { // csp header is found
            // modify frame-src here
            headers[i].value = headers[i].value.replace("frame-src", "frame-src https://domain-you-want-to-iframe.com/");
        }
    }
    // return modified headers
    return {responseHeaders: headers};
}, {
    urls: [ "<all_urls>" ], // match all pages
    types: [ "sub_frame" ] // for framing only
}, ["blocking", "responseHeaders"]);
Run Code Online (Sandbox Code Playgroud)

示例摘自我的博客文章here