-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
185 lines (157 loc) · 6.24 KB
/
Copy pathbackground.js
File metadata and controls
185 lines (157 loc) · 6.24 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
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";
import { getURL, createWindow, removeTabs, isFirefox, getStorage, getCurrentWindow } from "./utils/browserAPI.js";
import { captureTabScreenshot } from "./utils/preview.js";
async function loadSettings() {
return new Promise((resolve) => {
const storage = getStorage();
storage.get([
"tab_source",
"specific_window_id"
], (result) => {
resolve({
tabSource: result.tab_source || 'current-window',
specificWindowId: result.specific_window_id || null
});
});
});
}
const handleMessage = async (message, sender) => {
switch (message.action) {
case "analyze_tabs":
try {
const settings = await loadSettings();
const tabSource = settings.tabSource || 'current-window';
let queryOptions = {};
if (tabSource === 'current-window') {
const currentWindow = await getCurrentWindow();
queryOptions = { windowId: currentWindow.id };
} else if (tabSource === 'specific-window' && settings.specificWindowId) {
queryOptions = { windowId: parseInt(settings.specificWindowId) };
}
const rawTabs = await queryTabs(queryOptions);
const validTabs = ensureArray(rawTabs);
if (validTabs.length === 0) {
throw new Error("No tabs found");
}
const groups = await groupTabsBySimilarity(validTabs);
if (!Array.isArray(groups)) {
throw new Error("Expected an array of groups, but received " + typeof groups);
}
const workspaces = groups.map(group => ({
name: group.name,
tabs: group.tabs.map(apiTab => {
const originalTab = validTabs.find(tab => tab.url === apiTab.url && tab.title === apiTab.title);
return originalTab || apiTab; // Fallback to API tab if no match
}),
createdAt: new Date().toISOString()
}));
await saveWorkspaces(workspaces);
if (tabSource === 'current-window') {
for (const workspace of workspaces) {
await groupExistingWorkspaceTabsInCurrentWindow(workspace);
}
}
let sourceDescription = tabSource;
if (tabSource === 'specific-window' && settings.specificWindowId) {
sourceDescription = `specific-window-${settings.specificWindowId}`;
}
return { success: true, workspaces, source: sourceDescription };
} catch (error) {
console.error("Tab analysis error:", error);
return {
error: error.message || "Failed to analyze tabs",
details: error.toString()
};
}
case "get_workspaces":
try {
const workspaces = await loadWorkspaces();
return { success: true, workspaces };
} catch (error) {
console.error("Failed to load workspaces:", error);
return { error: "Failed to load workspaces" };
}
case "capture_tab_screenshot":
try {
const tabId = message.tabId;
const screenshot = await captureTabScreenshot(tabId);
return { success: true, screenshot };
} catch (error) {
console.error("Failed to capture screenshot:", error);
return { error: "Failed to capture screenshot" };
}
case "switch_workspace_sandboxed":
try {
const workspaces = await loadWorkspaces();
const workspace = workspaces[message.workspaceIndex];
if (!workspace) throw new Error("Workspace not found");
const newWindow = await createWindow({
url: getURL("sandbox.html") + "?workspaceIndex=" + message.workspaceIndex,
focused: true
});
const initialTabs = await queryTabs({ windowId: newWindow.id });
if (initialTabs.length > 1) await removeTabs(initialTabs[0].id);
return { success: true };
} catch (error) {
console.error("Sandboxed workspace switch failed:", error);
return { error: error.message };
}
case "delete_workspace":
try {
const workspaces = await loadWorkspaces();
const workspace = workspaces[message.workspaceIndex];
if (!workspace) throw new Error("Workspace not found");
await ungroupWorkspaceTabs(workspace);
workspaces.splice(message.workspaceIndex, 1);
await saveWorkspaces(workspaces);
return { success: true, workspaces };
} catch (error) {
console.error("Failed to delete workspace:", error);
return { error: error.message };
}
case "save_workspaces":
try {
await saveWorkspaces(message.workspaces);
return { success: true };
} catch (error) {
console.error("Failed to save workspaces:", error);
return { error: "Failed to save workspaces" };
}
case "settings_updated":
try {
return { success: true, message: "Settings updated successfully" };
} catch (error) {
console.error("Failed to process settings update:", error);
return { error: "Failed to process settings update" };
}
case "get_settings":
try {
const settings = await loadSettings();
return { success: true, settings };
} catch (error) {
console.error("Failed to load settings:", error);
return { error: "Failed to load settings" };
}
default:
console.warn("Unknown action:", message.action);
return { error: "Unknown action" };
}
};
if (isFirefox) {
browser.runtime.onMessage.addListener((message, sender) => {
return handleMessage(message, sender);
});
} else {
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
handleMessage(message, sender)
.then(response => sendResponse(response))
.catch(error => {
console.error("Error in message handler:", error);
sendResponse({ error: error.message || "Unknown error" });
});
return true;
});
}