-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
53 lines (49 loc) · 1.48 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
//set defualt categorize by semester
chrome.storage.local.get(["sem", "down"], function (result) {
if (result.sem == undefined || result.down == undefined) {
setState(true, 1);
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "download") {
chrome.storage.local.get(["sem", "down"], function (result) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
var path = "NLearn/";
if (result.sem) {
path += request.year + "/";
}
path += request.path;
var conflict = "uniquify";
if (result.down == 2) conflict = "overwrite";
else if (result.down == 3) conflict = "prompt";
chrome.downloads.download(
{
url: request.url,
filename: path,
conflictAction: conflict,
},
(downloadId) => {
if (downloadId) {
console.error(`Download failed: ${chrome.runtime.lastError}`);
}
}
);
});
} else if (request.action === "setState") {
setState(request.sem, request.down);
} else if (request.action === "getState") {
chrome.storage.local.get(["sem", "down"], function (result) {
sendResponse({ sem: result.sem, down: result.down });
});
return true;
}
});
function setState(sem, down) {
chrome.storage.local.set({ sem, down }, function () {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
}
});
}