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

标签 google-chrome google-chrome-extension content-security-policy manifest.json

开始了:
我是 google 的 chrome 扩展开发的新手,所以请多多包涵。

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

Refused to frame 'https://api.xxx.jp/' because it violates the following Content Security Policy directive: "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 twitter: 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,
}

在我的 content.js 文件中,我通过 iframe 标签内部调用 api:
<iframe src="'+url+'" name="xxxExtensionsFrame" width="380" height="' + (heightBase - 5) + '" border="0" frameborder="0" scrolling="no"></iframe>

api 的 url 始终为 https 形式。
  • 此扩展程序适用于大多数网站,但适用于某些网站,例如 https://twitter.com/?lang=en ,它会显示一个带有上述错误消息的灰色弹出框。

  • Gray pop up box
    请帮我找到这个问题的解决方案。

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

    <video class="image-viewer horizontal" poster="https://thumb.gyazo.com/thumb/642_w/_262d5667a035ff8505079ce6994d3c3f-gif.jpg" autoplay="" playsinline="" loop="" style="max-width: 642px; max-height: 100%;"><source src="https://i.gyazo.com/90701bdda37df8282699208efaa215a5.mp4" type="video/mp4"></video>


    欢迎任何帮助。

    最佳答案

    Twitter 使用 Content-Security-Policy标题。您问题的唯一解决方案是使用 chrome.webRequest 修改响应 header 后台脚本中的 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"]);
    

    示例摘自我的博文 here .

    关于google-chrome - 拒绝框架 'https://api.xxx.jp/' 因为它违反了以下内容安全策略指令 : "frame-src ' self',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48412720/

    相关文章:

    python - 如何使用 Python 访问 Chrome Devtools 网络选项卡中的值?

    javascript - Chrome 扩展程序 : Clicking a button every X seconds

    content-security-policy - 当内容安全策略到位时,如何显示视频内容?

    asp.net - ASP.NET WebForms 中的内容安全策略

    javascript - 向浏览器请求模态以选择证书

    javascript - 在 iphone chrome 浏览器中不使用 BLOB 对象下载文件

    google-chrome-extension - 解压后的扩展程序的 Chrome 扩展程序 ID 是如何生成的?

    javascript - 如何在没有不安全的内联 JavaScript/CSS 代码的情况下使用 React?

    javascript - 检查是否安装了扩展,如果用户删除扩展则关闭选项卡

    javascript - 将 'unsafe-eval' 添加到现有的 Chrome 扩展