tabsaver/popup.js
2025-05-08 09:58:39 +02:00

49 lines
1.4 KiB
JavaScript
Executable File

async function save(event) {
event.preventDefault();
event.stopImmediatePropagation();
let content = "";
// iterate ghrough all windows and all tabs
// windows are separated by a blank line
// tabs are separated by newline
const _windows = await chrome.windows.getAll({ populate: true });
_windows.forEach((_window, _windowIndex) => {
_window.tabs.forEach((_tab, _tabIndex) => {
content += _tab.url + "\n";
});
content += "\n";
});
const fileHandle = await showSaveFilePicker();
const writable = await fileHandle.createWritable();
await writable.write(content);
await writable.close();
window.close();
}
async function restore(event) {
event.preventDefault();
event.stopImmediatePropagation();
let tabs = [];
const [fileHandle] = await window.showOpenFilePicker();
const file = await fileHandle.getFile();
const contents = await file.text();
// get tabs from text file (each line is a tab, an empty line indicates we can open a window with previous tabs)
contents.split("\n").forEach(async (line) => {
if (line == "" && tabs.length) {
// create new window with tabs in array
chrome.windows.create({
state: "maximized",
url: tabs,
});
tabs = []; // reset tabs array
} else {
// add tab to array
tabs.push(line);
}
});
}
document.querySelector("a.save").onclick = save;
document.querySelector("a.restore").onclick = restore;