-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
100 lines (93 loc) · 3.71 KB
/
background.js
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
import { groupTabsBySimilarity } from "./ml-model.js";
import { queryTabs, ensureArray } from "./utils/tabUtils.js";
import { loadWorkspaces, saveWorkspaces, generateWorkspaceName } from "./utils/workspaceUtils.js";
import { groupExistingWorkspaceTabsInCurrentWindow, ungroupWorkspaceTabs } from "./utils/groupUtils.js";
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.action) {
case "analyze_tabs":
chrome.tabs.query({}, async (rawTabs) => {
try {
const validTabs = ensureArray(rawTabs).filter(tab =>
tab.url &&
!tab.url.startsWith("chrome://") &&
!tab.url.startsWith("chrome-extension://")
);
if (validTabs.length === 0) {
throw new Error("No valid tabs found");
}
const groups = groupTabsBySimilarity(validTabs);
const workspaces = groups.map(group => ({
name: generateWorkspaceName(group.tabs),
tabs: group.tabs,
createdAt: new Date().toISOString()
}));
await saveWorkspaces(workspaces);
for (const workspace of workspaces) {
await groupExistingWorkspaceTabsInCurrentWindow(workspace);
}
sendResponse({ success: true, workspaces });
} catch (error) {
console.error("Tab analysis error:", error);
sendResponse({
error: error.message || "Failed to analyze tabs",
details: error.toString()
});
}
});
return true;
case "get_workspaces":
loadWorkspaces()
.then(workspaces => sendResponse({ success: true, workspaces }))
.catch(error => {
console.error("Failed to load workspaces:", error);
sendResponse({ error: "Failed to load workspaces" });
});
return true;
case "switch_workspace_sandboxed":
loadWorkspaces()
.then(async (workspaces) => {
const workspace = workspaces[message.workspaceIndex];
if (!workspace) throw new Error("Workspace not found");
const newWindow = await chrome.windows.create({
url: chrome.runtime.getURL("sandbox.html") + "?workspaceIndex=" + message.workspaceIndex,
focused: true,
state: "maximized"
});
const initialTabs = await queryTabs({ windowId: newWindow.id });
if (initialTabs.length > 1) await chrome.tabs.remove(initialTabs[0].id);
return { success: true };
})
.then(result => sendResponse(result))
.catch(error => {
console.error("Sandboxed workspace switch failed:", error);
sendResponse({ error: error.message });
});
return true;
case "delete_workspace":
loadWorkspaces()
.then(async (workspaces) => {
const workspace = workspaces[message.workspaceIndex];
if (!workspace) throw new Error("Workspace not found");
await ungroupWorkspaceTabs(workspace);
workspaces.splice(message.workspaceIndex, 1);
await saveWorkspaces(workspaces);
sendResponse({ success: true, workspaces });
})
.catch(error => {
console.error("Failed to delete workspace:", error);
sendResponse({ error: error.message });
});
return true;
case "save_workspaces":
saveWorkspaces(message.workspaces)
.then(() => sendResponse({ success: true }))
.catch(error => {
console.error("Failed to save workspaces:", error);
sendResponse({ error: "Failed to save workspaces" });
});
return true;
default:
console.warn("Unknown action:", message.action);
sendResponse({ error: "Unknown action" });
}
});