-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbackground.js
250 lines (238 loc) · 7.89 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
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
const apiTimeout = 25000;
let ongoingTab = null;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function downloadFile(options) {
return new Promise(async (resolve, reject) => {
let iid, deltas = {};
const onDownloadComplete = delta => {
if (iid === undefined) {
deltas[delta.id] = delta;
} else if (delta.id == iid) {
checkDelta(delta);
}
}
chrome.downloads.onChanged.addListener(onDownloadComplete);
function checkDelta(delta) {
if (delta.state && delta.state.current === "complete") {
chrome.downloads.onChanged.removeListener(onDownloadComplete);
resolve(delta.id);
} else if (delta.error) {
chrome.downloads.onChanged.removeListener(onDownloadComplete);
reject(new Error(delta.error.current));
} else if (delta.state && delta.state.current === "interrupted") {
chrome.downloads.onChanged.removeListener(onDownloadComplete);
reject(new Error(delta.state.current));
}
}
const timeId = setTimeout(async () => {
if (iid !== undefined) return;
if (options.url) {
const query = { url: options.url };
const items = await chrome.downloads.search(query);
if (items.length) {
let item = items[0];
let iid = item.id;
if (iid in deltas) {
console.log(`saved with timeout ${options.filename} | item id = ${iid}`);
checkDelta(deltas[iid]);
} else if ((item.state && item.state === "complete") || (item.bytesReceived && item.totalBytes && item.bytesReceived === item.totalBytes)) {
console.log(`saved from log with timeout ${options.filename} | item id = ${iid}`);
chrome.downloads.onChanged.removeListener(onDownloadComplete);
resolve(iid);
} else {
console.log(`download timed out on ${options.filename} | item id = ${iid}`);
}
return;
}
}
chrome.downloads.onChanged.removeListener(onDownloadComplete);
reject(new Error("API timeout"));
}, apiTimeout);
try {
iid = await chrome.downloads.download(options);
} catch (err) {
chrome.downloads.onChanged.removeListener(onDownloadComplete);
return reject(err);
} finally {
clearTimeout(timeId);
}
if (iid in deltas) checkDelta(deltas[iid]);
});
}
async function menuAlert(msg, decline_msg = "") {
await sleep(400);
chrome.runtime.sendMessage({
type: "coursedump_alert",
msg
}).catch(err=>{
if (decline_msg) {
console.log(decline_msg);
}
});
}
function updAllMenus() {
chrome.runtime.sendMessage({
type: "coursedump_updateOngoings"
}).catch(err=>{});
}
function ciddsParse(cidd_strings) {
try {
return cidd_strings.map(cidd_string => JSON.parse(cidd_string));
} catch (err) {
console.error("Error parsing cidd strings: ", err);
return [];
}
}
chrome.runtime.onMessage.addListener(async (arg, sender, sendResponse) => {
//messages from menu
if (arg.type === "coursedump_checkOngoing") {
sendResponse({ "ongoing-status": !!ongoingTab });
return;
} else if (arg.type === "coursedump_startDownload") {
if (ongoingTab) {
sendResponse({ status: "error", msg: "A download is already in progress in another tab" });
return;
}
ongoingTab = arg.tab_id;
updAllMenus();
//pass arguments
chrome.scripting.executeScript({
target: {tabId: ongoingTab},
args: [{'cidds': ciddsParse(arg.cidd_strings), 'settings': arg.settings}],
func: vars => Object.assign(self, vars),
}, () => {
//import module
chrome.scripting.executeScript({
target: {tabId: ongoingTab},
files: ['progressbars.js']
}, () => {
// run scanning script
chrome.scripting.executeScript({
target: {tabId: ongoingTab},
files: ['coursescan.js']},
(scanningFeedback) => {
if (chrome.runtime.lastError) {
console.log('Downloading tab was closed'); //during scan phase
ongoingTab = null;
menuAlert("Scanning tab was closed. Download terminated",
"no open menus left, download termination alert was not sent");
updAllMenus();
}
//console.log('scanning script callback', scanningFeedback);
//return signal from the scanning script
if (scanningFeedback?.[0]?.result === "scanning unauthorised") {
console.log('User not logged in. Download terminated');
ongoingTab = null;
updAllMenus();
} else if (scanningFeedback?.[0]?.result === "scanning stopped") {
console.log('Download stopped by user during scanning phase');
updAllMenus();
}
}
);
}
);
});
sendResponse({ 'status': "initiated" });
return;
} else if (arg.type === "coursedump_stopDownload") {
if (!ongoingTab) {
sendResponse({ status: "error", msg: "No downloads in progress" });
updAllMenus();
return;
}
chrome.tabs.sendMessage(ongoingTab, {
type: "coursedump_stopScan"
}).then(re => {
console.log("stop scanning signal went through");
}).catch(err => {
console.log("Downloading tab no longer exists - no scanning to stop");
});
ongoingTab = null;
console.log("bg stopped");
//updAllMenus();
sendResponse({ status: "stop signals sent" });
return;
}
//messages from the scanning script
const tabId = sender.tab.id;
if (tabId !== ongoingTab) {
console.warn(`download request from ${tabId} rejected - ongoing download was initiated by ${ongoingTab}`);
menuAlert("File download request from a conflicting tab received and rejected");
return;
};
let maxConnections = 5;
let urls = [];
if (arg.type === "coursedump_downloadFiles") {
let terminated = false;
if (arg.file_queue) urls = arg.file_queue;
const todo = urls.length;
if (arg.maxThreads) maxConnections = arg.maxThreads;
console.log(`max threads set to : ${maxConnections}`);
let done = 0;
let pids = Array(maxConnections).fill().map((_, i) => i + 1);
const results = await Promise.allSettled(pids.map(async pid => {
while (ongoingTab && urls.length) {
// const [url, filename] = ["some url", "some filename.ext"]; //emu
// const emu = urls.shift();//emu
const [url, filename] = urls.shift();
await sleep(200);
let did;
try {
did = await downloadFile({url, filename, conflictAction: "overwrite" });
//did = await sleep(Math.floor(Math.random() * 600 + 300)); //emulate download
} catch (err) {
console.error(filename, err);
chrome.tabs.sendMessage(tabId, {
type: "coursedump_error",
error: err.message,
url, filename
}).catch(err => {});
}
if (did !== undefined) {
await chrome.downloads.erase({ id: did });
}
done++;
chrome.tabs.sendMessage(tabId, {
type: "coursedump_progressMedia_upd",
done, todo
}).catch(err => {
console.warn(err);
if (err.message.includes('Could not establish connection. Receiving end does not exist.')) {
console.log('Downloading tab appears to be closed. terminating file download.');
terminated = true;
ongoingTab = null;
}
})
}
}));
for (let i = 0; i < results.length; i++) {
const r = results[i];
if (r.status === "rejected") {
console.error(`pid ${i + 1}: ${r.reason}`);
}
}
if (ongoingTab) {
chrome.tabs.sendMessage(tabId, {
type: "coursedump_mediaFinished",
status: "done"
}).catch(err => {});
ongoingTab = null;
} else {
if (!terminated) {
console.log('Download stopped by user during file downloading phase');
} else {
console.log('Downloading tab was closed during file downloading phase');
menuAlert("Downloading tab was closed. Download terminated",
"no open menus left, download termination alert was not sent");
}
chrome.tabs.sendMessage(tabId, {
type: "coursedump_mediaFinished",
status: "stopped"
}).catch(err => {});
}
updAllMenus();
}
});