开始了:
我是 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 形式。
请帮我找到这个问题的解决方案。
这是我在工作中的扩展示例:
<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/