-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
42 lines (34 loc) · 1.26 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
let folderMap = {
'pdf': 'PDFs/',
'exe': 'Installers/',
'jpg': 'Images/',
'png': 'Images/',
'docx': 'Documents/',
'xlsx': 'Documents/',
'zip': 'ZIP/',
// Initial default mappings; these can be updated dynamically
};
chrome.downloads.onDeterminingFilename.addListener((downloadItem, suggest) => {
try {
const fileExtension = downloadItem.filename.split('.').pop().toLowerCase();
if (folderMap[fileExtension]) {
const newFilename = folderMap[fileExtension] + downloadItem.filename;
suggest({filename: newFilename});
}
} catch (error) {
console.error('Error in redirecting download:', error);
suggest({filename: 'Other/' + item.filename});
}
});
chrome.downloads.onChanged.addListener((downloadDelta) => {
if (downloadDelta.error) {
console.error('Download error:', downloadDelta.error.current);
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.command === 'updateFolderMap' && request.newFolderMap) {
// Update the folderMap with the new mappings provided in the request
folderMap = request.newFolderMap;
sendResponse({status: 'Folder map updated successfully'});
}
});