![](/img/trans.png)
[英]Chrome extension - getting Content-Type in onBeforeRequest
[英]Block downloading by Content-Type via Chrome Extension
我正在開發一種擴展程序,用於阻止按文件的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進行回復的資源。
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"]);
解決問題280464之后 ,也可以使用先前的方法來防止子幀中的卸載。
https://robwu.nl/204是我的網站的一部分。 未記錄對此URL的訪問。 對該條目的響應將始終為“ 204 No Content”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.