-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfrycAPI-background.js
More file actions
292 lines (273 loc) · 10.3 KB
/
frycAPI-background.js
File metadata and controls
292 lines (273 loc) · 10.3 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* eslint-disable no-extra-semi */
"use strict";
const log = console.log;
Object.defineProperty(Object.prototype, "log", {
get() {
log(this);
return this;
},
});
// #region Unused chrome.storage.session wrapper
/* // This someday might be useful...
const varDefaults = { // Global persistent variables. Can't be null or undefined
injectedStyles: {},
test: 0,
};
let readWrite = null;
const varCache = {};
async function getVar(name) {
if (readWrite) await readWrite;
if (varCache.hasOwnProperty(name)) return varCache[name];
readWrite = chrome.storage.session.get(name);
const temp = varCache[name] = (await readWrite)[name] ?? varDefaults[name];
readWrite = null;
return temp;
}
async function setVar(name, value) {
if (readWrite) await readWrite;
varCache[name] = value;
readWrite = chrome.storage.session.set({ [name]: value });
await readWrite;
readWrite = null;
}
async function getSetVar(name, callback) { // callback will get value and should return new value
if (readWrite) await readWrite;
let newVal;
if (varCache.hasOwnProperty(name)) {
newVal = varCache[name] = callback(varCache[name]);
} else {
readWrite = chrome.storage.session.get(name);
newVal = varCache[name] = callback((await readWrite)[name] ?? varDefaults[name]);
}
readWrite = chrome.storage.session.set({ [name]: newVal });
await readWrite;
readWrite = null;
return newVal;
}
*/
/* // Left as a example usage of getSetVar
function defineStyle(data, sender) {
const CSSInjection = {
css: data.style,
target: { tabId: sender.tab.id },
};
getSetVar("injectedStyles", injectedStyles => {
injectedStyles[data.id] = CSSInjection;
return injectedStyles;
});
return CSSInjection;
}
*/
/* // Left as a example usage of getVar
case "injectStyleAgain": return chrome.scripting.insertCSS((await getVar("injectedStyles"))[data.id]); // happens second
case "removeStyle": return chrome.scripting.removeCSS((await getVar("injectedStyles"))[data.id]); // happens second
*/
// #endregion
function CSSInjection(data = {}, sender) {
return {
css: data.style,
target: { tabId: sender.tab.id, allFrames: data.allFrames },
};
}
function blobToBase64(blob) {
const reader = new FileReader();
reader.readAsDataURL(blob);
return new Promise(resolve => {
reader.onloadend = () => {
resolve(reader.result);
};
});
}
function onTabClosed(tabId, callback) {
const listener = closedTabId => {
if (closedTabId !== tabId) return;
chrome.tabs.onRemoved.removeListener(listener);
callback();
};
chrome.tabs.onRemoved.addListener(listener);
}
async function runOnPage(tabId, daFunc, args = []) {
const [{ result }] = await chrome.scripting.executeScript({
world: "MAIN",
target: { tabId },
func: daFunc,
args: args,
});
return result;
} // const out = await runOnPage(sender.tab.id, () => {});
let spotifyAuthorizationResolve, spotifyAuthorizationReject;
const spotifyAuthorization = new Promise((resolve, reject) => {
spotifyAuthorizationResolve = resolve;
spotifyAuthorizationReject = reject;
});
function catchSpotifyRequest(details) {
if (details.method !== "GET") return;
const authorization = details.requestHeaders?.find(
header => header.name.toLowerCase() === "authorization"
)?.value;
if (!authorization) return;
chrome.webRequest.onBeforeSendHeaders.removeListener(catchSpotifyRequest);
spotifyAuthorizationResolve(authorization);
}
chrome.runtime.onMessageExternal.addListener(function ({ name, data }, sender, sendResp) {
// log(name);
// log(data);
(async () => { switch (name) { // eslint-disable-line brace-style
case "closeTab": return void chrome.tabs.remove(sender.tab.id);
case "downloadPDF": {
chrome.downloads.download({ url: data.url }).then(downloadId => {
// log(downloadId);
if (data.czyZamknąć) {
chrome.tabs.remove(sender.tab.id);
} else {
chrome.tabs.goBack(sender.tab.id);
}
});
return;
};
case "downloadURL": return void chrome.downloads.download(data);
case "downloadNotify": return new Promise(resolve => {
let theId;
function onChanged({ id, state }) {
if (id === theId && state?.current !== "in_progress") { // state.current === "interrupted" || state.current === "complete"
chrome.downloads.onChanged.removeListener(onChanged);
resolve();
}
}
chrome.downloads.onChanged.addListener(onChanged);
chrome.downloads.download(data).then(id => { theId = id });
});
case "getImgAsDataUrl": return fetch(data).then(res => res.blob()).then(async blob => "data:" + blob.type + ";" + (await blobToBase64(blob)));
case "injectStyle": return sender.tab ? chrome.scripting.insertCSS(CSSInjection(data, sender)) : 0;
case "removeStyle": return sender.tab ? chrome.scripting.removeCSS(CSSInjection(data, sender)) : 0;
case "setStorage": return chrome.storage.sync.get(data.UUID).then(result => {
const obj = result[data.UUID];
if (obj) {
let objChanged = false;
for (const [key, value] of Object.entries(data.obj)) {
// hasOwnProperty is necessary to catch possibility of defining a new key with value undefined
if (obj[key] !== value || !obj.hasOwnProperty(key)) objChanged = true;
obj[key] = value;
}
if (objChanged) chrome.storage.sync.set({ [data.UUID]: obj });
} else {
chrome.storage.sync.set({ [data.UUID]: data.obj });
}
});
case "getStorage": return chrome.storage.sync.get(data.UUID).then(result => {
const obj = result[data.UUID];
if (obj) {
const newObj = {};
for (const key of data.keys) {
newObj[key] = obj[key];
}
return newObj;
} else {
return {};
}
});
case "removeStorage": return chrome.storage.sync.remove(data);
case "storageUsed": return chrome.storage.sync.getBytesInUse(null);
case "storageUsedPerc": return chrome.storage.sync.getBytesInUse(null).then(bytesInUse => {
return (bytesInUse / chrome.storage.sync.QUOTA_BYTES * 100).toFixed(3) + " %";
});
case "catchSpotifyRequest": {
chrome.webRequest.onBeforeSendHeaders.addListener(catchSpotifyRequest, { urls: ["https://api.spotify.com/*"], tabId: sender.tab.id }, ["requestHeaders"]);
onTabClosed(sender.tab.id, () => {
chrome.webRequest.onBeforeSendHeaders.removeListener(catchSpotifyRequest);
spotifyAuthorizationReject();
});
return;
};
case "getSpotifyAuthorization": return spotifyAuthorization;
case "test": {
log(data);
return data;
};
default: throw new Error(`There is no event with name "${name}". Received data: ${data}`);
}})().then(dataOut => { // eslint-disable-line brace-style
sendResp({ error: false, data: dataOut });
}).catch(err => {
sendResp({ error: true, errObj: { message: err.message, stack: err.stack } });
});
return true; // Necessary if we want to send a response using sendResp
});
function findHeader(headers, headerStr) {
return headers.find(header => header.name.toLowerCase() === headerStr);
}
chrome.webRequest.onHeadersReceived.addListener(details => {
// This method breaks when server marks quickly repeated requests as a bot behavior
if (details.type !== "main_frame") return; // only intercept PDFs that will be view as an entire page
if (!findHeader(details.responseHeaders, "content-type")?.value.toLowerCase().includes("application/pdf")) return; // request is not for PDF
if (findHeader(details.responseHeaders, "content-disposition")?.value.toLowerCase().includes("attachment")) return; // requested PDF will be downloaded so don't download it again
// log(details);
chrome.downloads.download({ url: details.url, conflictAction: "overwrite" });
chrome.tabs.onUpdated.addListener(function tabsOnUpdated(tabId, changeInfo, tab) { // addlistener to close the tab or go back
// log(changeInfo);
if (changeInfo.status === "loading") {
chrome.tabs.goBack(tabId).then(null, () => { // if going back is not possible the promise gets rejected and that means that the PDF was opened in new tab so close the tab
chrome.tabs.remove(tabId);
});
chrome.tabs.onUpdated.removeListener(tabsOnUpdated);
}
});
}, { urls: ["<all_urls>"] }, ["responseHeaders"]);
/* // For the future if we needed to add repetative declarativeNetRequest rules. It has to be done iside onInstalled
chrome.runtime.onInstalled.addListener(() => {
chrome.declarativeNetRequest.getDynamicRules().then(oldRules => {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: oldRules.map(rule => rule.id),
addRules: [
"value",
].map((value, index) => ({
id: index + 1,
priority: 1,
condition: {},
action: {},
})),
});
});
});
*/
/* // Force all downloads to overwrite any existing files instead of inserting ' (1)', ' (2)', etc. | Does not work and is dangerous
chrome.downloads.onDeterminingFilename.addListener((item, suggest) => {
if (item.byExtensionId) {
log(item.byExtensionId);
suggest(); // does not work, conflictAction from download is ignored
} else {
log("Empty");
suggest({ filename: item.filename, conflictAction: "overwrite" });
}
});
*/
// #region Unused
/* // As of Manifest V3, this is only available to policy installed extensions.
//* This method breaks on this URL http://www.ee.ic.ac.uk/pcheung/teaching/DE1_EE/stores/sg90_datasheet.pdf
chrome.webRequest.onHeadersReceived.addListener(details => {
if (details.type !== "main_frame") return;
const contentType = details.responseHeaders.find(header => header.name.toLowerCase() === "content-type");
if (contentType === undefined || !contentType.value.toLowerCase().includes("application/pdf")) return; // request is not for PDF
// log(details);
const contDispIdx = details.responseHeaders.findIndex(header => header.name.toLowerCase() === "content-disposition");
if (contDispIdx !== -1) { // content-disposition header is present on the response
const oldVal = details.responseHeaders[contDispIdx].value.toLowerCase();
if (oldVal.includes("attachment")) {
return;
} else {
details.responseHeaders[contDispIdx].value = "attachment";
}
} else {
details.responseHeaders.push({ name: "content-disposition", value: "attachment" });
}
return { responseHeaders: details.responseHeaders };
}, { urls: ["<all_urls>"] }, ["blocking", "responseHeaders"]);
*/
// chrome.webRequest.onHeadersReceived.addListener(details => {
// log(details.type);
// }, { urls: ["<all_urls>"] }, ["responseHeaders"]);
/* getActiveTarget
async function getActiveTarget() {
return { tabId: (await chrome.tabs.query({ active: true, currentWindow: true }))[0].id };
} // const tabID = await getActiveTarget();
*/
// #endregion