通过Chrome扩展程序阻止按内容类型下载 [英] Block downloading by Content-Type via Chrome Extension

查看:122
本文介绍了通过Chrome扩展程序阻止按内容类型下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个阻止按文件的Content-Type进行下载的扩展。这是处理标题的后台脚本的一部分:

  chrome.webRequest.onHeadersReceived.addListener(function(details){
var headers = details.responseHeaders;

for(var i = 0,l = headers.length; i< l; ++ i){
if(headers [i] .name ==Content-Type&& headers [i] .value ==< some_type>){
return {cancel:true};
}
}
},{urls:[< all_urls>]},[responseHeaders,blocking]);

我得到错误信息页面:

解决方案

如果您希望防止根据内容类型在(i)框架中导航,那么你的运气不佳 - 目前无法阻止页面卸载。



<如果你想阻止顶层框架离开,那么就有希望了。您可以将页面重定向到使用HTTP状态码204回复的资源。


$ b

  chrome.webRequest .onHeadersReceived.addListener(function(details){
// ...检查请求是否被阻止的代码...

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};
},{
urls:[< all_urls>],
types:[main_frame,sub_frame]
},[responseHeaders,blocking]) ;

一旦 issue 280464 已解决,以前的方法也可以用来防止子帧中的卸载。

https://robwu.nl/204 是我网站的一部分。访问此URL不记录。对此条目的回应将始终为204无内容。


I am developing an extension for blocking downloading by file's Content-Type. This is part of background script which handle headers receiving:

chrome.webRequest.onHeadersReceived.addListener(function(details) {
    var headers = details.responseHeaders;

    for(var i = 0, l = headers.length; i < l; ++i) {
        if(headers[i].name == "Content-Type" && headers[i].value == "<some_type>") {
            return {cancel: true};
        }
    }
}, {urls: ["<all_urls>"]}, ["responseHeaders", "blocking"]);

And I get page with error message:

So I need some solution to keep user on previous page without reloading to displaying this error page OR somehow move back from this service page after it displayed.

解决方案

If you wish to prevent navigating away in an (i)frame based on the content type, then you're out of luck - it is not possible to prevent a page from unloading at this point.

If you want to prevent the top-level frame from navigating away, then there's hope. You can redirect the page to a resource that replies with HTTP status code 204.

chrome.webRequest.onHeadersReceived.addListener(function(details) {
    // ... your code that checks whether the request should be blocked ...

    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};
}, {
    urls: ["<all_urls>"],
    types: ["main_frame", "sub_frame"]
}, ["responseHeaders", "blocking"]);

Once issue 280464 is solved, the previous method can also be used to prevent unloads in subframes.

https://robwu.nl/204 is part of my website. Access to this URL is not logged. The response to this entry will always be "204 No Content".

这篇关于通过Chrome扩展程序阻止按内容类型下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆