-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
68 lines (58 loc) · 2.25 KB
/
popup.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
document.addEventListener('DOMContentLoaded', () => {
const toggleSwitch = document.getElementById('toggleSwitch');
const status = document.getElementById('status');
const folderPath = document.getElementById('folderPath');
const saveFolder = document.getElementById('saveFolder');
const currentFolder = document.getElementById('currentFolder');
const orderImages = document.getElementById('orderImages');
// Get current states
chrome.storage.local.get(['isActive', 'downloadFolder', 'orderImages', 'currentImageNumber'], (data) => {
toggleSwitch.checked = data.isActive || false;
orderImages.checked = data.orderImages || false;
updateStatus(data.isActive || false);
if (data.downloadFolder) {
currentFolder.textContent = data.downloadFolder;
folderPath.value = data.downloadFolder;
}
});
toggleSwitch.addEventListener('change', () => {
const isActive = toggleSwitch.checked;
chrome.storage.local.set({ isActive: isActive }, () => {
chrome.tabs.query({}, (tabs) => {
tabs.forEach(tab => {
chrome.tabs.sendMessage(tab.id, {
action: "toggle",
isActive: isActive
}).catch(err => {
console.log('Tab update skipped:', err);
});
});
});
});
updateStatus(isActive);
});
orderImages.addEventListener('change', () => {
const isOrdered = orderImages.checked;
chrome.storage.local.set({
orderImages: isOrdered,
currentImageNumber: 1 // Reset counter when toggling
});
});
saveFolder.addEventListener('click', () => {
const folder = folderPath.value.trim();
const cleanFolder = folder
.replace(/\\/g, '/')
.replace(/^\/+|\/+$/g, '')
.replace(/[<>:"|?*]/g, '_');
chrome.storage.local.set({ downloadFolder: cleanFolder }, () => {
currentFolder.textContent = cleanFolder || 'Downloads';
status.textContent = 'Folder path saved!';
setTimeout(() => {
status.textContent = toggleSwitch.checked ? 'OneClick Download is active' : 'OneClick Download is inactive';
}, 2000);
});
});
function updateStatus(isActive) {
status.textContent = isActive ? 'OneClick Download is active' : 'OneClick Download is inactive';
}
});