-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
115 lines (94 loc) · 3.85 KB
/
Copy pathbackground.js
File metadata and controls
115 lines (94 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var browser = browser || chrome;
const defaultConfig = [
{ url: "https://www.wikipedia.org/", pinned: true, muted: false },
{ url: "https://www.wiktionary.org/", pinned: false, muted: true }
];
async function restoreTabs() {
try {
const data = await browser.storage.local.get(["savedTabs", "closeOtherTabs"]);
const configTabs = data.savedTabs || defaultConfig;
const shouldCloseOthers = data.closeOtherTabs || false;
if (configTabs.length === 0) return;
const currentWindow = await browser.windows.getCurrent();
const currentTabs = await browser.tabs.query({ windowId: currentWindow.id });
const normalize = (url) => {
if (!url || url === "about:newtab" || url === "chrome://newtab/") return "kJ_NEW_TAB_kJ";
try {
const u = new URL(url);
let host = u.hostname.replace(/^www\./, "");
let path = u.pathname.replace(/\/$/, "");
return host + path + u.search + u.hash;
} catch (e) {
return url.replace(/\/$/, "");
}
};
const reusedTabIds = new Set();
const finalTabIds = [];
for (let i = 0; i < configTabs.length; i++) {
const target = configTabs[i];
const targetNorm = normalize(target.url);
const urlToOpen = target.url || undefined;
const existingTab = currentTabs.find(t => {
if (reusedTabIds.has(t.id)) return false;
return normalize(t.url) === targetNorm;
});
if (existingTab) {
reusedTabIds.add(existingTab.id);
finalTabIds.push(existingTab.id);
await browser.tabs.move(existingTab.id, { index: i });
await browser.tabs.update(existingTab.id, {
pinned: target.pinned,
muted: target.muted
});
if (target.focus) {
await browser.tabs.update(existingTab.id, { active: true });
}
} else {
const isActive = target.focus || false;
const newTab = await browser.tabs.create({
url: urlToOpen,
index: i,
pinned: target.pinned,
active: isActive,
windowId: currentWindow.id
});
finalTabIds.push(newTab.id);
if (target.muted) {
await browser.tabs.update(newTab.id, { muted: true });
}
}
}
if (shouldCloseOthers) {
const allTabsNow = await browser.tabs.query({ windowId: currentWindow.id });
const tabsToRemove = allTabsNow
.filter(t => !finalTabIds.includes(t.id))
.filter(t => !t.url.startsWith(browser.runtime.getURL("")))
.map(t => t.id);
if (tabsToRemove.length > 0) {
await browser.tabs.remove(tabsToRemove);
}
}
} catch (e) {
console.error("Error restoring tabs:", e);
}
}
browser.runtime.onStartup.addListener(() => {
setTimeout(restoreTabs, 800);
});
browser.runtime.onInstalled.addListener(async (details) => {
if (details.reason === "install") {
const data = await browser.storage.local.get("hasShownInstallPage");
const currentStored = await browser.storage.local.get("savedTabs");
if (!currentStored.savedTabs) {
await browser.storage.local.set({ savedTabs: defaultConfig });
}
if (!data.hasShownInstallPage) {
browser.runtime.openOptionsPage();
await browser.storage.local.set({ hasShownInstallPage: true });
}
}
restoreTabs();
});
browser.action.onClicked.addListener(() => {
browser.runtime.openOptionsPage();
});