+// Create a backup on first install (or if storage is wiped for some reason.
+chrome.storage.local.get(function(items) {
+ // Setup defaults.
+ if (items.prefs_max_backup_items === undefined) {
+ chrome.storage.local.set({prefs_max_backup_items: 10});
+ }
+ if (items.prefs_backup_timer === undefined) {
+ chrome.storage.local.set({prefs_backup_timer: 30});
+ }
-// Set default values if needed
-if (!localStorage.prefsMaxBackupItems) {
- localStorage.prefsMaxBackupItems = "10";
-}
-
-if (!localStorage.prefsBackupTimer) {
- localStorage.prefsBackupTimer = "30";
-}
-
-if (!localStorage.lastBackupTime) {
- localStorage.lastBackupTime = -1;
+ // If a backup exists already, nothing to do.
+ if (items.backups_list) {
+ return;
+ }
// Create a backup now
var d = new Date();
var formattedDate = date_format (d);
- backupNow(true, formattedDate, function(success, backupName, backupObj) {
+ backupNow(true, formattedDate, function({success, backupName, backupObj}) {
// backup completed
});
-}
+});
function initAlarm () {
//console.log("initAlarm");
// Clear any previous alarm
chrome.alarms.clearAll();
- var timerMinutes = parseInt(localStorage.prefsBackupTimer);
- chrome.alarms.create(BACKUP_ALARM_NAME, {periodInMinutes: timerMinutes});
+ chrome.storage.local.get(function(items) {
+ const timerMinutes = items.prefs_backup_timer;
+ chrome.alarms.create(BACKUP_ALARM_NAME, {periodInMinutes: timerMinutes});
+ });
}
initAlarm();
// if last backup time != lastTabsEdit
// perform automatic backup
- backupNow(true, formattedDate, function(success, backupName, backupObj) {
+ backupNow(true, formattedDate, function({success, backupName, backupObj}) {
// automatic backup completed
var popupViews = chrome.extension.getViews({type: "popup"});
if (popupViews.length > 0) {
}
function deleteOldestBackup () {
- chrome.storage.local.get("backups_list", function(items) {
+ chrome.storage.local.get(function(items) {
if(!items.backups_list) {
return;
}
var backupsList = items.backups_list;
- var numItemsToDelete = backupsList.length - parseInt(localStorage.prefsMaxBackupItems);
+ var numItemsToDelete = backupsList.length - items.prefs_max_backup_items;
if (numItemsToDelete > 0) {
var i = 0;
var loopFunc = function () {
//console.log ("Window " + i);
var bkpWindow = {
+ state: window.state,
+ top: window.top,
+ left: window.left,
+ width: window.width,
+ height: window.height,
tabs: []
};
var windowTabs = window.tabs;
+
+ // If it's a single window sittig at the new tab page, don't bother
+ // saving it. This is a nice shortcut when things crash as it will
+ // only show a single window.
+ if (windowTabs.length == 1) {
+ const tab = windowTabs[0];
+ if (tab.url == 'chrome://newtab/')
+ continue;
+ }
+
for (var j = 0; j < windowTabs.length; j++) {
var tab = windowTabs[j];
var bkpTab = {
url: tab.url,
- title: tab.title
+ title: tab.title,
+ highlighted: tab.highlighted,
+ pinned: tab.pinned,
};
// Add tab to tabs arrays
//alert ("Error: " + chrome.runtime.lastError.message);
updateBrowserActionIcon (1);
- callbackDone(false);
+ callbackDone({success: false});
} else {
console.log("backup saved");
//alert("Backup saved successfully!");
- chrome.storage.local.get("backups_list", function(items) {
+ chrome.storage.local.get(function(items) {
var backupsList = [];
if(items.backups_list) {
backupsList = items.backups_list;
if (chrome.runtime.lastError) {
console.log ("Error saving backups_list: " + chrome.runtime.lastError.message);
updateBrowserActionIcon (1);
- callbackDone(false);
+ callbackDone({success: false});
} else {
console.log("Backups list saved successfully");
updateBrowserActionIcon (0);
- callbackDone(true, backupName, fullBackup);
+ callbackDone({
+ success: true,
+ backupName,
+ backupObj: fullBackup,
+ });
- if (backupsList.length > parseInt(localStorage.prefsMaxBackupItems)) {
+ if (backupsList.length > items.prefs_max_backup_items) {
deleteOldestBackup();
}
}
break;
}
- chrome.browserAction.setIcon({path: icon});
+ chrome.action.setIcon({path: icon});
}
function deleteBackup (backupName, callback) {
var fullBackup = items[backupName];
for(var i=0;i<fullBackup.windows.length;i++) {
- var window = fullBackup.windows[i];
+ const window = fullBackup.windows[i];
//console.log ("Window " + i);
urlsToOpen = [];
- var windowTabs = window.tabs;
- for (var j = 0; j < windowTabs.length; j++) {
- var tab = windowTabs[j];
- var tabUrl = tab.url;
+ const windowTabs = window.tabs;
+ for (let j = 0; j < windowTabs.length; j++) {
+ const tab = windowTabs[j];
+ const tabUrl = tab.url;
urlsToOpen.push(tabUrl);
}
- var windowProperties = {
- url: urlsToOpen
+ const windowProperties = {
+ state: 'normal',
+ url: urlsToOpen,
+ top: window.top,
+ left: window.left,
+ width: window.width,
+ height: window.height,
};
// Create a new Window
chrome.windows.create(windowProperties, function(createdWindow) {
- //console.log("Created window id: " + createdWindow.id);
-
- //chrome.tabs.remove(createdWindow.tabs[0].id);
-
- // Create new tabs
- /*var windowTabs = window.tabs;
- for (var j = 0; j < windowTabs.length; j++) {
- var tab = windowTabs[j];
- var tabUrl = tab.url;
+ // Chrome errors if the dimensions are set on non-normal windows.
+ // So we create the window first with the right settings, then
+ // update the window state.
+ if (window.state != 'normal') {
+ chrome.windows.update(createdWindow.id, {state: window.state});
+ }
- console.log("==> Tab " + j + ": " + tabUrl);
+ chrome.windows.get(createdWindow.id, {populate: true}, ({tabs}) => {
+ for (let tabi = 0; tabi < windowTabs.length; ++tabi) {
+ const oldtab = windowTabs[tabi];
+ const newtab = tabs[tabi];
+ chrome.tabs.update(newtab.id, {
+ highlighted: oldtab.highlighted,
+ pinned: oldtab.pinned,
+ }, () => {
+ if (!oldtab.highlighted) {
+ // If we discard a tab too fast, Chrome will completely
+ // throw it away. Wait until it's in a stable enough
+ // state for us to discard it.
+ let retryCount = 60;
+ const checktab = (id) => {
+ if (retryCount-- < 0)
+ return;
+ chrome.tabs.get(id, (tab) => {
+ if (tab.pendingUrl)
+ setTimeout(() => checktab(id), 500);
+ else
+ chrome.tabs.discard(id);
+ });
+ };
+ checktab(newtab.id);
+ }
+ });
+ }
+ });
+ });
+ }
+ });
+}
- var tabProperties = {
- url: tabUrl,
- windowId: createdWindow.id
- };
+/**
+ * Callback from other pages (like the popup).
+ */
+chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
+ console.log(`Got message from ${sender.id}: action=${request.action}`, request);
- chrome.tabs.create(tabProperties, function(createdTab) {
- // do nothing..
- });
- }*/
- });
+ let asyncResponse = false;
+ switch (request?.action) {
+ case 'initAlarm':
+ initAlarm();
+ break;
+ case 'restoreNow':
+ restoreNow(...request.args);
+ break;
+ case 'deleteBackup':
+ deleteBackup(...request.args, sendResponse);
+ asyncResponse = true;
+ break;
- }
- });
-}
+ case 'backupNowManual':
+ backupNowManual(sendResponse);
+ asyncResponse = true;
+ break;
+ }
+ return asyncResponse;
+});