-
Notifications
You must be signed in to change notification settings - Fork 7
/
background.js
69 lines (60 loc) · 1.72 KB
/
background.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
let redirectToOriginalImageEnabled = false;
let useOldAccept = false;
function redirect(details) {
const url = new URL(details.url);
const targetURL = url.searchParams.get("url");
if (targetURL) {
return {
redirectUrl: targetURL
};
}
}
function redirectToOriginalImage(details) {
if (redirectToOriginalImageEnabled) {
var url = new URL(details.url);
url.hostname = "i.redd.it";
return {redirectUrl: url.toString()};
}
}
function modifyAcceptHeader(details) {
if (useOldAccept) {
acceptValue = "image/png,image/*;q=0.8,*/*;q=0.5";
} else {
acceptValue = "image/avif,image/webp,*/*"
}
let newHeaders = details.requestHeaders.map(header => {
if (header.name.toLowerCase() === 'accept') {
return {
name: header.name,
value: acceptValue
};
}
return header;
});
return { requestHeaders: newHeaders };
}
checkSetting();
function checkSetting() {
browser.storage.local.get('redirectToOriginalImage', function(result) {
redirectToOriginalImageEnabled = result.redirectToOriginalImage
});
browser.storage.local.get('useOldAccept', function(result) {
useOldAccept = result.useOldAccept
});
}
browser.storage.onChanged.addListener(checkSetting);
browser.webRequest.onBeforeRequest.addListener(
redirect,
{ urls: ["*://www.reddit.com/media*"], types: ["main_frame"] },
["blocking"]
);
browser.webRequest.onBeforeRequest.addListener(
redirectToOriginalImage,
{ urls: ["*://preview.redd.it/*"], types: ["main_frame"] },
["blocking"]
);
browser.webRequest.onBeforeSendHeaders.addListener(
modifyAcceptHeader,
{ urls: ['*://i.redd.it/*', '*://external-preview.redd.it/*', '*://preview.redd.it/*'] },
['blocking', 'requestHeaders']
);