]>
Commit | Line | Data |
---|---|---|
d9e77fbf MF |
1 | // Create a backup on first install (or if storage is wiped for some reason. |
2 | chrome.storage.local.get(function(items) { | |
3 | // Setup defaults. | |
4 | if (items.prefs_max_backup_items === undefined) { | |
5 | chrome.storage.local.set({prefs_max_backup_items: 10}); | |
6 | } | |
7 | if (items.prefs_backup_timer === undefined) { | |
8 | chrome.storage.local.set({prefs_backup_timer: 30}); | |
9 | } | |
10 | ||
11 | // If a backup exists already, nothing to do. | |
a18caa4d MF |
12 | if (items.backups_list) { |
13 | return; | |
14 | } | |
70c1267e MF |
15 | |
16 | // Create a backup now | |
17 | var d = new Date(); | |
18 | var formattedDate = date_format (d); | |
19 | ||
eda98b5f | 20 | backupNow(true, formattedDate, function({success, backupName, backupObj}) { |
70c1267e MF |
21 | // backup completed |
22 | }); | |
a18caa4d | 23 | }); |
70c1267e MF |
24 | |
25 | function initAlarm () { | |
e0b6a877 | 26 | //console.log("initAlarm"); |
70c1267e MF |
27 | |
28 | var BACKUP_ALARM_NAME = "backup_alarm"; | |
29 | ||
30 | // Clear any previous alarm | |
31 | chrome.alarms.clearAll(); | |
70c1267e | 32 | |
d9e77fbf MF |
33 | chrome.storage.local.get(function(items) { |
34 | const timerMinutes = items.prefs_backup_timer; | |
35 | chrome.alarms.create(BACKUP_ALARM_NAME, {periodInMinutes: timerMinutes}); | |
36 | }); | |
70c1267e MF |
37 | } |
38 | ||
39 | initAlarm(); | |
40 | ||
41 | function onAlarm (alarm) { | |
42 | var d = new Date(); | |
43 | var formattedDate = date_format (d); | |
44 | ||
e0b6a877 | 45 | console.log("Alarm {" + alarm + "} fired up: " + formattedDate); |
70c1267e | 46 | |
70c1267e MF |
47 | // if last backup time != lastTabsEdit |
48 | // perform automatic backup | |
eda98b5f | 49 | backupNow(true, formattedDate, function({success, backupName, backupObj}) { |
70c1267e MF |
50 | // automatic backup completed |
51 | var popupViews = chrome.extension.getViews({type: "popup"}); | |
52 | if (popupViews.length > 0) { | |
53 | for (var i = 0; i < popupViews.length; i++) { | |
54 | var popupView = popupViews[i]; | |
55 | if (!popupView.insertBackupItem) { | |
56 | continue; | |
57 | } | |
58 | ||
59 | popupView.insertBackupItem(backupName, backupObj, true /*insertAtBeginning*/, true /*doAnimation*/); | |
60 | popupView.updateStorageInfo(); | |
61 | } | |
62 | } | |
63 | }); | |
70c1267e MF |
64 | } |
65 | ||
66 | chrome.alarms.onAlarm.addListener(onAlarm); | |
67 | ||
68 | function date_prependZero (val) { | |
69 | return val < 10 ? "0" + val : "" + val; | |
70 | } | |
71 | ||
72 | // yyyy-m-d h:i:s | |
73 | function date_format (d) { | |
74 | var monthOneOffset = d.getMonth() + 1; // convert from 0-11 to 1-12 | |
75 | ||
76 | var formattedDate = d.getFullYear() + "-" + date_prependZero(monthOneOffset) + "-" + date_prependZero(d.getDate()) | |
77 | + " " + date_prependZero(d.getHours()) + ":" + date_prependZero(d.getMinutes()) + ":" + date_prependZero(d.getSeconds()); | |
78 | ||
79 | return formattedDate; | |
80 | } | |
81 | ||
82 | ||
83 | function backupNowManual (callbackDone) { | |
84 | var d = new Date(); | |
85 | var formattedDate = date_format (d); | |
86 | ||
87 | backupNow(false, formattedDate, callbackDone); | |
88 | ||
89 | ||
90 | } | |
91 | ||
92 | function deleteOldestBackup () { | |
d9e77fbf | 93 | chrome.storage.local.get(function(items) { |
70c1267e MF |
94 | if(!items.backups_list) { |
95 | return; | |
96 | } | |
97 | ||
98 | var backupsList = items.backups_list; | |
d9e77fbf | 99 | var numItemsToDelete = backupsList.length - items.prefs_max_backup_items; |
70c1267e MF |
100 | if (numItemsToDelete > 0) { |
101 | var i = 0; | |
102 | var loopFunc = function () { | |
103 | // | |
104 | if (i > 0) { | |
105 | var deletedBackupName = backupsList[i-1]; | |
106 | var popupViews = chrome.extension.getViews({type: "popup"}); | |
107 | if (popupViews.length > 0) { | |
108 | for (var j = 0; j < popupViews.length; j++) { | |
109 | var popupView = popupViews[j]; | |
110 | if (!popupView.removeBackupItemDiv) { | |
111 | continue; | |
112 | } | |
113 | ||
114 | popupView.removeBackupItemDiv(deletedBackupName); | |
115 | popupView.updateStorageInfo(); | |
116 | } | |
117 | } | |
118 | } | |
119 | // | |
120 | ||
121 | if (i >= numItemsToDelete) { | |
122 | return; | |
123 | } | |
124 | ||
125 | deleteBackup (backupsList[i], loopFunc); | |
126 | i++; | |
127 | }; | |
128 | ||
129 | loopFunc (); | |
130 | } | |
131 | ||
132 | //for (var i = 0; i < numItemsToDelete; i++) { | |
133 | // TODO WARNING: I'm calling deleteBackup rapidly, while deleting is async...(I should wait for each delete to complete before deleting the next) | |
134 | //deleteBackup (backupsList[i], function() { | |
135 | ||
136 | //}); | |
137 | //} | |
138 | ||
139 | }); | |
140 | } | |
141 | ||
142 | //var isCreatingBackup = false; | |
143 | ||
144 | function backupNow(isAutomatic, backupName, callbackDone) { | |
145 | console.log("backupNow - isAutomatic: " + isAutomatic + " name: " + backupName); | |
146 | /*if (isCreatingBackup === true) { | |
147 | console.log("backupNow - already running..skipping.."); | |
148 | return; | |
149 | }*/ | |
150 | ||
151 | //isCreatingBackup = true; | |
152 | ||
153 | /*if (!confirm("Perform a full backup? All windows and their tabs will be saved!")) { | |
154 | return; | |
155 | }*/ | |
156 | ||
157 | var fullBackup = { | |
158 | windows: [], | |
159 | isAutomatic: isAutomatic, | |
160 | totNumTabs: 0 | |
161 | }; | |
162 | ||
163 | chrome.windows.getAll({populate : true}, function (window_list) { | |
164 | var totNumTabs = 0; | |
165 | ||
166 | for(var i=0;i<window_list.length;i++) { | |
167 | var window = window_list[i]; | |
168 | ||
169 | //console.log ("Window " + i); | |
170 | ||
171 | var bkpWindow = { | |
4fe91aa7 MF |
172 | state: window.state, |
173 | top: window.top, | |
174 | left: window.left, | |
175 | width: window.width, | |
176 | height: window.height, | |
70c1267e MF |
177 | tabs: [] |
178 | }; | |
179 | ||
180 | var windowTabs = window.tabs; | |
4fe91aa7 MF |
181 | |
182 | // If it's a single window sittig at the new tab page, don't bother | |
183 | // saving it. This is a nice shortcut when things crash as it will | |
184 | // only show a single window. | |
185 | if (windowTabs.length == 1) { | |
186 | const tab = windowTabs[0]; | |
3b8161c3 | 187 | if (tab.url == 'chrome://newtab/') |
4fe91aa7 MF |
188 | continue; |
189 | } | |
190 | ||
70c1267e MF |
191 | for (var j = 0; j < windowTabs.length; j++) { |
192 | var tab = windowTabs[j]; | |
193 | ||
194 | //console.log("==> Tab " + j + " (" + tab.index + "): " + tabUrl); | |
195 | ||
e11f3338 MF |
196 | // Ignore windows that we can't/shouldn't backup. |
197 | if (tab.url.startsWith('chrome-untrusted://')) { | |
198 | continue; | |
199 | } | |
200 | ||
70c1267e MF |
201 | var bkpTab = { |
202 | url: tab.url, | |
4fe91aa7 MF |
203 | title: tab.title, |
204 | highlighted: tab.highlighted, | |
205 | pinned: tab.pinned, | |
70c1267e MF |
206 | }; |
207 | ||
208 | // Add tab to tabs arrays | |
209 | bkpWindow.tabs.push(bkpTab); | |
210 | } | |
211 | ||
e11f3338 MF |
212 | if (bkpWindow.tabs.length) { |
213 | totNumTabs += bkpWindow.tabs.length; | |
70c1267e | 214 | |
e11f3338 MF |
215 | fullBackup.windows.push(bkpWindow); |
216 | } | |
70c1267e MF |
217 | } |
218 | ||
e0b6a877 MF |
219 | if (totNumTabs == 0) |
220 | return; | |
221 | ||
70c1267e MF |
222 | fullBackup.totNumTabs = totNumTabs; |
223 | ||
224 | var storageSetValues = {}; | |
225 | storageSetValues[backupName] = fullBackup; | |
226 | ||
227 | // Store backup | |
228 | chrome.storage.local.set(storageSetValues, function () { | |
229 | if (chrome.runtime.lastError) { | |
230 | //isCreatingBackup = false; | |
231 | // TODO change icon to error.. | |
232 | //alert ("Error: " + chrome.runtime.lastError.message); | |
233 | updateBrowserActionIcon (1); | |
234 | ||
eda98b5f | 235 | callbackDone({success: false}); |
70c1267e MF |
236 | } else { |
237 | console.log("backup saved"); | |
238 | //alert("Backup saved successfully!"); | |
239 | ||
d9e77fbf | 240 | chrome.storage.local.get(function(items) { |
70c1267e MF |
241 | var backupsList = []; |
242 | if(items.backups_list) { | |
243 | backupsList = items.backups_list; | |
244 | } | |
245 | ||
246 | console.log("Updating 'backups_list' - cur. size: " + backupsList.length); | |
247 | ||
248 | backupsList.push(backupName); | |
249 | ||
250 | chrome.storage.local.set({"backups_list": backupsList}, function () { | |
251 | //isCreatingBackup = false; | |
252 | ||
253 | if (chrome.runtime.lastError) { | |
254 | console.log ("Error saving backups_list: " + chrome.runtime.lastError.message); | |
255 | updateBrowserActionIcon (1); | |
eda98b5f | 256 | callbackDone({success: false}); |
70c1267e MF |
257 | } else { |
258 | console.log("Backups list saved successfully"); | |
259 | ||
260 | updateBrowserActionIcon (0); | |
eda98b5f MF |
261 | callbackDone({ |
262 | success: true, | |
263 | backupName, | |
264 | backupObj: fullBackup, | |
265 | }); | |
70c1267e | 266 | |
d9e77fbf | 267 | if (backupsList.length > items.prefs_max_backup_items) { |
70c1267e MF |
268 | deleteOldestBackup(); |
269 | } | |
270 | } | |
271 | }); | |
272 | }); | |
273 | } | |
274 | }); | |
275 | }); | |
276 | } | |
277 | ||
278 | /** | |
279 | * 0 ==> OK | |
280 | * 1 ==> ERROR | |
281 | */ | |
282 | function updateBrowserActionIcon (status) { | |
283 | var icon; | |
284 | switch(status) { | |
285 | case 0: | |
286 | icon = "icon_ok.png"; | |
287 | break; | |
288 | default: | |
289 | icon = "icon_error.png"; | |
290 | break; | |
291 | } | |
292 | ||
1f779f9c | 293 | chrome.action.setIcon({path: icon}); |
70c1267e MF |
294 | } |
295 | ||
296 | function deleteBackup (backupName, callback) { | |
297 | console.log("Deleting backup " + backupName); | |
298 | ||
299 | chrome.storage.local.remove(backupName, function() { | |
300 | //console.log ("=> Deleted backup " + backupName); | |
301 | ||
302 | chrome.storage.local.get("backups_list", function(items) { | |
303 | //console.log ("==> got backups_list " + backupName); | |
304 | ||
305 | if(!items.backups_list) { | |
306 | callback(); | |
307 | return; | |
308 | } | |
309 | ||
310 | var backupsList = items.backups_list; | |
311 | ||
312 | var index = backupsList.indexOf(backupName); | |
313 | if (index >= 0) { | |
314 | backupsList.splice(index, 1); | |
315 | } | |
316 | ||
317 | //console.log ("===> Updating backups_list (removing " + backupName + ")"); | |
318 | ||
319 | chrome.storage.local.set({"backups_list": backupsList}, function() { | |
320 | //console.log ("===> Updated backups_list (removed " + backupName + ")"); | |
321 | ||
322 | callback(); | |
323 | }); | |
324 | ||
325 | //console.log ("==> EXIT got backups_list " + backupName); | |
326 | }); | |
327 | ||
328 | //console.log ("=> EXIT Deleted backup " + backupName); | |
329 | }); | |
330 | ||
331 | //console.log("EXIT Deleting backup " + backupName); | |
332 | ||
333 | ||
334 | } | |
335 | ||
336 | function restoreNow(backupName) { | |
337 | console.log("restoreNow"); | |
338 | ||
339 | chrome.storage.local.get(backupName, function(items) { | |
340 | if(!items[backupName]) { | |
341 | alert("No Backup found"); | |
342 | return; | |
343 | } | |
344 | ||
345 | /*if (!confirm("Restore full backup?")) { | |
346 | return; | |
347 | }*/ | |
348 | /* | |
349 | if (confirm("Would you like to close all existing windows first?")) { | |
350 | chrome.windows.getAll({populate : false}, function (window_list) { | |
351 | for(var i=0;i<window_list.length;i++) { | |
352 | var window = window_list[i]; | |
353 | chrome.windows.remove(window.id); | |
354 | } | |
355 | }); | |
356 | }*/ | |
357 | ||
358 | ||
359 | var fullBackup = items[backupName]; | |
360 | ||
361 | for(var i=0;i<fullBackup.windows.length;i++) { | |
4fe91aa7 | 362 | const window = fullBackup.windows[i]; |
70c1267e MF |
363 | |
364 | //console.log ("Window " + i); | |
365 | ||
366 | urlsToOpen = []; | |
367 | ||
4fe91aa7 MF |
368 | const windowTabs = window.tabs; |
369 | for (let j = 0; j < windowTabs.length; j++) { | |
370 | const tab = windowTabs[j]; | |
371 | const tabUrl = tab.url; | |
70c1267e MF |
372 | urlsToOpen.push(tabUrl); |
373 | } | |
374 | ||
4fe91aa7 MF |
375 | const windowProperties = { |
376 | state: 'normal', | |
377 | url: urlsToOpen, | |
378 | top: window.top, | |
379 | left: window.left, | |
380 | width: window.width, | |
381 | height: window.height, | |
70c1267e MF |
382 | }; |
383 | ||
384 | // Create a new Window | |
385 | chrome.windows.create(windowProperties, function(createdWindow) { | |
4fe91aa7 MF |
386 | // Chrome errors if the dimensions are set on non-normal windows. |
387 | // So we create the window first with the right settings, then | |
388 | // update the window state. | |
389 | if (window.state != 'normal') { | |
390 | chrome.windows.update(createdWindow.id, {state: window.state}); | |
391 | } | |
70c1267e | 392 | |
4fe91aa7 MF |
393 | chrome.windows.get(createdWindow.id, {populate: true}, ({tabs}) => { |
394 | for (let tabi = 0; tabi < windowTabs.length; ++tabi) { | |
395 | const oldtab = windowTabs[tabi]; | |
396 | const newtab = tabs[tabi]; | |
397 | chrome.tabs.update(newtab.id, { | |
398 | highlighted: oldtab.highlighted, | |
399 | pinned: oldtab.pinned, | |
400 | }, () => { | |
401 | if (!oldtab.highlighted) { | |
402 | // If we discard a tab too fast, Chrome will completely | |
403 | // throw it away. Wait until it's in a stable enough | |
404 | // state for us to discard it. | |
405 | let retryCount = 60; | |
406 | const checktab = (id) => { | |
407 | if (retryCount-- < 0) | |
408 | return; | |
409 | chrome.tabs.get(id, (tab) => { | |
410 | if (tab.pendingUrl) | |
411 | setTimeout(() => checktab(id), 500); | |
412 | else | |
413 | chrome.tabs.discard(id); | |
414 | }); | |
415 | }; | |
416 | checktab(newtab.id); | |
417 | } | |
418 | }); | |
419 | } | |
420 | }); | |
70c1267e | 421 | }); |
70c1267e MF |
422 | } |
423 | }); | |
e0b6a877 | 424 | } |
eda98b5f MF |
425 | |
426 | /** | |
427 | * Callback from other pages (like the popup). | |
428 | */ | |
429 | chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { | |
430 | console.log(`Got message from ${sender.id}: action=${request.action}`, request); | |
431 | ||
432 | let asyncResponse = false; | |
433 | switch (request?.action) { | |
434 | case 'initAlarm': | |
435 | initAlarm(); | |
436 | break; | |
437 | ||
438 | case 'restoreNow': | |
439 | restoreNow(...request.args); | |
440 | break; | |
441 | ||
442 | case 'deleteBackup': | |
443 | deleteBackup(...request.args, sendResponse); | |
444 | asyncResponse = true; | |
445 | break; | |
446 | ||
447 | case 'backupNowManual': | |
448 | backupNowManual(sendResponse); | |
449 | asyncResponse = true; | |
450 | break; | |
451 | } | |
452 | return asyncResponse; | |
453 | }); |