[英]Block downloading by Content-Type via Chrome Extension
I am developing an extension for blocking downloading by file's Content-Type. 我正在开发一种扩展程序,用于阻止按文件的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. 如果您希望防止根据内容类型在(i)框架中导航,那么您就不走运了-目前无法阻止页面卸载。
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. 您可以将页面重定向到使用HTTP状态代码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. 解决问题280464之后 ,也可以使用先前的方法来防止子帧中的卸载。
https://robwu.nl/204 is part of my website. https://robwu.nl/204是我的网站的一部分。 Access to this URL is not logged. 未记录对此URL的访问。 The response to this entry will always be "204 No Content". 对该条目的响应将始终为“ 204 No Content”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.