-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
188 lines (171 loc) · 5.87 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Copyright 2023. Jefferson "jscher2000" Scher. License: MPL-2.0.
v0.5 - initial design
v0.5.1 - updated icons
v0.6 - pesky commas...
v0.7 - new menu; option to also not accept AVIF
v0.8 - ability to exempt a site and embedded/linked files
v0.8.1, v0.8.2 - bug fix
v0.9 - automatic deconfliction
*/
let nowlistening; // used for toggling listener
/**** Initialize Preferences ****/
// Default starting values
var oPrefs = {
toBlock: [
"image/webp"
],
exemptSites: [
"www.patreon.com",
"patreon.com"
]
}
// Update oPrefs from storage
browser.storage.local.get("prefs").then( (results) => {
if (results.prefs != undefined){
if (JSON.stringify(results.prefs) != '{}'){
var arrSavedPrefs = Object.keys(results.prefs);
for (var j=0; j<arrSavedPrefs.length; j++){
oPrefs[arrSavedPrefs[j]] = results.prefs[arrSavedPrefs[j]];
}
}
}
// initialize
if (oPrefs.toBlock.length > 0) startListening();
}).catch((err) => {console.log('Error retrieving "prefs" from storage: '+err.message);});
// Build deconfliction list and set up related extension management events (version 0.9)
deconExtList = [
{
id: "{4c421bb7-c1de-4dc6-80c7-ce8625e34d24}",
name: "Load Reddit Images Directly",
hosts: ["i.redd.it", "external-preview.redd.it", "preview.redd.it"]
}
];
deconActive = [];
function deconChecker(extInfo){
var dPat = deconExtList.find(objExt => objExt.id === extInfo.id);
if (dPat == undefined) return;
var indx = deconActive.indexOf(dPat.id);
if (extInfo.enabled == true && indx > -1) return; // no action needed
if (extInfo.enabled != true && indx <= -1) return; // no action needed
if (extInfo.enabled == true && indx <= -1) deconActive.push(dPat.id); // add to active list
if (extInfo.enabled != true && indx > -1) deconActive.splice(indx, 1); // remove from active list
}
browser.management.getAll().then((arrExtInfo) => {
for (var j=0; j<arrExtInfo.length; j++){
deconChecker(arrExtInfo[j]);
}
});
browser.management.onInstalled.addListener(deconChecker);
browser.management.onEnabled.addListener(deconChecker);
browser.management.onDisabled.addListener(deconChecker);
browser.management.onUninstalled.addListener(deconChecker);
/**** Clean Accept Headers of Intercepted Requests ****/
function cleanAccept(details) {
// v0.8 exit for hostnames on the exemptSites list [FUTURE VERSION: allow partial matches?]
var reqUrl = new URL(details.url);
if (oPrefs.exemptSites.length > 0){
if (oPrefs.exemptSites.includes(reqUrl.hostname)) return { requestHeaders: details.requestHeaders };
if (details.documentUrl != null && details.documentUrl != undefined){
var docUrl = new URL(details.documentUrl);
if (oPrefs.exemptSites.includes(docUrl.hostname)) return { requestHeaders: details.requestHeaders };
}
if (details.originUrl != null && details.originUrl != undefined){
var origUrl = new URL(details.originUrl);
if (oPrefs.exemptSites.includes(origUrl.hostname)) return { requestHeaders: details.requestHeaders };
}
}
// v0.9 exit for hostnames on the deconfliction list
for (var i=0; i<deconActive.length; i++){
var deconObj = deconExtList.find(objExt => objExt.id === deconActive[i]);
var deconHosts = deconObj.hosts;
if (deconHosts.includes(reqUrl.hostname)){
console.log('Deconfliction in effect for ' + reqUrl.hostname + '; SKIPPING!');
return { requestHeaders: details.requestHeaders };
}
}
// examine and modify the Accept header if present
for (let header of details.requestHeaders) {
if (header.name.toLowerCase() === 'accept'){
for (var i=0; i<oPrefs.toBlock.length; i++){
let pos = header.value.toLowerCase().indexOf(oPrefs.toBlock[i] + ',');
if (pos > -1){ // remove this content type with trailing comma
header.value = header.value.slice(0, pos) + header.value.slice(pos + 11);
}
pos = header.value.toLowerCase().indexOf(oPrefs.toBlock[i]);
if (pos > -1){ // remove this content type
header.value = header.value.slice(0, pos) + header.value.slice(pos + 10);
}
pos = header.value.indexOf(',,');
if (pos > -1){ // clean up any double comma
header.value = header.value.slice(0, pos) + header.value.slice(pos + 1);
}
}
break;
}
}
// dispatch headers, we're done
return { requestHeaders: details.requestHeaders };
}
/**** Set up and Tear Down webRequest listener ****/
function startListening(){
browser.webRequest.onBeforeSendHeaders.addListener(
cleanAccept,
{ urls: ["<all_urls>"] },
["blocking", "requestHeaders"]
);
nowlistening = true;
// Update toolbar button
setButton();
}
function stopListening(){
browser.webRequest.onBeforeSendHeaders.removeListener(cleanAccept);
nowlistening = false;
// Update toolbar button
setButton();
}
/**** Handle Icon Changes ****/
// when a window is focused, make sure it has the correct button
browser.windows.onFocusChanged.addListener((wid) => {
setButton();
});
// Update icon image and tooltip
function setButton(){
if (nowlistening){
browser.browserAction.setIcon({
path: {
"128": "icons/dont-accept-webp2.png"
}
});
} else {
browser.browserAction.setIcon({
path: {
"128": "icons/do-accept-webp2.png"
}
});
}
}
/**** MESSAGE HANDLER ****/
// Handle Popup menu actions
function handleMessage(request, sender, sendResponse) {
if ('prefs' in request) {
// Return oPrefs to popup
sendResponse({
prefs: oPrefs
});
return true;
} else if ('prefupdate' in request){
// Take what the popup sends us, assume it's valid
oPrefs = request.prefupdate;
// Update storage (async is fine)
browser.storage.local.set({prefs: oPrefs})
.catch((err) => {console.log('Error on browser.storage.local.set(): '+err.message);});
// Update listener if needed
if (nowlistening == true && oPrefs.toBlock.length === 0) { // Stop listening
stopListening();
} else if (nowlistening == false && oPrefs.toBlock.length > 0) { // Start listening
startListening();
}
}
}
browser.runtime.onMessage.addListener(handleMessage);