-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
172 lines (137 loc) · 5.89 KB
/
Copy pathbackground.js
File metadata and controls
172 lines (137 loc) · 5.89 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
// Ghost Tab v1 - background.js
function normalizeUrl(url) {
if (!url) return null;
url = url.trim();
if (!/^https?:\/\//i.test(url)) url = "https://" + url;
try { new URL(url); return url; } catch { return null; }
}
// ── Mode 1: Ghost Sessions (incognito launchers) ───────────────────────────────
async function getGhostSessions() {
const r = await chrome.storage.local.get("ghostSessions");
return r.ghostSessions || [];
}
async function saveGhostSessions(sessions) {
await chrome.storage.local.set({ ghostSessions: sessions });
}
async function createGhostSession(name, urls) {
const sessions = await getGhostSessions();
const id = Date.now().toString(36) + Math.random().toString(36).slice(2, 5);
const session = { id, name, urls: urls.filter(Boolean), createdAt: Date.now() };
sessions.push(session);
await saveGhostSessions(sessions);
return session;
}
async function deleteGhostSession(id) {
const sessions = await getGhostSessions();
await saveGhostSessions(sessions.filter(s => s.id !== id));
}
async function updateGhostSession(id, name, urls) {
const sessions = await getGhostSessions();
const s = sessions.find(s => s.id === id);
if (s) { s.name = name; s.urls = urls.filter(Boolean); }
await saveGhostSessions(sessions);
}
function launchGhostSession(urls) {
// Open all URLs as tabs inside one new incognito window
const valid = urls.map(normalizeUrl).filter(Boolean);
if (!valid.length) valid.push(undefined); // blank incognito window
chrome.windows.create({ incognito: true, focused: true, url: valid });
}
// ── Mode 2: Workspaces (persistent tab groups) ────────────────────────────────
async function getWorkspaces() {
const r = await chrome.storage.local.get("workspaces");
return r.workspaces || [];
}
async function saveWorkspaces(ws) {
await chrome.storage.local.set({ workspaces: ws });
}
async function createWorkspace(name, urls) {
const ws = await getWorkspaces();
const id = Date.now().toString(36) + Math.random().toString(36).slice(2, 5);
const workspace = { id, name, urls: urls.filter(Boolean), createdAt: Date.now() };
ws.push(workspace);
await saveWorkspaces(ws);
return workspace;
}
async function deleteWorkspace(id) {
const ws = await getWorkspaces();
await saveWorkspaces(ws.filter(w => w.id !== id));
}
async function updateWorkspace(id, name, urls) {
const ws = await getWorkspaces();
const w = ws.find(w => w.id === id);
if (w) { w.name = name; w.urls = urls.filter(Boolean); }
await saveWorkspaces(ws);
}
function launchWorkspace(urls) {
const valid = urls.map(normalizeUrl).filter(Boolean);
if (!valid.length) return;
// Open first URL in a new normal window, rest as additional tabs
chrome.windows.create({ url: valid, focused: true });
}
// ── Save current tabs to workspace ────────────────────────────────────────────
async function saveCurrentTabs(name) {
const tabs = await chrome.tabs.query({ currentWindow: true });
const urls = tabs.map(t => t.url).filter(u => u && u.startsWith("http"));
return createWorkspace(name || "My Workspace", urls);
}
// ── Context menu ──────────────────────────────────────────────────────────────
chrome.runtime.onInstalled.addListener((details) => {
chrome.contextMenus.create({
id: "ghost-open-page",
title: "Open page in Ghost Tab (incognito)",
contexts: ["page"]
});
chrome.contextMenus.create({
id: "ghost-open-link",
title: "Open link in Ghost Tab (incognito)",
contexts: ["link"]
});
if (details.reason === "install") {
chrome.tabs.create({ url: chrome.runtime.getURL("pages/onboarding.html") });
}
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
const url = info.linkUrl || info.pageUrl || tab?.url;
launchGhostSession([url]);
});
// ── Message router ────────────────────────────────────────────────────────────
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
switch (msg.action) {
// Ghost sessions
case "getGhostSessions":
getGhostSessions().then(sendResponse); return true;
case "createGhostSession":
createGhostSession(msg.name, msg.urls).then(sendResponse); return true;
case "deleteGhostSession":
deleteGhostSession(msg.id).then(() => sendResponse({ ok: true })); return true;
case "updateGhostSession":
updateGhostSession(msg.id, msg.name, msg.urls).then(() => sendResponse({ ok: true })); return true;
case "launchGhostSession":
getGhostSessions().then(sessions => {
const s = sessions.find(s => s.id === msg.id);
if (s) launchGhostSession(s.urls);
sendResponse({ ok: true });
}); return true;
case "launchGhostUrls":
launchGhostSession(msg.urls);
sendResponse({ ok: true }); break;
// Workspaces
case "getWorkspaces":
getWorkspaces().then(sendResponse); return true;
case "createWorkspace":
createWorkspace(msg.name, msg.urls).then(sendResponse); return true;
case "deleteWorkspace":
deleteWorkspace(msg.id).then(() => sendResponse({ ok: true })); return true;
case "updateWorkspace":
updateWorkspace(msg.id, msg.name, msg.urls).then(() => sendResponse({ ok: true })); return true;
case "launchWorkspace":
getWorkspaces().then(ws => {
const w = ws.find(w => w.id === msg.id);
if (w) launchWorkspace(w.urls);
sendResponse({ ok: true });
}); return true;
case "saveCurrentTabs":
saveCurrentTabs(msg.name).then(sendResponse); return true;
}
});