-
Notifications
You must be signed in to change notification settings - Fork 1
/
never-download.js
53 lines (46 loc) · 1.53 KB
/
never-download.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
const ATTACHMENT_REGEX = /^attachment/;
const EXTENSION_REGEX = /\.(gif|jpe?g|png|pdf|txt)$/;
const FILENAME_REGEX = /filename[^;=\n]*=(('|")(.*?)\2|[^;\n]*)/;
const TYPE_REGEX = /^(image|text)|\bpdf\b/;
function headerListener(e) {
let h = e.responseHeaders;
let type = h.find(h => h.name.toLowerCase() == 'content-type');
let disp = h.find(h => h.name.toLowerCase() == 'content-disposition');
let filename = disp && disp.value.match(FILENAME_REGEX);
filename = filename && filename[3];
let ext = filename && filename.match(EXTENSION_REGEX)
|| e.url.match(EXTENSION_REGEX);
ext = ext && ext[1];
// If the content disposition forces download, rewrite that.
if (disp && disp.value.match(ATTACHMENT_REGEX)) {
if ((type && type.value.match(TYPE_REGEX))
|| (filename && filename.match(FILENAME_REGEX))
|| (ext)
) {
disp.value = disp.value.replace(ATTACHMENT_REGEX, ' inline');
}
}
// If the MIME type forces download, rewrite that.
if (type && type.value.startsWith('application/octet-stream')) {
let m = e && e.url && e.url.match(EXTENSION_REGEX);
switch (ext) {
case 'gif':
case 'jpg':
case 'jpeg':
case 'png':
type.value = 'image/' + ext;
case 'pdf':
type.value = 'application/pdf';
break;
case 'txt':
type.value = 'text/plain';
break;
}
}
return {responseHeaders: h};
}
browser.webRequest.onHeadersReceived.addListener(
headerListener,
{'urls': ['<all_urls>']},
['blocking', 'responseHeaders']);