From 4fe91aa76b6093bc0bd06ae81335dbef70e785e4 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Sun, 26 Jan 2020 15:12:56 -0500 Subject: [PATCH] save & restore more window/tab state 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 | 95 ++++++++++++++++++++++++++++++++++----------------- manifest.json | 2 +- 2 files changed, 65 insertions(+), 32 deletions(-) diff --git a/background.js b/background.js index 6b6f89d..0f8a06c 100644 --- a/background.js +++ b/background.js @@ -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 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); + } + }); + } + }); }); - - - } }); } diff --git a/manifest.json b/manifest.json index 06162e6..50ef333 100644 --- a/manifest.json +++ b/manifest.json @@ -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", -- 2.39.5