-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
109 lines (88 loc) · 3.68 KB
/
popup.js
File metadata and controls
109 lines (88 loc) · 3.68 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
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
// targeting elements from popup.html
const originalFilenameElement = document.getElementById("original-filename")
const newFilenameInput = document.getElementById("new-filename")
// store filename and extension separately
let originalFilename = ""
let originalExtension = ""
// function to split the filename into base and extension
const splitFilename = (filename) => {
const name = filename.split("/").pop() // actual filename
const dotIndex = name.lastIndexOf(".")
// if no extension, then return only base name
if (dotIndex === -1) return { base: name, ext: "" };
return {
base: name.substring(0, dotIndex),
ext: name.substring(dotIndex)
}
}
// track user renaming
let userEditing = false;
// set the flag true on event "input"
newFilenameInput.addEventListener("input", () => {
userEditing = true;
})
// fetching the original filename stored by background.js
chrome.storage.local.get("originalFilename", (data) => {
if (data.originalFilename) {
originalFilename = data.originalFilename
// split base and extension
const { base, ext } = splitFilename(originalFilename)
originalExtension = ext;
// show full original filename in popup
originalFilenameElement.innerText = originalFilename.split("/").pop();
// prefill the input with only the base name
if (!userEditing && !newFilenameInput.value) {
newFilenameInput.value = base;
newFilenameInput.focus()
}
} else {
originalFilenameElement.innerText = "No active download found !!"
newFilenameInput.value = ""
}
})
// Adding event listener to the download button when clicked
document.getElementById("resume-download").addEventListener("click", () => {
const customBaseName = newFilenameInput.value.trim();
if (!customBaseName) return; // If empty input, do nothing
const finalFileName = customBaseName + originalExtension;
// Checking if there is any download is in process.
chrome.downloads.search({ state: "in_progress" }, (results) => {
if (chrome.runtime.lastError) {
console.error("Error checking downloads", chrome.runtime.lastError.message)
return;
}
if (results.length === 0) {
originalFilenameElement.innerText = "No files are downloading currently !!"
newFilenameInput.value = ""
}
})
// Send new filename to background.js
chrome.runtime.sendMessage({ action: "setFilename", filename: finalFileName }, (response) => {
if (response && response.success) {
console.log("filename sent to background script: ", finalFileName)
window.close()
} else {
console.log("failed to send filename")
}
})
})
// skip rename button handler
document.getElementById("skip-rename").addEventListener("click", () => {
// runs this code for skip button
chrome.storage.local.get("originalFilename", (data) => {
if (!data.originalFilename) return;
const skipName = data.originalFilename.split('/').pop()
chrome.runtime.sendMessage({ action: "setFilename", filename: skipName }, (response) => {
if (response && response.success) {
console.log("Skipped renaming, used original filename ", skipName)
window.close()
} else {
console.log("failed to send filename")
}
})
})
})
// Remove the original filename from the local storage
window.addEventListener("unload", () => {
chrome.storage.local.remove("originalFilename")
})