-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
401 lines (373 loc) · 14.3 KB
/
Copy pathbackground.js
File metadata and controls
401 lines (373 loc) · 14.3 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Import Registries in worker context
if (typeof importScripts === 'function') {
importScripts('utils/site_profiles_registry.js');
importScripts('utils/skills_registry.js');
}
const chromeAPI = typeof chrome !== 'undefined' ? chrome : browser;
let managerWindowId = null;
// Milestone 1.2: Ephemeral Tab Registry
const tabTracker = {
activeTabs: new Map(), // tabId -> { url, title, windowId }
lastCreatedTabId: null,
eventsLog: {
activatedCount: 0,
lifecycleCount: 0
}
};
// Startup Tab Scan
function runStartupTabScan() {
chromeAPI.tabs.query({}, (tabs) => {
if (chromeAPI.runtime.lastError || !tabs) return;
tabTracker.activeTabs.clear();
for (const tab of tabs) {
if (tab.id) {
tabTracker.activeTabs.set(tab.id, {
url: tab.url || "",
title: tab.title || "",
windowId: tab.windowId
});
}
}
console.log(`[Background] Startup tab scan complete. Registered ${tabTracker.activeTabs.size} tabs.`);
});
}
// Run immediately on background start
runStartupTabScan();
// Tab lifecycle listeners
chromeAPI.tabs.onCreated.addListener((tab) => {
if (!tab.id) return;
tabTracker.activeTabs.set(tab.id, {
url: tab.url || "",
title: tab.title || "",
windowId: tab.windowId
});
tabTracker.lastCreatedTabId = tab.id;
tabTracker.eventsLog.lifecycleCount++;
console.log(`[Background] Tab created: ${tab.id}. Lifecycle count: ${tabTracker.eventsLog.lifecycleCount}`);
});
chromeAPI.tabs.onRemoved.addListener((tabId) => {
if (tabTracker.activeTabs.has(tabId)) {
tabTracker.activeTabs.delete(tabId);
tabTracker.eventsLog.lifecycleCount++;
console.log(`[Background] Tab removed: ${tabId}. Lifecycle count: ${tabTracker.eventsLog.lifecycleCount}`);
}
});
chromeAPI.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (!tabId) return;
tabTracker.activeTabs.set(tabId, {
url: tab.url || "",
title: tab.title || "",
windowId: tab.windowId
});
tabTracker.eventsLog.lifecycleCount++;
console.log(`[Background] Tab updated: ${tabId}. Lifecycle count: ${tabTracker.eventsLog.lifecycleCount}`);
});
chromeAPI.tabs.onActivated.addListener((activeInfo) => {
tabTracker.eventsLog.activatedCount++;
console.log(`[Background] Tab activated: ${activeInfo.tabId}. Activated count: ${tabTracker.eventsLog.activatedCount}`);
});
// Initialize the extension when installed or updated
chromeAPI.runtime.onInstalled.addListener(() => {
console.log("Workspace Manager Extension Installed.");
runStartupTabScan();
const defaultProfiles = {
chatgpt: {
siteName: "ChatGPT",
domain: "chatgpt.com",
newChatUrl: "https://chatgpt.com/",
selectors: {
inputSelector: "#prompt-textarea",
sendButtonSelector: "button[data-testid*=\"send\" i], button[aria-label*=\"send\" i]",
newChatButtonSelector: "a[href=\"/\"], button[aria-label*=\"new chat\" i]",
copyButtonSelector: "button[data-testid$=\"copy-turn-action-button\"], button[aria-label*=\"copy\" i]",
userCopyButtonSelector: "button[aria-label*=\"copy message\" i]",
stopButtonSelector: "button[data-testid*=\"stop-button\" i], button[aria-label*=\"stop generating\" i]",
messageTurnSelector: "article, [data-testid^=\"conversation-turn\"]"
},
flags: {
useEnterFallback: false,
postSubmitDelayMs: 0
}
},
claude: {
siteName: "Claude",
domain: "claude.ai",
newChatUrl: "https://claude.ai/new",
selectors: {
inputSelector: "div[contenteditable=\"true\"]",
sendButtonSelector: "button[aria-label*=\"send\" i], button[data-testid*=\"send\" i], button:has(svg polygon)",
newChatButtonSelector: "a[href*=\"/new\" i], [data-testid*=\"new-chat\" i]",
copyButtonSelector: "button[data-testid*=\"copy\" i], button[aria-label*=\"copy\" i]",
userCopyButtonSelector: "div.items-end button[data-testid=\"action-bar-copy\"], div.items-end button[aria-label*=\"copy\" i]",
stopButtonSelector: "button[aria-label*=\"stop\" i]",
messageTurnSelector: "div.font-claude-message, div[data-testid*=\"message\" i], div.chat-and-document, [class*=\"conversation-turn\" i]"
},
flags: {
useEnterFallback: true,
postSubmitDelayMs: 1500
}
},
gemini: {
siteName: "Gemini",
domain: "gemini.google.com",
newChatUrl: "https://gemini.google.com/app",
selectors: {
inputSelector: "div[contenteditable=\"true\"]",
sendButtonSelector: "button[aria-label*=\"send\" i], button.send-button",
newChatButtonSelector: "a[href*=\"/app\" i][aria-label*=\"new\" i], [aria-label*=\"new chat\" i], [data-testid*=\"new-chat\" i], button.new-chat-button",
copyButtonSelector: "button[aria-label*=\"copy\" i]",
userCopyButtonSelector: "button[aria-label*=\"copy prompt\" i]",
stopButtonSelector: "button[aria-label*=\"stop\" i]",
messageTurnSelector: "response-container, .response-container, .query-container, message-content, div.conversation-turn"
},
flags: {
useEnterFallback: true,
postSubmitDelayMs: 0
}
},
copilot: {
siteName: "Copilot",
domain: "copilot.microsoft.com",
newChatUrl: "https://copilot.microsoft.com/",
selectors: {
inputSelector: "textarea, #searchbar",
sendButtonSelector: "button[aria-label*=\"submit\" i], button[aria-label*=\"send\" i]",
newChatButtonSelector: "button[aria-label*=\"new\" i]",
copyButtonSelector: ".group\\/ai-message button[aria-label*=\"copy\" i], [data-testid=\"ai-message\" i] button[aria-label*=\"copy\" i]",
userCopyButtonSelector: ".group\\/user-message button[aria-label*=\"copy\" i], [data-testid=\"user-message\" i] button[aria-label*=\"copy\" i]",
stopButtonSelector: "button[data-testid*=\"stop\" i], button[aria-label*=\"interrupt\" i], button#stop-responding-button, button[id*=\"stop\" i], button[aria-label*=\"stop\" i]",
messageTurnSelector: "div.chat-message, div.message-bubble, div.group\\/ai-message, div.group\\/user-message, [data-testid=\"ai-message\" i], [data-testid=\"user-message\" i]"
},
flags: {
useEnterFallback: true,
postSubmitDelayMs: 0
}
}
};
// Seed storage.local with default preferredMode and siteProfiles
chromeAPI.storage.local.set({
preferredMode: 'window',
siteProfiles: defaultProfiles
});
// For Chrome: Configure default behavior for the side panel API if available
if (chromeAPI.sidePanel && typeof chromeAPI.sidePanel.setPanelBehavior === 'function') {
// We don't want the side panel to open on action click by default,
// because we default to a detached window.
chromeAPI.sidePanel.setPanelBehavior({ openPanelOnActionClick: false })
.catch((error) => console.error("Error setting panel behavior:", error));
}
});
// Listen to the toolbar extension icon click
chromeAPI.action.onClicked.addListener(async (tab) => {
const data = await chromeAPI.storage.local.get(['preferredMode']);
const preferredMode = data.preferredMode || 'window';
if (preferredMode === 'panel') {
openSidePanel(tab);
} else {
openDetachedWindow();
}
});
// Function to open the side panel
async function openSidePanel(tab, allowFallback = true) {
// Try Chrome sidePanel API
if (chromeAPI.sidePanel && typeof chromeAPI.sidePanel.open === 'function') {
try {
await chromeAPI.sidePanel.open({ windowId: tab.windowId });
closeDetachedWindow();
} catch (err) {
console.error("Failed to open side panel via API:", err);
if (allowFallback) openDetachedWindow();
}
}
// Try Firefox sidebarAction API
else if (chromeAPI.sidebarAction && typeof chromeAPI.sidebarAction.open === 'function') {
try {
await chromeAPI.sidebarAction.open();
closeDetachedWindow();
} catch (err) {
console.error("Failed to open Firefox sidebar programmatically:", err);
if (allowFallback) openDetachedWindow();
}
}
// Fallback
else {
console.log("Side panel/sidebar APIs not supported or accessible in this browser.");
if (allowFallback) openDetachedWindow();
}
}
// Function to open the detached popup window
async function openDetachedWindow() {
// If the window is already open, try to focus it
if (managerWindowId !== null) {
try {
await chromeAPI.windows.update(managerWindowId, { focused: true });
return;
} catch (e) {
// Window was closed manually by the user
managerWindowId = null;
}
}
// Create new detached window (popup type to hide toolbar/bookmarks)
chromeAPI.windows.create({
url: chromeAPI.runtime.getURL("ui/manager.html?mode=window"),
type: "popup",
width: 850,
height: 600
}, (win) => {
if (win) {
managerWindowId = win.id;
}
});
}
// Function to close the detached window
function closeDetachedWindow() {
if (managerWindowId !== null) {
chromeAPI.windows.remove(managerWindowId, () => {
managerWindowId = null;
});
} else {
// Fallback: search all windows to find the manager window and close it
try {
chromeAPI.windows.getAll({ populate: true }, (windows) => {
if (chromeAPI.runtime.lastError || !windows) return;
const managerWin = windows.find(win =>
win.tabs && win.tabs.some(tab => tab.url && tab.url.includes("manager.html?mode=window"))
);
if (managerWin) {
chromeAPI.windows.remove(managerWin.id);
}
});
} catch (e) {
console.warn("Could not query windows:", e);
}
}
}
// Message Bus Dispatcher
chromeAPI.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("Background received message:", message);
if (message.action === 'querySkills') {
if (typeof SkillsRegistry !== 'undefined') {
SkillsRegistry.load().then(() => {
const matches = SkillsRegistry.getMatchingSkills(message.prefix, 6);
sendResponse({ matches });
}).catch(err => {
console.error("Failed to query skills in background:", err);
sendResponse({ matches: [] });
});
} else {
sendResponse({ matches: [] });
}
return true; // asynchronous response
}
if (message.action === 'getTestTrackerLogs') {
sendResponse({
activatedCount: tabTracker.eventsLog.activatedCount,
lifecycleCount: tabTracker.eventsLog.lifecycleCount,
lastCreatedTabId: tabTracker.lastCreatedTabId,
callerTabId: sender.tab ? sender.tab.id : null
});
return true;
}
if (message.action === 'switchTab') {
const { tabId } = message.payload;
chromeAPI.tabs.update(tabId, { active: true }, (tab) => {
if (chromeAPI.runtime.lastError || !tab) {
sendResponse({ status: "error", error: chromeAPI.runtime.lastError?.message });
return;
}
chromeAPI.windows.update(tab.windowId, { focused: true }, () => {
sendResponse({ status: "success" });
});
});
return true;
}
if (message.action === 'switchViewMode') {
const targetMode = message.payload.mode;
chromeAPI.storage.local.set({ preferredMode: targetMode }, () => {
if (targetMode === 'panel') {
// Switch to side panel: open it for the current window and close the detached window
if (sender.tab && sender.tab.windowId) {
openSidePanel(sender.tab, false);
} else {
// If message is sent from popup window, we query the active tab's window
chromeAPI.tabs.query({ active: true, currentWindow: false }, (tabs) => {
// Find a tab that is not in our popup window (our popup has no normal tabs)
const activeTab = tabs.find(t => t.url && !t.url.includes("manager.html"));
if (activeTab) {
openSidePanel(activeTab, false);
} else {
// Fallback: query active tab in any window
chromeAPI.tabs.query({ active: true }, (allActiveTabs) => {
const targetTab = allActiveTabs.find(t => t.url && !t.url.includes("manager.html"));
if (targetTab) {
openSidePanel(targetTab, false);
} else {
console.error("Could not find a valid window tab to attach side panel.");
}
});
}
});
}
} else if (targetMode === 'window') {
// Switch to detached window: open it and let the side panel be closed or stay
openDetachedWindow();
}
sendResponse({ status: "success" });
});
return true; // Keep message channel open for async response
}
if (message.action === 'closeDetached') {
closeDetachedWindow();
sendResponse({ status: "success" });
return true;
}
if (message.action === 'bridgeCommand') {
const { tabId, command, payload } = message.payload;
ensureContentScript(tabId).then(() => {
chromeAPI.tabs.sendMessage(tabId, { action: `bridge_${command}`, payload }, (response) => {
if (chromeAPI.runtime.lastError) {
sendResponse({ status: "error", error: chromeAPI.runtime.lastError.message });
} else {
sendResponse(response);
}
});
}).catch(err => {
sendResponse({ status: "error", error: err.message });
});
return true; // Keep channel open
}
// Forward observation state changes back to manager UI
if (message.action === 'bridge_state_change') {
if (sender && sender.tab) {
message.payload = message.payload || {};
message.payload.tabId = sender.tab.id;
}
chromeAPI.runtime.sendMessage(message).catch(() => {});
return false;
}
return false;
});
// Dynamic content script loader validation helper
async function ensureContentScript(tabId) {
try {
const response = await new Promise((resolve, reject) => {
chromeAPI.tabs.sendMessage(tabId, { action: "bridge_ping" }, (res) => {
if (chromeAPI.runtime.lastError || !res) {
reject(chromeAPI.runtime.lastError);
} else {
resolve(res);
}
});
});
if (response && response.status === "pong") return true;
} catch (e) {
// Content script not loaded or handshake failed
}
// Inject scripts dynamically
await chromeAPI.scripting.executeScript({
target: { tabId },
files: ["bridge.js", "content.js"]
});
console.log(`[Background] Dynamically injected content scripts into tab ${tabId}.`);
return true;
}