Skip to content

Commit

Permalink
move session restoration to background page
Browse files Browse the repository at this point in the history
  • Loading branch information
mastef committed Oct 1, 2024
1 parent 61e1ec6 commit fcfbc1a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 78 deletions.
95 changes: 20 additions & 75 deletions lib/Session.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
}
}
}
94 changes: 91 additions & 3 deletions lib/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -98,15 +186,15 @@ async function updateTabCount() {
};
}
if (!found) toRemove.push(i);
};
}
}
// console.log("to remove", toRemove);
for (var i = toRemove.length - 1; i >= 0; i--) {
// console.log("removing", toRemove[i]);
if (!!window.tabsActive && window.tabsActive.length > 0) {
if (!!window.tabsActive[toRemove[i]]) window.tabsActive.splice(toRemove[i], 1);
}
};
}
} else {
await browser.browserAction.setBadgeText({ text: "" });
}
Expand Down

0 comments on commit fcfbc1a

Please sign in to comment.