forked from terrakok/FuzzyTabs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
310 lines (297 loc) · 11 KB
/
Copy pathbackground.js
File metadata and controls
310 lines (297 loc) · 11 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
(function(){
// Use browser.* if available, fallback to chrome.* for compatibility
const api = (typeof browser !== 'undefined') ? browser : chrome;
const DEBUG = false;
const log = (...args) => { if (!DEBUG) return; try { console.debug('[FuzzyTabs][background]', ...args); } catch (_) {} };
log('background loaded');
// Small helpers to deduplicate repeated tab activation code
function activateTabAndRespond(tabId, sendResponse) {
try {
api.tabs.update(tabId, { active: true }, () => {
const err = api.runtime && api.runtime.lastError;
if (err) {
log('tabs.update error', err);
sendResponse({ ok: false, error: String((err && err.message) || err) });
} else {
sendResponse({ ok: true });
}
});
} catch (e) {
sendResponse({ ok: false, error: String(e) });
}
}
function focusWindowThenActivate(windowId, tabId, sendResponse) {
try {
if (typeof windowId === 'number' && api.windows && api.windows.update) {
api.windows.update(windowId, { focused: true }, () => {
// ignore possible lastError on focusing
activateTabAndRespond(tabId, sendResponse);
});
} else {
// No windows API or no windowId, just activate the tab
activateTabAndRespond(tabId, sendResponse);
}
} catch (e) {
log('error focusing window', e);
// Try to activate anyway
activateTabAndRespond(tabId, sendResponse);
}
}
// Handle messages from content scripts
if (api && api.runtime && api.runtime.onMessage) {
api.runtime.onMessage.addListener((msg, sender, sendResponse) => {
try {
if (!msg || !msg.type) return; // not ours
if (msg.type === 'get-all-tabs') {
log('get-all-tabs request');
api.storage.local.get(['tabAccessTimes'], (result) => {
const accessTimes = result.tabAccessTimes || {};
api.tabs.query({}, (tabs) => {
try {
const data = (tabs || []).map(t => {
const lastAcc = accessTimes[t.id] || t.lastAccessed || 0;
return {
id: t.id,
title: t.title,
url: t.url,
favIconUrl: t.favIconUrl,
active: t.active,
windowId: t.windowId,
lastAccessed: lastAcc
};
});
sendResponse({ ok: true, tabs: data });
} catch (e) {
log('error mapping tabs', e);
sendResponse({ ok: false, error: String(e) });
}
});
});
return true; // keep the message channel open for async sendResponse
} else if (msg.type === 'get-all-bookmarks') {
log('get-all-bookmarks request');
try {
api.storage.local.get(['bookmarkAccessTimes'], (result) => {
const accessTimes = result.bookmarkAccessTimes || {};
api.bookmarks.getTree((tree) => {
try {
const items = [];
const flatten = (nodes) => {
for (const n of nodes) {
if (n.url) {
items.push({
id: n.id,
title: n.title,
url: n.url,
lastAccessed: accessTimes[n.url] || 0
});
}
if (n.children) flatten(n.children);
}
};
flatten(tree || []);
sendResponse({ ok: true, bookmarks: items });
} catch (e) {
log('error flattening bookmarks', e);
sendResponse({ ok: false, error: String(e) });
}
});
});
return true;
} catch (e) {
log('bookmarks.getTree threw', e);
sendResponse({ ok: false, error: String(e) });
}
} else if (msg.type === 'activate-tab') {
const tabId = msg && msg.tabId;
if (typeof tabId === 'number') {
log('activate-tab request', { tabId });
try {
const now = Date.now();
api.storage.local.get(['tabAccessTimes'], (result) => {
const accessTimes = result.tabAccessTimes || {};
accessTimes[tabId] = now;
api.storage.local.set({ tabAccessTimes: accessTimes }, () => {
api.tabs.get(tabId, (tabInfo) => {
const getErr = api.runtime && api.runtime.lastError;
if (getErr) {
log('tabs.get error', getErr);
activateTabAndRespond(tabId, sendResponse);
return;
}
const targetWindowId = tabInfo && tabInfo.windowId;
focusWindowThenActivate(targetWindowId, tabId, sendResponse);
});
});
});
return true; // async
} catch (e) {
log('tabs.update threw', e);
sendResponse({ ok: false, error: String(e) });
}
} else {
sendResponse({ ok: false, error: 'Invalid tabId' });
}
} else if (msg.type === 'open-bookmark') {
const url = msg && msg.url;
if (typeof url === 'string') {
log('open-bookmark request', { url });
try {
const now = Date.now();
api.storage.local.get(['bookmarkAccessTimes'], (result) => {
const accessTimes = result.bookmarkAccessTimes || {};
accessTimes[url] = now;
api.storage.local.set({ bookmarkAccessTimes: accessTimes }, () => {
api.tabs.create({ url }, (tab) => {
const err = api.runtime && api.runtime.lastError;
if (err) {
log('tabs.create error', err);
sendResponse({ ok: false, error: String(err && err.message || err) });
} else {
sendResponse({ ok: true });
}
});
});
});
return true; // async
} catch (e) {
log('tabs.create threw', e);
sendResponse({ ok: false, error: String(e) });
}
} else {
sendResponse({ ok: false, error: 'Invalid url' });
}
} else if (msg.type === 'close-tab') {
const tabId = msg && msg.tabId;
if (typeof tabId === 'number') {
log('close-tab request', { tabId });
try {
api.tabs.remove(tabId, () => {
const err = api.runtime && api.runtime.lastError;
if (err) {
log('tabs.remove error', err);
sendResponse({ ok: false, error: String(err && err.message || err) });
} else {
sendResponse({ ok: true });
}
});
return true; // async
} catch (e) {
log('tabs.remove threw', e);
sendResponse({ ok: false, error: String(e) });
}
} else {
sendResponse({ ok: false, error: 'Invalid tabId' });
}
}
} catch (e) {
log('onMessage handler error', e);
}
});
}
let searchWindowId = null;
function openCenteredPopup(mode) {
// If the window is already open, focus it and update the mode
if (searchWindowId !== null) {
api.windows.get(searchWindowId, (win) => {
const err = api.runtime && api.runtime.lastError;
if (err || !win) {
searchWindowId = null;
createWindow();
} else {
api.windows.update(searchWindowId, { focused: true }, () => {
api.storage.local.set({ mode: mode });
});
}
});
return;
}
createWindow();
function createWindow() {
api.storage.local.set({ mode: mode }, () => {
// Query last focused window to center relative to it
api.windows.getLastFocused({ populate: false }, (currentWin) => {
const width = 800;
const height = 560;
let left = 200;
let top = 150;
if (currentWin && currentWin.width && currentWin.height) {
left = Math.round(currentWin.left + (currentWin.width - width) / 2);
top = Math.round(currentWin.top + (currentWin.height - height) / 2);
}
api.windows.create({
url: api.runtime.getURL('app.html'),
type: 'popup',
width: width,
height: height,
left: left,
top: top,
focused: true
}, (win) => {
searchWindowId = win.id;
});
});
});
}
}
// Handle Command Shortcuts
if (api && api.commands && api.commands.onCommand) {
api.commands.onCommand.addListener((command) => {
if (command === 'open-tabs') {
openCenteredPopup('tabs');
} else if (command === 'open-bookmarks') {
openCenteredPopup('bookmarks');
}
});
}
// Handle Browser Action Toolbar Icon Click
if (api && api.browserAction && api.browserAction.onClicked) {
api.browserAction.onClicked.addListener(() => {
openCenteredPopup('tabs');
});
}
// Track closed windows to reset searchWindowId reference
if (api && api.windows && api.windows.onRemoved) {
api.windows.onRemoved.addListener((windowId) => {
if (windowId === searchWindowId) {
searchWindowId = null;
}
});
}
// Active tab tracking listeners
if (api && api.tabs) {
if (api.tabs.onActivated) {
api.tabs.onActivated.addListener((activeInfo) => {
const tabId = activeInfo.tabId;
const now = Date.now();
api.storage.local.get(['tabAccessTimes'], (result) => {
const accessTimes = result.tabAccessTimes || {};
accessTimes[tabId] = now;
api.storage.local.set({ tabAccessTimes: accessTimes });
});
});
}
if (api.tabs.onRemoved) {
api.tabs.onRemoved.addListener((tabId) => {
api.storage.local.get(['tabAccessTimes'], (result) => {
const accessTimes = result.tabAccessTimes || {};
delete accessTimes[tabId];
api.storage.local.set({ tabAccessTimes: accessTimes });
});
});
}
// Initialize access time for currently active tab
try {
api.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs && tabs[0]) {
const tabId = tabs[0].id;
api.storage.local.get(['tabAccessTimes'], (result) => {
const accessTimes = result.tabAccessTimes || {};
accessTimes[tabId] = Date.now();
api.storage.local.set({ tabAccessTimes: accessTimes });
});
}
});
} catch (_) {}
}
})();