Javascript 如何防止在chrome扩展中接收响应头时下载
因为这个问题对我不起作用,我现在开始一个新问题。 我使用以下代码根据内容类型标头阻止下载:Javascript 如何防止在chrome扩展中接收响应头时下载,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,因为这个问题对我不起作用,我现在开始一个新问题。 我使用以下代码根据内容类型标头阻止下载: chrome.webRequest.onHeadersReceived.addListener(function(details) { preventDownload = false; details.responseHeaders.push({name:"X-Content-Options",value: "no-sniff"}); // Hack 1 details.stat
chrome.webRequest.onHeadersReceived.addListener(function(details) {
preventDownload = false;
details.responseHeaders.push({name:"X-Content-Options",value: "no-sniff"}); // Hack 1
details.statusLine = "HTTP/1.1 302 Moved Temporarily"; // Hack 2
for (var i = 0; i < details.responseHeaders.length; ++i)
{
if (details.responseHeaders[i].name == 'Content-Type')
{
var contentType = details.responseHeaders[i].value;
if (contentType.indexOf("application/xyz")!=-1)
{
preventDownload = true;
details.responseHeaders[i].value = 'text/plain'; //Hack 3
}
else
{
return {responseHeaders: details.responseHeaders};
}
}
}
if(preventDownload)
{
if (details.frameId === 0) // Top frame, yay!
{
var scheme = /^https/.test(details.url) ? "https" : "http";
chrome.tabs.update(details.tabId, {
url: scheme + "://robwu.nl/204"});
return; //return {cancel: true}; should be used but it displays block page
}
return {cancel: true};
}
return {responseHeaders: details.responseHeaders};
}, {urls: ["<all_urls>"],types: ["main_frame", "sub_frame"]}, ['blocking', 'responseHeaders']);
chrome.webRequest.onHeadersReceived.addListener(函数(详细信息)){
preventDownload=false;
details.responseHeaders.push({name:“X-Content-Options”,value:“no sniff”});//Hack 1
details.statusLine=“HTTP/1.1 302临时移动”;//Hack 2
对于(变量i=0;i
我成功阻止了下载,但出现了一个错误的Web阻止页面。我需要让用户保持在上一页,而不重新加载显示此错误页面,或者在显示此服务页面后以某种方式从该页面移回
我在上面的代码中使用了一个hack,但它不会阻止下载。
details
是提供给您的扩展的一个对象,其中包含有关请求的信息。更改其值实际上对请求没有任何影响
Chrome 35.0.1911.0,您只需重定向到一个回复状态代码为204的页面,即可防止卸载上一页:
chrome.webRequest.onHeadersReceived.addListener(function(details) {
// ... your code that checks whether the request should be blocked ...
// (omitted for brevity)
var scheme = /^https/.test(details.url) ? "https" : "http";
return {redirectUrl: scheme + "://robwu.nl/204" };
}, {
urls: ["<all_urls>"],
types: ["main_frame", "sub_frame"]
}, ["responseHeaders", "blocking"]);
注意:这是一个黑客方法。您可以随意将其用于个人用途,但请不要将此类扩展上传到Chrome Web Store。似乎使用“详细信息”对象对响应标题进行任何更改都不会产生任何效果。谢谢,它是现成的。为什么更改状态行没有任何效果。此外,我们什么时候可以期待OnHeadersReceived事件的重定向URL支持,这样我们就不需要上述攻击了。@adnankamili如前所述,
details
只是提供信息而已。Chrome扩展API中的几乎每个参数都只是一个普通的JavaScript对象。我希望OnHeadersRedirectURL可以在Chrome35或36中登陆(请关注进度)。Chrome的发布周期为6周,从canary/dev->beta->stable开始。因此,即使补丁在今天登陆,它也需要7-12周才能被广泛使用。如果您想更快地获得功能,只需从beta或dev频道()安装Chrome即可。通过在OnHeaderReceived事件中注入内容脚本可以实现上述目的。@adnankamili“上述”是什么意思?@adnan只有一小部分请求详细信息可以更改,有关详细信息,请参阅的文档。至于另一个问题:试一试。。。
chrome.webRequest.onHeadersReceived.addListener(function(details) {
// ... your code that checks whether the request should be blocked ...
// (omitted for brevity)
if (details.frameId === 0) { // Top frame, yay!
// Prevent current page from unloading:
var scheme = /^https/.test(details.url) ? "https" : "http";
chrome.tabs.update(details.tabId, {
url: scheme + "://robwu.nl/204"
});
// Prevent file from being downloaded via the headers:
var responseHeaders = details.responseHeaders.filter(function(header) {
var name = header.name.toLowerCase();
return name !== 'content-type' &&
name !== 'x-content-type-options' &&
name !== 'content-disposition';
}).concat([{
// Change content type to something non-downloadable
name: 'Content-Type',
value: 'text/plain'
}, {
// Disable MIME-type sniffing:
name: 'X-Content-Type-Options',
value: 'nosniff'
}]);
return {
responseHeaders: responseHeaders
};
}
// else not the top frame...
return {cancel: true};
}, {
urls: ["<all_urls>"],
types: ["main_frame", "sub_frame"]
}, ["responseHeaders", "blocking"]);