-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup_script.js
289 lines (264 loc) · 11.8 KB
/
popup_script.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const button = document.getElementById("btn-close");
button.addEventListener("click", () => {
window.close();
})
const locationOutput = document.getElementById("location");
const getLocationBtn = document.getElementById("btn-get-location");
const saveButton = document.getElementById('saveButton');
const doneSaving = document.getElementById('doneSaving');
const addTabsBtn = document.getElementById('btn-add-tabs');
getLocationBtn.addEventListener("click", () => {
navigator.geolocation.getCurrentPosition(function(position) {
const { latitude, longitude } = position.coords;
const coordinates = latitude.toFixed(3) + " " + longitude.toFixed(3)
locationOutput.innerHTML = coordinates;
document.getElementById('openTabsButton').innerHTML = "Open Tabs for " + coordinates;
const locationKey = `${latitude.toFixed(3)},${longitude.toFixed(3)}`;
chrome.storage.local.get({names: {}, namesToLocation: {}}, function(result) {
const names = result.names;
const namesToLocation = result.namesToLocation;
if (!names[locationKey]) {
const nameButton = document.getElementById('naming-location');
nameButton.style.display = 'block';
document.getElementById('nameLocation').addEventListener("click", () => {
nameButton.style.display = 'none';
document.getElementById('save-name').style.display = 'block';
document.getElementById('saveName').addEventListener("click", () => {
const nameInput = document.getElementById('nameInput');
const name = nameInput.value.trim();
console.log(name);
names[locationKey] = name;
namesToLocation[name] = locationKey;
locationOutput.innerHTML = name + " (" + coordinates + ")";
document.getElementById('openTabsButton').innerHTML = "Open Tabs for " + name;
chrome.storage.local.set({names, namesToLocation}, function() {
document.getElementById('nameInput').value = '';
document.getElementById('naming-location').style.display = 'none';
document.getElementById('save-name').style.display = 'none';
});
});
});
}
else {
locationOutput.innerHTML = names[locationKey] + " (" + coordinates + ")";
document.getElementById('openTabsButton').innerHTML = "Open Tabs for " + names[locationKey];
}
});
console.log(locationKey);
}, function(error) {
console.error(error);
});
document.getElementById('open').style.display = 'block';
});
addTabsBtn.addEventListener("click", () => {
const searchField = document.getElementById('search-field');
searchField.style.display = 'block';
console.log(searchField);
});
saveButton.addEventListener('click', function() {
const urlInput = document.getElementById('urlInput');
const url = urlInput.value.trim();
const existingErrorMessage = document.querySelector('.error-message');
if (existingErrorMessage) {
existingErrorMessage.remove();
}
if (!url) {
const errorMessage = document.createElement('p');
errorMessage.textContent = 'No URL entered.';
errorMessage.classList.add('error-message');
errorMessage.style.color = 'red';
urlInput.parentNode.insertBefore(errorMessage, urlInput.nextSibling);
return;
}
if (!isValidUrl(url)) {
const errorMessage = document.createElement('p');
errorMessage.textContent = 'Invalid URL. Please enter a valid URL.';
errorMessage.classList.add('error-message');
errorMessage.style.color = 'red';
urlInput.parentNode.insertBefore(errorMessage, urlInput.nextSibling);
return;
}
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function(position) {
const { latitude, longitude } = position.coords;
const locationKey = `${latitude.toFixed(3)},${longitude.toFixed(3)}`; // Simplify coords
saveUrlForLocation(locationKey, url);
}, function(error) {
console.error(error);
});
} else {
console.log("Geolocation is not supported by this browser.");
}
});
doneSaving.addEventListener("click", () => {
const searchField = document.getElementById('search-field');
searchField.style.display = 'none';
const existingErrorMessage = document.querySelector('.error-message');
if (existingErrorMessage) {
existingErrorMessage.remove();
}
document.getElementById('urlInput').value = '';
});
const openTabsButton = document.getElementById("openTabsButton");
openTabsButton.addEventListener("click", function() {
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function(position) {
const { latitude, longitude } = position.coords;
const locationKey = `${latitude.toFixed(3)},${longitude.toFixed(3)}`;
openLocationBasedUrls(locationKey);
}, function(error) {
console.error(error);
});
} else {
console.log("Geolocation is not supported by this browser.");
}
});
function saveUrlForLocation(locationKey, url) {
chrome.storage.local.get({locations: {}}, function(result) {
const locations = result.locations;
console.log(locations[locationKey]);
chrome.storage.local.get({names: {}}, function(result) {
const names = result.names;
if (!names[locationKey]) {
if (!locations[locationKey]) {
locations[locationKey] = [];
}
locations[locationKey].push(url);
}
else {
if (!locations[names[locationKey]]) {
locations[names[locationKey]] = [];
}
locations[names[locationKey]].push(url);
}
chrome.storage.local.set({locations}, function() {
console.log(`URL saved for location ${names[locationKey] ? names[locationKey] : locationKey}:`, url);
document.getElementById('urlInput').value = '';
});
});
});
}
function openLocationBasedUrls(locationKey) {
const tabIds = [];
chrome.storage.local.get({locations: {}}, function(results) {
chrome.storage.local.get({names: {}}, async function(result) {
const names = result.names;
let title;
let urls;
if (!names[locationKey]) {
urls = results.locations[locationKey];
title = locationKey;
} else {
urls = results.locations[names[locationKey]];
title = names[locationKey];
}
if (urls) {
for (const url of urls) {
const tab = await chrome.tabs.create({ url });
tabIds.push(tab.id);
}
} else {
console.log("No URLs saved for this location.");
}
if (tabIds.length) {
const group = await chrome.tabs.group({ tabIds });
await chrome.tabGroups.update(group, { title: title });
}
});
});
console.log(tabIds);
}
function removeUrlFromLocation(locationKey, urlToRemove) {
chrome.storage.local.get({locations: {}}, function(result) {
const locations = result.locations;
if (locations[locationKey]) {
locations[locationKey] = locations[locationKey].filter(url => url !== urlToRemove);
chrome.storage.local.set({locations}, function() {
console.log(`URL removed from location ${locationKey}:`, urlToRemove);
viewSavedTabsButton.click();
});
}
});
}
function removeLocation(locationKey) {
chrome.storage.local.get({locations: {}, namesToLocation: {}, names: {}}, function(result) {
const locations = result.locations;
const namesToLocation = result.namesToLocation;
const names = result.names;
if (locations[locationKey]) {
const savedUrls = locations[locationKey];
savedUrls.forEach(savedUrl => {
locations[locationKey] = locations[locationKey].filter(url => url !== savedUrl);
});
delete locations[locationKey]; // Remove the location entry
delete names[namesToLocation[locationKey]];
chrome.storage.local.set({locations, namesToLocation, names}, function() {
console.log(`Location ${locationKey} and all associated URLs removed.`);
viewSavedTabsButton.click(); // Update the UI
});
}
});
}
const viewSavedTabsButton = document.getElementById("viewSavedTabsButton");
const savedTabsContainer = document.getElementById("savedTabsContainer");
viewSavedTabsButton.addEventListener("click", function() {
chrome.storage.local.get({locations: {}}, function(result) {
const locations = result.locations;
savedTabsContainer.innerHTML = '';
let locationCount = 0;
Object.keys(locations).forEach(locationKey => {
locationCount++;
const savedUrls = locations[locationKey];
const locationElement = document.createElement('div');
locationElement.className = "border";
locationElement.style.width = "400px";
const urlsList = document.createElement('ul');
const locationName = document.createElement('h3');
locationName.textContent = `Location: ${locationKey}`;
urlsList.appendChild(locationName);
let tabCount = 0;
savedUrls.forEach(url => {
tabCount++;
const urlItem = document.createElement('li');
const link = document.createElement('a');
link.href = url;
link.target = "_blank";
link.textContent = url;
urlItem.appendChild(link);
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.addEventListener('click', function() {
removeUrlFromLocation(locationKey, url);
});
urlItem.appendChild(removeButton);
urlsList.appendChild(urlItem);
});
if (tabCount === 0) {
const noTabs = document.createElement('h4');
noTabs.textContent = 'No tabs currently saved...'
urlsList.appendChild(noTabs);
}
locationElement.appendChild(urlsList);
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete Location';
deleteButton.addEventListener('click', function() {
removeLocation(locationKey);
});
locationElement.appendChild(deleteButton);
savedTabsContainer.appendChild(locationElement);
});
if (locationCount === 0) {
const locationElement = document.createElement('div');
locationElement.className = "border";
locationElement.style.width = "400px";
const locationName = document.createElement('h3');
locationName.textContent = "Start saving tabs to see them here";
locationElement.appendChild(locationName);
savedTabsContainer.appendChild(locationElement);
}
});
});
function isValidUrl(url) {
const urlPattern = /^(https?|ftp|file|chrome):\/\/\S+$/i;
return urlPattern.test(url);
}