]>
git.wh0rd.org - chrome-ext/tabs-backup.git/blob - background.js
1 // Create a backup on first install (or if storage is wiped for some reason.
2 chrome
.storage
.local
.get(function(items
) {
4 if (items
.prefs_max_backup_items
=== undefined) {
5 chrome
.storage
.local
.set({prefs_max_backup_items
: 10});
7 if (items
.prefs_backup_timer
=== undefined) {
8 chrome
.storage
.local
.set({prefs_backup_timer
: 30});
11 // If a backup exists already, nothing to do.
12 if (items
.backups_list
) {
16 // Create a backup now
18 var formattedDate
= date_format (d
);
20 backupNow(true, formattedDate
, function({success
, backupName
, backupObj
}) {
25 function initAlarm () {
26 //console.log("initAlarm");
28 var BACKUP_ALARM_NAME
= "backup_alarm";
30 // Clear any previous alarm
31 chrome
.alarms
.clearAll();
33 chrome
.storage
.local
.get(function(items
) {
34 const timerMinutes
= items
.prefs_backup_timer
;
35 chrome
.alarms
.create(BACKUP_ALARM_NAME
, {periodInMinutes
: timerMinutes
});
41 function onAlarm (alarm
) {
43 var formattedDate
= date_format (d
);
45 console
.log("Alarm {" + alarm
+ "} fired up: " + formattedDate
);
47 // if last backup time != lastTabsEdit
48 // perform automatic backup
49 backupNow(true, formattedDate
, function({success
, backupName
, backupObj
}) {
50 // automatic backup completed
51 chrome
.runtime
.sendMessage({
52 action
: 'insertBackupItem',
53 args
: [backupName
, backupObj
, true /*insertAtBeginning*/, true /*doAnimation*/],
58 chrome
.alarms
.onAlarm
.addListener(onAlarm
);
60 // yyyy-mm-dd hh:mm:ss
61 function date_format(d
) {
62 const prependZero
= (val
) => val
.toString().padStart(2, '0');
63 return d
.getFullYear() + "-" +
64 prependZero(d
.getMonth() + 1) + "-" +
65 prependZero(d
.getDate()) + " " +
66 prependZero(d
.getHours()) + ":" +
67 prependZero(d
.getMinutes()) + ":" +
68 prependZero(d
.getSeconds());
72 function backupNowManual (callbackDone
) {
74 var formattedDate
= date_format (d
);
76 backupNow(false, formattedDate
, callbackDone
);
81 function deleteOldestBackup () {
82 chrome
.storage
.local
.get(function(items
) {
83 if(!items
.backups_list
) {
87 var backupsList
= items
.backups_list
;
88 var numItemsToDelete
= backupsList
.length
- items
.prefs_max_backup_items
;
89 if (numItemsToDelete
> 0) {
91 var loopFunc = function () {
94 var deletedBackupName
= backupsList
[i
-1];
95 chrome
.runtime
.sendMessage({
96 action
: 'removeBackupItemDiv',
97 args
: [deletedBackupName
],
102 if (i
>= numItemsToDelete
) {
106 deleteBackup(backupsList
[i
]).then(loopFunc
);
115 //var isCreatingBackup = false;
117 function backupNow(isAutomatic
, backupName
, callbackDone
) {
118 console
.log("backupNow - isAutomatic: " + isAutomatic
+ " name: " + backupName
);
119 /*if (isCreatingBackup === true) {
120 console.log("backupNow - already running..skipping..");
124 //isCreatingBackup = true;
126 /*if (!confirm("Perform a full backup? All windows and their tabs will be saved!")) {
132 isAutomatic
: isAutomatic
,
136 chrome
.windows
.getAll({populate
: true}, function (window_list
) {
139 for(var i
=0;i
<window_list
.length
;i
++) {
140 var window
= window_list
[i
];
142 //console.log ("Window " + i);
149 height
: window
.height
,
153 var windowTabs
= window
.tabs
;
155 // If it's a single window sittig at the new tab page, don't bother
156 // saving it. This is a nice shortcut when things crash as it will
157 // only show a single window.
158 if (windowTabs
.length
== 1) {
159 const tab
= windowTabs
[0];
160 if (tab
.url
== 'chrome://newtab/')
164 for (var j
= 0; j
< windowTabs
.length
; j
++) {
165 var tab
= windowTabs
[j
];
167 //console.log("==> Tab " + j + " (" + tab.index + "): " + tabUrl);
169 // Ignore windows that we can't/shouldn't backup.
170 if (tab
.url
.startsWith('chrome-untrusted://')) {
177 highlighted
: tab
.highlighted
,
181 // Add tab to tabs arrays
182 bkpWindow
.tabs
.push(bkpTab
);
185 if (bkpWindow
.tabs
.length
) {
186 totNumTabs
+= bkpWindow
.tabs
.length
;
188 fullBackup
.windows
.push(bkpWindow
);
195 fullBackup
.totNumTabs
= totNumTabs
;
197 var storageSetValues
= {};
198 storageSetValues
[backupName
] = fullBackup
;
201 chrome
.storage
.local
.set(storageSetValues
, function () {
202 if (chrome
.runtime
.lastError
) {
203 //isCreatingBackup = false;
204 // TODO change icon to error..
205 //alert ("Error: " + chrome.runtime.lastError.message);
206 updateBrowserActionIcon (1);
208 callbackDone({success
: false});
210 console
.log("backup saved");
211 //alert("Backup saved successfully!");
213 chrome
.storage
.local
.get(function(items
) {
214 var backupsList
= [];
215 if(items
.backups_list
) {
216 backupsList
= items
.backups_list
;
219 console
.log("Updating 'backups_list' - cur. size: " + backupsList
.length
);
221 backupsList
.push(backupName
);
223 chrome
.storage
.local
.set({"backups_list": backupsList
}, function () {
224 //isCreatingBackup = false;
226 if (chrome
.runtime
.lastError
) {
227 console
.log ("Error saving backups_list: " + chrome
.runtime
.lastError
.message
);
228 updateBrowserActionIcon (1);
229 callbackDone({success
: false});
231 console
.log("Backups list saved successfully");
233 updateBrowserActionIcon (0);
237 backupObj
: fullBackup
,
240 if (backupsList
.length
> items
.prefs_max_backup_items
) {
241 deleteOldestBackup();
255 function updateBrowserActionIcon (status
) {
259 icon
= "icon_ok.png";
262 icon
= "icon_error.png";
266 chrome
.action
.setIcon({path
: icon
});
269 async
function deleteBackup(backupName
) {
270 console
.log("Deleting backup " + backupName
);
272 await chrome
.storage
.local
.remove(backupName
);
273 //console.log("=> Deleted backup " + backupName);
275 const items
= await chrome
.storage
.local
.get("backups_list");
276 //console.log("==> got backups_list " + backupName);
278 if (!items
.backups_list
) {
282 var backupsList
= items
.backups_list
;
284 var index
= backupsList
.indexOf(backupName
);
286 backupsList
.splice(index
, 1);
289 //console.log("===> Updating backups_list (removing " + backupName + ")");
291 await chrome
.storage
.local
.set({"backups_list": backupsList
});
292 //console.log("===> Updated backups_list (removed " + backupName + ")");
295 function restoreNow(backupName
) {
296 console
.log("restoreNow");
298 chrome
.storage
.local
.get(backupName
, function(items
) {
299 if(!items
[backupName
]) {
300 alert("No Backup found");
304 /*if (!confirm("Restore full backup?")) {
308 if (confirm("Would you like to close all existing windows first?")) {
309 chrome.windows.getAll({populate : false}, function (window_list) {
310 for(var i=0;i<window_list.length;i++) {
311 var window = window_list[i];
312 chrome.windows.remove(window.id);
318 var fullBackup
= items
[backupName
];
320 for(var i
=0;i
<fullBackup
.windows
.length
;i
++) {
321 const window
= fullBackup
.windows
[i
];
323 //console.log ("Window " + i);
327 const windowTabs
= window
.tabs
;
328 for (let j
= 0; j
< windowTabs
.length
; j
++) {
329 const tab
= windowTabs
[j
];
330 const tabUrl
= tab
.url
;
331 urlsToOpen
.push(tabUrl
);
334 const windowProperties
= {
340 height
: window
.height
,
343 // Create a new Window
344 chrome
.windows
.create(windowProperties
, function(createdWindow
) {
345 // Chrome errors if the dimensions are set on non-normal windows.
346 // So we create the window first with the right settings, then
347 // update the window state.
348 if (window
.state
!= 'normal') {
349 chrome
.windows
.update(createdWindow
.id
, {state
: window
.state
});
352 chrome
.windows
.get(createdWindow
.id
, {populate
: true}, ({tabs
}) => {
353 for (let tabi
= 0; tabi
< windowTabs
.length
; ++tabi
) {
354 const oldtab
= windowTabs
[tabi
];
355 const newtab
= tabs
[tabi
];
356 chrome
.tabs
.update(newtab
.id
, {
357 highlighted
: oldtab
.highlighted
,
358 pinned
: oldtab
.pinned
,
360 if (!oldtab
.highlighted
) {
361 // If we discard a tab too fast, Chrome will completely
362 // throw it away. Wait until it's in a stable enough
363 // state for us to discard it.
365 const checktab
= (id
) => {
366 if (retryCount
-- < 0)
368 chrome
.tabs
.get(id
, (tab
) => {
370 setTimeout(() => checktab(id
), 500);
372 chrome
.tabs
.discard(id
);
386 * Callback from other pages (like the popup).
388 chrome
.runtime
.onMessage
.addListener(function(request
, sender
, sendResponse
) {
389 console
.log(`Got message from ${sender.id}: action=${request.action}`, request
);
391 let asyncResponse
= false;
392 switch (request
?.action
) {
398 restoreNow(...request
.args
);
402 deleteBackup(...request
.args
).then(sendResponse
);
403 asyncResponse
= true;
406 case 'backupNowManual':
407 backupNowManual(sendResponse
);
408 asyncResponse
= true;
411 return asyncResponse
;