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

问题描述 投票:2回答:1

我们走了:我是谷歌Chrome扩展开发的新手,所以请耐心等待。

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

拒绝框架 'https://api.xxx.jp/',因为它违反了以下内容安全政策指令:“帧的src '自我' 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,
}

在我的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>

欢迎任何帮助。

google-chrome google-chrome-extension content-security-policy manifest.json
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.