]> git.wh0rd.org Git - chrome-ext/tabs-backup.git/commitdiff
save & restore more window/tab state
authorMike Frysinger <vapier@gentoo.org>
Sun, 26 Jan 2020 20:12:56 +0000 (15:12 -0500)
committerMike Frysinger <vapier@gentoo.org>
Sun, 26 Jan 2020 20:12:56 +0000 (15:12 -0500)
Keep window dimensions & dispositions.
Keep tab focus & pinned state.

Automatically discard new tabs once they've loaded (enough) so we
don't kill the system when trying to load a ton of tabs.  Users can
click all the tabs they care about to force them to load if needed.

background.js
manifest.json

index 6b6f89db93fbeb0694834d40552a9889e2a24cba..0f8a06cf202c259bd46b7df012bfd16f56bcd7a8 100644 (file)
@@ -165,10 +165,25 @@ function backupNow(isAutomatic, backupName, callbackDone) {
                        //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.title == 'New Tab' && tab.url == 'chrome://newtab/')
+                                       continue;
+                       }
+
                        for (var j = 0; j < windowTabs.length; j++) {
                                var tab = windowTabs[j];
 
@@ -176,7 +191,9 @@ function backupNow(isAutomatic, backupName, callbackDone) {
 
                                var bkpTab = {
                                        url: tab.url,
-                                       title: tab.title
+                                       title: tab.title,
+                                       highlighted: tab.highlighted,
+                                       pinned: tab.pinned,
                                };
 
                                // Add tab to tabs arrays
@@ -327,50 +344,66 @@ function restoreNow(backupName) {
                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;
-
-                                       console.log("==> Tab " + j + ": " + tabUrl);
-
-                                       var tabProperties = {
-                                               url: tabUrl,
-                                               windowId: createdWindow.id
-                                       };
+                               // 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});
+                               }
 
-                                       chrome.tabs.create(tabProperties, function(createdTab) {
-                                               // do nothing..
-                                       });
-                               }*/
+                               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);
+                                                       }
+                                               });
+                                       }
+                               });
                        });
-
-
-
                }
        });
 }
index 06162e67bb900f89846eecfb34bf136949946cf8..50ef3332c0fc9dd6c48be0c90b801c2cfbc7ff69 100644 (file)
@@ -1,6 +1,6 @@
 {
   "name": "vapier tabs backup",
-  "version": "0.2.1.1",
+  "version": "0.2.1.2",
   "manifest_version": 2,
   "description": "With 'Tabs Backup & Restore' you will never lose your work again! Backup an entire Chrome session (windows and tabs).",
   "icons": { "16": "icon_16.png",