-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
87 lines (80 loc) · 2.7 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
// Sample initial domain registry (you can edit or add to this as needed)
const domainRegistry = {
"https://moodle.tu-darmstadt.de/course/view.php?id=35283":
"Documents/Uni/WiSe 23/Software and Internet Economics/",
};
function logDebugMessage(message) {
const timestamp = new Date().toLocaleString();
chrome.storage.local.get("debugLog", (data) => {
let debugLog = data.debugLog || [];
debugLog.push({ timestamp, message });
chrome.storage.local.set({ debugLog });
});
}
function suggestFilename(path, suggest) {
suggest(
{ filename: path, conflictAction: "uniquify" },
(downloadId) => {
if (chrome.runtime.lastError) {
console.error("error: ", chrome.runtime.lastError);
console.warn(
`An error occurred: ${chrome.runtime.lastError.message} and ${downloadId}`
);
return false;
} else {
console.log(`Download started with ID: ${downloadId}`);
return true;
}
}
);
}
const defaultPath = "Downloads/";
chrome.downloads.onDeterminingFilename.addListener((downloadItem, suggest) => {
try {
chrome.storage.local.get(["domainRegistry", "debugMode"], (data) => {
try {
const registry = data.domainRegistry;
console.log(registry);
const debugMode = data.debugMode;
console.log(downloadItem.referrer);
try {
const domainUrl = new URL(downloadItem.referrer);
} catch (error) {
console.log(error);
suggestFilename(defaultPath + downloadItem.filename, suggest);
return true;
}
console.log(downloadItem);
console.log(domainUrl);
const domain = new URL(downloadItem.referrer).href;
console.log(domain);
console.log(`Download recognized from ${domain}`);
console.log(`Checking if website ${domain} is in registry...`);
var newFilename = "";
if (registry[domain]) {
console.log(
`Website ${domain} found in registry. Redirecting download to ${registry[domain]}`
);
newFilename = registry[domain] + downloadItem.filename;
} else {
console.log(
`Website ${domain} not found in registry. Using default download path.`
);
newFilename = "Downloads/" + downloadItem.filename;
}
console.log(newFilename);
console.log(typeof newFilename);
console.log(downloadItem.finalUrl);
suggestFilename(newFilename, suggest);
} catch (error) {
console.error(error);
suggestFilename(defaultPath + downloadItem.filename, suggest);
return true;
}
});
return true;
} catch (error) {
console.error(error);
return false;
}
});