-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
177 lines (138 loc) · 4.63 KB
/
Copy pathbackground.js
File metadata and controls
177 lines (138 loc) · 4.63 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const STORAGE_KEY = "containerGroups";
const SETTINGS_KEY = "settings";
const MENU_ROOT_ID = "open-in-container-group-root";
function t(key, subs) {
return browser.i18n.getMessage(key, subs);
}
/* ---------- helpers ---------- */
function randomId() {
return Math.random().toString(16).slice(2) + Date.now().toString(16);
}
async function getGroups() {
const data = await browser.storage.local.get(STORAGE_KEY);
return Array.isArray(data[STORAGE_KEY]) ? data[STORAGE_KEY] : [];
}
async function getSettings() {
const data = await browser.storage.local.get(SETTINGS_KEY);
return Object.assign({ focusExisting: true }, data[SETTINGS_KEY] || {});
}
async function ensureInitialData() {
const groups = await getGroups();
if (groups.length === 0) {
await browser.storage.local.set({
[STORAGE_KEY]: [{ id: randomId(), name: "Default", containerIds: [] }]
});
}
}
async function findExistingTab(url, cookieStoreId) {
const tabs = await browser.tabs.query({ url });
return tabs.find(t => t.cookieStoreId === cookieStoreId) || null;
}
/* ---------- open logic (shared) ---------- */
async function openUrlInGroupByIndex(url, index) {
if (!url || !/^https?:\/\//i.test(url)) return;
const groups = await getGroups();
const group = groups[index];
if (!group) return;
const settings = await getSettings();
const identities = await browser.contextualIdentities.query({});
const validIds = new Set(identities.map(ci => ci.cookieStoreId));
for (const storeId of group.containerIds || []) {
if (!validIds.has(storeId)) continue;
const existing = await findExistingTab(url, storeId);
if (existing) {
if (settings.focusExisting) {
await browser.tabs.update(existing.id, { active: true });
await browser.windows.update(existing.windowId, { focused: true });
}
continue;
}
await browser.tabs.create({ url: url, cookieStoreId: storeId });
}
}
async function openTabInGroupByIndex(tab, index) {
if (!tab) return;
await openUrlInGroupByIndex(tab.url, index);
}
/* ---------- context menu ---------- */
const CONTEXT_DEFS = [
{ type: "tab", rootId: "root-tab", titleKey: "menuRootTitle" },
{ type: "link", rootId: "root-link", titleKey: "menuRootTitleLink" }
];
async function rebuildContextMenus() {
try { await browser.contextMenus.removeAll(); } catch (_) { }
const groups = await getGroups();
for (const ctx of CONTEXT_DEFS) {
browser.contextMenus.create({
id: ctx.rootId,
title: t(ctx.titleKey),
contexts: [ctx.type]
});
if (groups.length === 0) {
browser.contextMenus.create({
id: `no-groups-${ctx.type}`,
parentId: ctx.rootId,
title: t("noGroupsConfigured"),
enabled: false,
contexts: [ctx.type]
});
continue;
}
for (const g of groups) {
browser.contextMenus.create({
id: `group:${g.id}:${ctx.type}`,
parentId: ctx.rootId,
title: g.name || t("unnamedGroup"),
contexts: [ctx.type]
});
}
browser.contextMenus.create({
id: `sep-${ctx.type}`,
parentId: ctx.rootId,
type: "separator",
contexts: [ctx.type]
});
browser.contextMenus.create({
id: `open-options-${ctx.type}`,
parentId: ctx.rootId,
title: t("editGroupsMenuItem"),
contexts: [ctx.type]
});
}
}
browser.contextMenus.onClicked.addListener(async (info, tab) => {
if (String(info.menuItemId).startsWith("open-options-")) {
browser.runtime.openOptionsPage();
return;
}
const url = info.linkUrl || (tab && tab.url);
if (!url) return;
if (!String(info.menuItemId).startsWith("group:")) return;
// Format is "group:GROUP_ID:TYPE"
const parts = String(info.menuItemId).split(":");
if (parts.length < 3) return;
const groupId = parts[1];
const groups = await getGroups();
const group = groups.find(g => g.id === groupId);
if (!group) return;
const index = groups.indexOf(group);
await openUrlInGroupByIndex(url, index);
});
/* ---------- keyboard shortcuts ---------- */
browser.commands.onCommand.addListener(async (command) => {
const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
if (!tab) return;
if (command === "open-group-1") await openTabInGroupByIndex(tab, 0);
if (command === "open-group-2") await openTabInGroupByIndex(tab, 1);
if (command === "open-group-3") await openTabInGroupByIndex(tab, 2);
});
/* ---------- init ---------- */
(async () => {
await ensureInitialData();
await rebuildContextMenus();
})();
browser.storage.onChanged.addListener((changes, area) => {
if (area === "local" && changes[STORAGE_KEY]) {
rebuildContextMenus();
}
});