From fcfbc1a7551062ef7c6086064d3c438c2bb91b5a Mon Sep 17 00:00:00 2001 From: mastef Date: Tue, 1 Oct 2024 09:47:12 +0700 Subject: [PATCH] move session restoration to background page --- lib/Session.jsx | 95 ++++++++++------------------------------------- lib/background.js | 94 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 78 deletions(-) diff --git a/lib/Session.jsx b/lib/Session.jsx index e10bab73..900894a5 100644 --- a/lib/Session.jsx +++ b/lib/Session.jsx @@ -127,91 +127,26 @@ class Session extends React.Component { return true; } stop(e) { - e.stopPropagation(); + this.stopProp(e); } async windowClick(e) { var _this2 = this; - e.stopPropagation(); + this.stopProp(e); console.log("source window", this.props.window); // chrome.runtime.getBackgroundPage(function callback(tabs, backgroundPage) { // backgroundPage.createWindowWithTabs(tabs); // }.bind(null, this.props.window.tabs)); - var customName = false; - if (this.props.window && this.props.window.name && this.props.window.customName) { - customName = this.props.window.name; - } - - var whitelistWindow = ["left", "top", "width", "height", "incognito", "type"]; - + let backgroundPage = await browser.runtime.getBackgroundPage(); if (navigator.userAgent.search("Firefox") > -1) { - whitelistWindow = ["left", "top", "width", "height", "incognito", "type"]; - } - - var whitelistTab = ["url", "active", "selected", "pinned"]; - - if (navigator.userAgent.search("Firefox") > -1) { - whitelistTab = ["url", "active", "pinned"]; - } - - var filteredWindow = Object.keys(this.props.window.windowsInfo) - .filter(function(key) { - return whitelistWindow.includes(key); - }) - .reduce(function(obj, key) { - obj[key] = _this2.props.window.windowsInfo[key]; - return obj; - }, {}); - console.log("filtered window", filteredWindow); - - var newWindow = await browser.windows.create(filteredWindow).catch(function(error) { - console.error(error); - console.log(error); - console.log(error.message); - }); - - var emptyTab = newWindow.tabs[0].id; - - for (var i = 0; i < this.props.window.tabs.length; i++) { - var newTab = Object.keys(this.props.window.tabs[i]) - .filter(function(key) { - return whitelistTab.includes(key); - }) - .reduce(function(obj, key) { - obj[key] = _this2.props.window.tabs[i][key]; - return obj; - }, {}); - console.log("source tab", newTab); - if (navigator.userAgent.search("Firefox") > -1) { - if (!!newTab.url && newTab.url.search("about:") > -1) { - console.log("filtered by about: url", newTab.url); - newTab.url = ""; - } - } - newTab.windowId = newWindow.id; - var tabCreated = await browser.tabs.create(newTab).catch(function(error) { - console.error(error); - console.log(error); - console.log(error.message); - }); + backgroundPage.createWindowWithTabsFromSessionDelayed(this.props.window); + } else { + backgroundPage.createWindowWithTabsFromSession(this.props.window); } - await browser.tabs.remove(emptyTab).catch(function(error) { - console.error(error); - console.log(error); - console.log(error.message); - }); + /////// - if (customName) { - var names = localStorage["windowNames"]; - if (!!names) { - names = JSON.parse(names); - } else { - names = {}; - } - names[newWindow.id] = customName || ""; - localStorage["windowNames"] = JSON.stringify(names); - } + console.log("updating parent"); this.props.parentUpdate(); @@ -237,16 +172,26 @@ class Session extends React.Component { // function (a) {this.props.parentUpdate();}.bind(this)); } async close(e) { - e.stopPropagation(); + this.stopProp(e); var value = await browser.storage.local.remove(this.props.window.id); console.log(value); this.props.parentUpdate(); // browser.windows.remove(this.props.window.windowsInfo.id); } maximize(e) { - e.stopPropagation(); + this.stopProp(e); // browser.windows.update(this.props.window.windowsInfo.id, { // "state": "normal" }, // function (a) {this.props.parentUpdate();}.bind(this)); } + stopProp(e) { + if (e && e.nativeEvent) { + e.nativeEvent.preventDefault(); + e.nativeEvent.stopPropagation(); + } + if (e && e.preventDefault) { + e.preventDefault(); + e.stopPropagation(); + } + } } diff --git a/lib/background.js b/lib/background.js index fd60beac..02e27593 100644 --- a/lib/background.js +++ b/lib/background.js @@ -38,11 +38,99 @@ async function createWindowWithTabs(tabs, isIncognito) { await browser.tabs.update(newTab.id, { pinned: tabPinned }); } } - }; + } } await browser.windows.update(w.id, { focused: true }); } +function createWindowWithTabsFromSessionDelayed(window) { + var window = JSON.parse(JSON.stringify(window)); + setTimeout(createWindowWithTabsFromSession.bind(this, window), 125); +} + +async function createWindowWithTabsFromSession(window) { + + var customName = false; + if (window && window.name && window.customName) { + customName = window.name; + } + + var whitelistWindow = ["left", "top", "width", "height", "incognito", "type"]; + + if (navigator.userAgent.search("Firefox") > -1) { + whitelistWindow = ["left", "top", "width", "height", "incognito", "type"]; + } + + var whitelistTab = ["url", "active", "selected", "pinned"]; + + if (navigator.userAgent.search("Firefox") > -1) { + whitelistTab = ["url", "active", "pinned"]; + } + + var filteredWindow = Object.keys(window.windowsInfo) + .filter(function (key) { + return whitelistWindow.includes(key); + }) + .reduce(function (obj, key) { + obj[key] = window.windowsInfo[key]; + return obj; + }, {}); + + try { + var newWindow = await browser.windows.create(filteredWindow); + } catch (error) { + console.error(error); + console.log(error); + console.log(error.message); + } + + var emptyTab = newWindow.tabs[0].id; + + for (var i = 0; i < window.tabs.length; i++) { + var newTab = Object.keys(window.tabs[i]) + .filter(function (key) { + return whitelistTab.includes(key); + }) + .reduce(function (obj, key) { + obj[key] = window.tabs[i][key]; + return obj; + }, {}); + + if (navigator.userAgent.search("Firefox") > -1) { + if (!!newTab.url && newTab.url.search("about:") > -1) { + console.log("filtered by about: url", newTab.url); + newTab.url = ""; + } + } + newTab.windowId = newWindow.id; + try { + let tabCreated = await browser.tabs.create(newTab); + } catch (error) { + console.error(error); + console.log(error); + console.log(error.message); + } + } + + await browser.tabs.remove(emptyTab).catch(function (error) { + console.error(error); + console.log(error); + console.log(error.message); + }); + + if (customName) { + var names = localStorage["windowNames"]; + if (!!names) { + names = JSON.parse(names); + } else { + names = {}; + } + names[newWindow.id] = customName || ""; + localStorage["windowNames"] = JSON.stringify(names); + } + +} + function focusOnTabAndWindowDelayed(tab) { var tab = JSON.parse(JSON.stringify(tab)); setTimeout(focusOnTabAndWindow.bind(this, tab), 125); @@ -98,7 +186,7 @@ async function updateTabCount() { }; } if (!found) toRemove.push(i); - }; + } } // console.log("to remove", toRemove); for (var i = toRemove.length - 1; i >= 0; i--) { @@ -106,7 +194,7 @@ async function updateTabCount() { if (!!window.tabsActive && window.tabsActive.length > 0) { if (!!window.tabsActive[toRemove[i]]) window.tabsActive.splice(toRemove[i], 1); } - }; + } } else { await browser.browserAction.setBadgeText({ text: "" }); }