我是 Intab 的作者,一个 Chrome 扩展程序,可让您查看内联链接而不是新标签页。幕后并没有太多花哨的东西,它只是一个加载用户点击的 URL 的 iframe。
除了将 X-Frame-Options header 设置为 DENY 或 SAMEORIGIN 的站点外,它工作得很好。一些非常大的网站,如谷歌和 Facebook 都使用它,这使得体验有点卡顿。
有什么办法可以解决这个问题吗?由于我使用的是 Chrome 扩展程序,是否有任何我可以访问的浏览器级别的内容可能有帮助?寻找任何想法或帮助!
最佳答案
Chrome 提供 webRequest
拦截和修改 HTTP 请求的 API。您可以删除 X-Frame-Options
header 以允许在 iframe 中内联页面。
chrome.webRequest.onHeadersReceived.addListener(
function(info) {
var headers = info.responseHeaders;
for (var i=headers.length-1; i>=0; --i) {
var header = headers[i].name.toLowerCase();
if (header == 'x-frame-options' || header == 'frame-options') {
headers.splice(i, 1); // Remove header
}
}
return {responseHeaders: headers};
}, {
urls: [
'*://*/*', // Pattern to match all http(s) pages
// '*://*.example.org/*', // Pattern to match one http(s) site
],
types: [ 'sub_frame' ]
}, [
'blocking',
'responseHeaders',
// Modern Chrome needs 'extraHeaders' to see and change this header,
// so the following code evaluates to 'extraHeaders' only in modern Chrome.
chrome.webRequest.OnHeadersReceivedOptions.EXTRA_HEADERS,
].filter(Boolean)
);
在 list 中,您需要指定webRequest
和webRequestBlocking
权限,以及URLs patterns对于上面的示例,您打算拦截 "*://*/*"
或 "*://www.example.org/*"
。
关于javascript - 在 Chrome 扩展中绕过 X-Frame-Options DENY?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15532791/