-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
143 lines (117 loc) · 4.14 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const saveTabsButton = document.getElementById('saveTabs');
const snapshotList = document.getElementById('snapshotList');
const modalOverlay = document.getElementById('modalOverlay');
const modalSnapshotName = document.getElementById('modalSnapshotName');
const tabList = document.getElementById('tabList');
const cancelModal = document.getElementById('cancelModal');
const confirmSaveTabs = document.getElementById('confirmSaveTabs');
let snapshots = [];
window.onload = function() {
const savedSnapshots = JSON.parse(localStorage.getItem('snapshots')) || [];
snapshots = savedSnapshots;
renderSnapshots();
};
saveTabsButton.addEventListener('click', async () => {
modalOverlay.classList.add('show');
await listOpenTabs();
});
cancelModal.addEventListener('click', () => {
closeModal();
});
async function listOpenTabs() {
const tabs = await getOpenTabs();
tabList.innerHTML = '';
tabs.forEach((tab, index) => {
const tabItem = document.createElement('div');
tabItem.className = 'tab-item';
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = `tab-${index}`;
checkbox.checked = true;
const label = document.createElement('label');
label.htmlFor = `tab-${index}`;
label.textContent = tab.title || tab.url;
tabItem.appendChild(checkbox);
tabItem.appendChild(label);
tabList.appendChild(tabItem);
});
}
async function getOpenTabs() {
return new Promise((resolve) => {
chrome.tabs.query({ currentWindow: true }, (tabs) => {
resolve(tabs.map(tab => ({ url: tab.url, title: tab.title })));
});
});
}
confirmSaveTabs.addEventListener('click', async () => {
const snapshotName = modalSnapshotName.value.trim();
if (!snapshotName) {
alert("Please enter a name for the snapshot.");
return;
}
const selectedTabs = [];
const checkboxes = tabList.querySelectorAll('input[type="checkbox"]');
const tabs = await getOpenTabs();
checkboxes.forEach((checkbox, index) => {
if (checkbox.checked) {
selectedTabs.push(tabs[index]);
}
});
const newSnapshot = { name: snapshotName, tabs: selectedTabs };
snapshots.push(newSnapshot);
localStorage.setItem('snapshots', JSON.stringify(snapshots));
closeModal();
renderSnapshots();
});
function closeModal() {
modalOverlay.classList.remove('show');
modalSnapshotName.value = '';
}
function renderSnapshots() {
snapshotList.innerHTML = '';
snapshots.forEach((snapshot, index) => {
const snapshotItem = document.createElement('li');
snapshotItem.className = 'snapshot-item';
const snapshotName = document.createElement('span');
snapshotName.className = 'snapshot-name';
snapshotName.textContent = snapshot.name;
snapshotName.onclick = () => openTabs(snapshot.tabs);
const iconContainer = document.createElement('div');
iconContainer.className = 'snapshot-icons';
const editButton = document.createElement('button');
editButton.className = 'icon-button';
editButton.title = 'Edit';
editButton.innerHTML = '✏️';
editButton.onclick = () => editSnapshot(index);
const deleteButton = document.createElement('button');
deleteButton.className = 'icon-button';
deleteButton.title = 'Delete';
deleteButton.innerHTML = '🗑️';
deleteButton.onclick = () => deleteSnapshot(index);
iconContainer.appendChild(editButton);
iconContainer.appendChild(deleteButton);
snapshotItem.appendChild(snapshotName);
snapshotItem.appendChild(iconContainer);
snapshotList.appendChild(snapshotItem);
});
}
function openTabs(tabs) {
tabs.forEach(tab => {
chrome.tabs.create({ url: tab.url });
});
}
function editSnapshot(index) {
const newName = prompt("Enter the new name for this snapshot:", snapshots[index].name);
if (newName && newName.trim() !== '') {
snapshots[index].name = newName.trim();
localStorage.setItem('snapshots', JSON.stringify(snapshots));
renderSnapshots();
}
}
function deleteSnapshot(index) {
if (confirm("Are you sure you want to delete this snapshot?")) {
snapshots.splice(index, 1);
localStorage.setItem('snapshots', JSON.stringify(snapshots));
renderSnapshots();
}
}