【问题标题】:Refused to frame 'https://api.xxx.jp/' because it violates the following Content Security Policy directive: "frame-src 'self'拒绝框架“https://api.xxx.jp/”,因为它违反了以下内容安全策略指令:“frame-src 'self'
【发布时间】:2018-07-02 22:08:00
【问题描述】:

我们开始: 我是 google 的 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 @ 987654326@https://www.facebook.comhttps://ton.twitter.comhttps://syndication.twitter.comhttps://vine.co推特:https://www.youtube.comhttps://platform.twitter.comhttps://upload.twitter.comhttps://s-static.ak.facebook.comhttps://4337974.fls.doubleclick.nethttps://8122179.fls.doubleclick.net987654336https://8122179.fls.doubleclick.netp>

我的 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)中,它会显示一个灰色弹出框,其中包含上述错误消息。

请帮我找到这个问题的解决方案。

这是我的扩展工作示例:

&lt;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%;"&gt;&lt;source src="https://i.gyazo.com/90701bdda37df8282699208efaa215a5.mp4" type="video/mp4"&gt;&lt;/video&gt;

欢迎任何帮助。

【问题讨论】:

  • @noogui 不是重复的,我检查了所有其他解决方案,但没有一个解决框架/iframe 的问题。如果您知道如何解决,请提供具体答案。

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


【解决方案1】:

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"]);

示例摘自我的博文here

【讨论】:

  • @Josimar Lopes 您能否将答案标记为解决方案,这样该问题就不会再出现在未回答的问题中了?
猜你喜欢
  • 2021-05-10
  • 2020-06-23
  • 2021-09-08
  • 1970-01-01
  • 2012-09-17
  • 2013-07-13
  • 1970-01-01
  • 2021-05-29
  • 2016-04-23
相关资源
最近更新 更多