对于一个非常具体的任务,我需要获取具有不同域的from
的内容。该框架指向我无法控制标头的外部域。
所以我想出了一个chrome扩展来绕过外域的起源策略。这是扩展名:
iframe
manifest.json:
{
...
"background": {
"scripts": ["background.js"]
},
"permissions": ["webRequest", "webRequestBlocking", "<all_urls>"]
}
background.js:
使用扩展程序,我能够构建不允许在框架内显示的页面。但另一方面,var strip = [
"content-security-policy",
"x-frame-options",
"access-control-allow-origin",
"x-content-type-options"
];
chrome.webRequest.onHeadersReceived.addListener(
function(details) {
var headers = details.responseHeaders.filter(function(header) {
return strip.indexOf(header.name.toLowerCase()) < 0;
});
headers.push({
name: "Access-Control-Allow-Origin",
value: "*"
});
return {
responseHeaders: headers
};
},
{
urls: ["<all_urls>"]
/* types: [ // default: all types
"main_frame",
"sub_frame",
"stylesheet",
"script",
"image",
"object",
"xmlhttprequest",
"other"
] */
},
["blocking", "responseHeaders"]
);
根本不会对iframe生效。如果我使用扩展程序运行Access-Control-Allow-Origin
,我仍然可以
$('iframe').contents();
这是一个错误吗?