-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
47 lines (38 loc) · 1.53 KB
/
background.js
File metadata and controls
47 lines (38 loc) · 1.53 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
let pendingDownloadingId = null;
let pendingSuggest = null;
// listens for any upcoming download file
chrome.downloads.onDeterminingFilename.addListener((downloadedItem, suggest) => {
// pause the download file so that the user give the custom filename
chrome.downloads.pause(downloadedItem.id, () => {
pendingDownloadingId = downloadedItem.id;
pendingSuggest = suggest;
// storing the original filename in the local storage
chrome.storage.local.set({
originalFilename: downloadedItem.filename
}).then(() => {
// opening the popup
chrome.action.openPopup();
})
})
// It necessary to return true bcz we call the suggest aysnchronously
// See the documentation https://developer.chrome.com/docs/extensions/reference/api/downloads?hl=en#event-onDeterminingFilename
return true;
})
// taking the user input from the popup using popup.js
chrome.runtime.onMessage.addListener((message) => {
// Checking if the customfilename received from popup.js
if (message.action === "setFilename" && message.filename && pendingSuggest && pendingDownloadingId) {
console.log("Received filename from popup:", message.filename)
} else {
return; // nothing to rename
}
// Renaming the original filename
pendingSuggest({ filename: message.filename })
chrome.downloads.resume(pendingDownloadingId, () => {
chrome.storage.local.remove("originalFilename", () => {
pendingDownloadingId = null
pendingSuggest = null
})
})
return true;
})