-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewtab.js
More file actions
603 lines (523 loc) · 19.9 KB
/
Copy pathnewtab.js
File metadata and controls
603 lines (523 loc) · 19.9 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
(function () {
'use strict';
const DEFAULT_ORG = '个人';
// 元素
const orgListEl = document.getElementById('orgList');
const orgNameEl = document.getElementById('orgName');
const searchInput = document.getElementById('search');
const myCollectionsBtn = document.getElementById('myCollections');
const starredBtn = document.getElementById('starredBtn');
const spaceListEl = document.getElementById('spaceList');
const addSpaceBtn = document.getElementById('addSpaceBtn');
const spaceTitleEl = document.getElementById('spaceTitle');
const colCountEl = document.getElementById('colCount');
const expandAllBtn = document.getElementById('expandAll');
const collapseAllBtn = document.getElementById('collapseAll');
const addCollectionBtn = document.getElementById('addCollectionBtn');
const railAddBtn = document.getElementById('railAdd');
const sectionsEl = document.getElementById('sections');
const emptyEl = document.getElementById('empty');
const noResultsEl = document.getElementById('noResults');
const searchOpenWrap = document.getElementById('searchOpenWrap');
const openResultsEl = document.getElementById('openResults');
const windowListEl = document.getElementById('windowList');
const exportBtn = document.getElementById('exportBtn');
const importBtn = document.getElementById('importBtn');
const importFile = document.getElementById('importFile');
// 状态
let collections = [];
let selectedOrg = null; // null = 全部组织
let selectedSpace = null; // null = 全部空间
let showStarred = false; // true = 只看星标收藏集
const collapsedIds = new Set();
const orgKey = c => c.org || '';
const orgDisplay = raw => raw || DEFAULT_ORG;
const spaceOf = c => c.space || '';
/* ---------- 工具 ---------- */
function faviconUrl(pageUrl) {
const u = new URL(chrome.runtime.getURL('/_favicon/'));
u.searchParams.set('pageUrl', pageUrl);
u.searchParams.set('size', '32');
return u.toString();
}
function faviconImg(url, title) {
const img = document.createElement('img');
img.src = faviconUrl(url);
img.alt = '';
img.addEventListener('error', () => {
const div = document.createElement('div');
div.className = 'letter-icon';
div.textContent = (title || url || '?').trim().charAt(0).toUpperCase();
img.replaceWith(div);
}, { once: true });
return img;
}
function shortUrl(url) {
return url.replace(/^https?:\/\//, '').replace(/\/$/, '');
}
function el(tag, className, text) {
const e = document.createElement(tag);
if (className) e.className = className;
if (text !== undefined) e.textContent = text;
return e;
}
/** 组织头像颜色:按名字哈希取色 */
function orgColor(name) {
let h = 0;
for (const ch of name) h = (h * 31 + ch.charCodeAt(0)) % 360;
return `hsl(${h}, 55%, 45%)`;
}
function visibleCollections() {
const visible = collections.filter(c =>
(selectedOrg === null || orgKey(c) === selectedOrg) &&
(showStarred ? !!c.starred
: (selectedSpace === null || spaceOf(c) === selectedSpace)));
// 星标收藏集置顶
return visible.sort((a, b) => (b.starred ? 1 : 0) - (a.starred ? 1 : 0));
}
/* ---------- 左一:组织栏 ---------- */
function renderRail() {
orgListEl.textContent = '';
const allBtn = el('div', 'org-avatar' + (selectedOrg === null ? ' active' : ''), '全');
allBtn.title = '全部组织';
allBtn.style.background = '#2c303a';
allBtn.addEventListener('click', () => {
selectedOrg = null;
selectedSpace = null;
showStarred = false;
render();
});
orgListEl.appendChild(allBtn);
const orgs = [...new Set(collections.map(orgKey))];
for (const org of orgs) {
const label = orgDisplay(org);
const avatar = el('div', 'org-avatar' + (selectedOrg === org ? ' active' : ''));
avatar.textContent = label.slice(0, 2);
avatar.title = label;
avatar.style.background = orgColor(label);
avatar.addEventListener('click', () => {
selectedOrg = selectedOrg === org ? null : org;
selectedSpace = null;
showStarred = false;
render();
});
orgListEl.appendChild(avatar);
}
}
/* ---------- 左二:空间面板 ---------- */
function renderSpaces() {
orgNameEl.textContent = selectedOrg === null ? '全部收藏集' : orgDisplay(selectedOrg);
const inOrg = collections.filter(c => selectedOrg === null || orgKey(c) === selectedOrg);
myCollectionsBtn.classList.toggle('active', !showStarred && selectedSpace === null);
myCollectionsBtn.innerHTML = '';
myCollectionsBtn.append('全部收藏集', el('span', 'count', String(inOrg.length)));
const starredCount = inOrg.filter(c => c.starred).length;
starredBtn.classList.toggle('active', showStarred);
starredBtn.innerHTML = '';
starredBtn.append('星标收藏集', el('span', 'count', String(starredCount)));
spaceListEl.textContent = '';
const spaceNames = [...new Set(inOrg.map(spaceOf))].filter(Boolean).sort((a, b) => a.localeCompare(b, 'zh-CN'));
for (const space of spaceNames) {
const count = inOrg.filter(c => spaceOf(c) === space).length;
const item = el('button', 'nav-item' + (!showStarred && selectedSpace === space ? ' active' : ''));
item.append(space, el('span', 'count', String(count)));
item.addEventListener('click', () => {
showStarred = false;
selectedSpace = selectedSpace === space ? null : space;
render();
});
spaceListEl.appendChild(item);
}
}
/* ---------- 收藏集头部:图标按钮与菜单 ---------- */
function iconBtn(text, tip, onClick) {
const b = el('button', 'icon-btn', text);
b.dataset.tip = tip;
b.addEventListener('click', (e) => {
e.stopPropagation();
onClick(e, b);
});
return b;
}
let openMenu = null;
function closeMenu() {
if (openMenu) {
openMenu.remove();
openMenu = null;
}
}
document.addEventListener('click', closeMenu);
function toggleColMenu(col, headerEl) {
if (openMenu) {
closeMenu();
return;
}
const menu = el('div', 'col-menu');
const items = [
['重命名', async () => {
const name = prompt('收藏集名称', col.name);
if (name && name.trim()) {
await Store.renameCollection(col.id, name.trim());
await load();
}
}],
['移动到空间', async () => {
const space = prompt('目标空间名称(留空 = 全部收藏集)', col.space || '');
if (space === null) return;
await Store.updateCollection(col.id, { space: space.trim() });
await load();
}],
['删除收藏集', async () => {
if (confirm(`删除收藏集「${col.name}」(${col.tabs.length} 个标签)?`)) {
await Store.deleteCollection(col.id);
await load();
}
}]
];
for (const [label, fn] of items) {
const item = el('button', 'menu-item', label);
item.addEventListener('click', async (e) => {
e.stopPropagation();
closeMenu();
await fn();
});
menu.appendChild(item);
}
headerEl.appendChild(menu);
openMenu = menu;
}
/* ---------- 标签编辑弹层 ---------- */
function toggleEditPopup(card, tab, onSave) {
if (openMenu) {
closeMenu();
return;
}
const panel = el('div', 'col-menu edit-popup');
panel.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); });
const titleInput = el('input', 'edit-input');
titleInput.type = 'text';
titleInput.placeholder = '标题';
titleInput.value = tab.title || '';
const urlInput = el('input', 'edit-input');
urlInput.type = 'text';
urlInput.placeholder = '链接(http:// 或 https:// 开头)';
urlInput.value = tab.url || '';
const errorEl = el('div', 'edit-error');
const actions = el('div', 'edit-actions');
const saveBtn = el('button', 'menu-item edit-save', '保存');
const cancelBtn = el('button', 'menu-item', '取消');
cancelBtn.addEventListener('click', closeMenu);
saveBtn.addEventListener('click', async () => {
try {
await onSave(titleInput.value.trim(), urlInput.value.trim());
closeMenu();
} catch (e) {
errorEl.textContent = e.message;
}
});
actions.append(saveBtn, cancelBtn);
panel.append(el('label', 'edit-label', '标题'), titleInput, el('label', 'edit-label', '链接'), urlInput, errorEl, actions);
card.appendChild(panel);
openMenu = panel;
urlInput.focus();
}
/* ---------- 中间:收藏集 ---------- */
function bookmarkCard(tab, options = {}) {
const card = el('a', 'bookmark-card');
card.href = tab.url;
card.target = '_blank';
card.rel = 'noopener';
card.appendChild(faviconImg(tab.url, tab.title));
const text = el('div', 'bc-text');
text.appendChild(el('div', 'bc-title', tab.title || tab.url));
text.appendChild(el('div', 'bc-url', shortUrl(tab.url)));
card.appendChild(text);
card.title = tab.url;
if (options.onEdit) {
const btn = el('button', 'edit', '✎');
btn.title = '编辑标题/链接';
btn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
toggleEditPopup(card, tab, options.onEdit);
});
card.appendChild(btn);
}
if (options.onRemove) {
const btn = el('button', 'remove', '×');
btn.title = '从收藏集移除';
btn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
options.onRemove();
});
card.appendChild(btn);
}
return card;
}
function colActions(col) {
const actions = el('div', 'col-actions');
actions.appendChild(iconBtn('↗', '在新窗口打开全部', () => {
if (col.tabs.length > 0) chrome.windows.create({ url: col.tabs.map(t => t.url) });
}));
actions.appendChild(iconBtn('⬇', '把当前窗口的标签保存到此收藏集', async () => {
const tabs = await chrome.tabs.query({ currentWindow: true });
const savable = tabs.filter(t => Store.isSavableUrl(t.url));
if (savable.length === 0) return;
await Store.addTabs(col.id, savable.map(t => ({ title: t.title, url: t.url })));
await load();
}));
const starBtn = iconBtn(col.starred ? '★' : '☆', col.starred ? '取消星标' : '设为星标', async () => {
await Store.updateCollection(col.id, { starred: !col.starred });
await load();
});
if (col.starred) starBtn.classList.add('starred-on');
actions.appendChild(starBtn);
actions.appendChild(iconBtn('⧉', '复制收藏集', async () => {
await Store.duplicateCollection(col.id);
await load();
}));
actions.appendChild(iconBtn('🔗', '复制全部链接', async (e, btn) => {
const text = col.tabs.map(t => t.url).join('\n');
try {
await navigator.clipboard.writeText(text);
btn.textContent = '✓';
setTimeout(() => { btn.textContent = '🔗'; }, 1200);
} catch (err) {
prompt('复制以下链接', text);
}
}));
actions.appendChild(iconBtn('⋮', '更多', (e, btn) => {
toggleColMenu(col, btn.closest('.collection-header'));
}));
return actions;
}
function renderMain(query) {
const visible = visibleCollections();
const q = (query || '').trim().toLowerCase();
spaceTitleEl.textContent = showStarred ? '星标收藏集' : (selectedSpace || selectedOrg || '全部收藏集');
colCountEl.textContent = `| ${visible.length} 个收藏集`;
sectionsEl.textContent = '';
let anyCard = false;
for (const col of visible) {
const tabs = q
? col.tabs.filter(t => (t.title || '').toLowerCase().includes(q) || (t.url || '').toLowerCase().includes(q))
: col.tabs;
if (q && tabs.length === 0) continue;
const section = el('section', 'collection' + (collapsedIds.has(col.id) && !q ? ' collapsed' : ''));
const header = el('div', 'collection-header');
header.appendChild(el('span', 'caret', '▾'));
header.appendChild(el('h3', '', col.name));
if (col.starred) header.appendChild(el('span', 'star-mark', '★'));
header.appendChild(el('span', 'count', `${col.tabs.length} 个`));
header.appendChild(colActions(col));
header.addEventListener('click', () => {
if (collapsedIds.has(col.id)) collapsedIds.delete(col.id);
else collapsedIds.add(col.id);
section.classList.toggle('collapsed');
});
section.appendChild(header);
const cards = el('div', 'cards');
tabs.forEach((tab) => {
cards.appendChild(bookmarkCard(tab, {
onEdit: async (title, url) => {
await Store.updateTab(col.id, col.tabs.indexOf(tab), { title, url });
await load();
},
onRemove: async () => {
await Store.deleteTab(col.id, col.tabs.indexOf(tab));
await load();
}
}));
});
if (tabs.length > 0) anyCard = true;
section.appendChild(cards);
sectionsEl.appendChild(section);
}
emptyEl.hidden = collections.length > 0;
noResultsEl.hidden = !(q && !anyCard && openResultsEl.children.length === 0);
}
/* ---------- 搜索 ---------- */
let debounceTimer;
searchInput.addEventListener('input', () => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(runSearch, 120);
});
async function runSearch() {
const q = searchInput.value.trim().toLowerCase();
if (!q) {
searchOpenWrap.hidden = true;
openResultsEl.textContent = '';
renderMain('');
return;
}
const openTabs = await chrome.tabs.query({});
const selfPrefix = chrome.runtime.getURL('');
const openMatches = openTabs.filter(t =>
t.url && !t.url.startsWith(selfPrefix) &&
((t.title || '').toLowerCase().includes(q) || (t.url || '').toLowerCase().includes(q)));
openResultsEl.textContent = '';
for (const t of openMatches) {
const card = bookmarkCard({ title: t.title, url: t.url });
card.addEventListener('click', (e) => {
e.preventDefault();
chrome.tabs.update(t.id, { active: true });
chrome.windows.update(t.windowId, { focused: true });
});
openResultsEl.appendChild(card);
}
searchOpenWrap.hidden = openMatches.length === 0;
renderMain(q);
}
document.addEventListener('keydown', (e) => {
if (e.key === '/' && document.activeElement !== searchInput) {
e.preventDefault();
searchInput.focus();
} else if (e.key === 'Escape' && document.activeElement === searchInput) {
searchInput.value = '';
runSearch();
searchInput.blur();
}
});
/* ---------- 右栏:打开的标签 ---------- */
async function renderOpenTabs() {
const [tabs, windows] = await Promise.all([
chrome.tabs.query({}),
chrome.windows.getAll({ windowTypes: ['normal'] })
]);
const selfPrefix = chrome.runtime.getURL('');
const winIds = windows.map(w => w.id).sort((a, b) => a - b);
windowListEl.textContent = '';
winIds.forEach((winId, i) => {
const winTabs = tabs.filter(t => t.windowId === winId && t.url && !t.url.startsWith(selfPrefix));
if (winTabs.length === 0) return;
const block = el('div', 'window-block');
const header = el('div', 'window-header');
header.appendChild(el('span', '', `窗口 ${i + 1}`));
header.appendChild(el('span', 'muted', `(${winTabs.length})`));
const actions = el('div', 'win-actions');
const saveBtn = el('button', '', '⬇');
saveBtn.title = '把该窗口保存为收藏集';
saveBtn.addEventListener('click', async () => {
const name = `窗口 ${i + 1} · ` + new Date().toLocaleString('zh-CN', {
month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit'
});
const savable = winTabs.filter(t => Store.isSavableUrl(t.url));
if (savable.length === 0) return;
await Store.addCollection(name, savable.map(t => ({ title: t.title, url: t.url })), {
org: selectedOrg === null ? '' : selectedOrg,
space: selectedSpace || ''
});
await load();
});
actions.appendChild(saveBtn);
header.appendChild(actions);
block.appendChild(header);
for (const t of winTabs) {
const row = el('div', 'open-tab-row' + (t.active ? ' active-tab' : ''));
row.appendChild(faviconImg(t.url, t.title));
row.appendChild(el('span', 'ot-title', t.title || t.url));
row.title = t.url;
row.addEventListener('click', () => {
chrome.tabs.update(t.id, { active: true });
chrome.windows.update(t.windowId, { focused: true });
});
block.appendChild(row);
}
windowListEl.appendChild(block);
});
}
let tabsDebounce;
function scheduleRenderOpenTabs() {
clearTimeout(tabsDebounce);
tabsDebounce = setTimeout(renderOpenTabs, 200);
}
for (const ev of ['onCreated', 'onRemoved', 'onUpdated', 'onMoved', 'onAttached', 'onDetached', 'onActivated']) {
chrome.tabs[ev].addListener(scheduleRenderOpenTabs);
}
chrome.windows.onRemoved.addListener(scheduleRenderOpenTabs);
chrome.windows.onCreated.addListener(scheduleRenderOpenTabs);
/* ---------- 顶部操作 ---------- */
myCollectionsBtn.addEventListener('click', () => {
selectedSpace = null;
showStarred = false;
render();
});
starredBtn.addEventListener('click', () => {
showStarred = !showStarred;
selectedSpace = null;
render();
});
expandAllBtn.addEventListener('click', () => {
collapsedIds.clear();
renderMain(searchInput.value);
});
collapseAllBtn.addEventListener('click', () => {
for (const c of visibleCollections()) collapsedIds.add(c.id);
renderMain(searchInput.value);
});
async function addCollection() {
const name = prompt('收藏集名称');
if (!name || !name.trim()) return;
await Store.addCollection(name.trim(), [], {
org: selectedOrg === null ? '' : selectedOrg,
space: selectedSpace || ''
});
await load();
}
addCollectionBtn.addEventListener('click', addCollection);
railAddBtn.addEventListener('click', addCollection);
addSpaceBtn.addEventListener('click', async () => {
const name = prompt('空间名称(会创建一个空收藏集占位,可随后重命名)');
if (!name || !name.trim()) return;
await Store.addCollection('未命名收藏集', [], {
org: selectedOrg === null ? '' : selectedOrg,
space: name.trim()
});
selectedSpace = name.trim();
showStarred = false;
await load();
});
/* ---------- 导入 / 导出 ---------- */
exportBtn.addEventListener('click', async () => {
const json = await Store.exportData();
const blob = new Blob([json], { type: 'application/json' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = `tabshelf-${new Date().toISOString().slice(0, 10)}.json`;
a.click();
URL.revokeObjectURL(a.href);
});
importBtn.addEventListener('click', () => importFile.click());
importFile.addEventListener('change', async () => {
const file = importFile.files[0];
importFile.value = '';
if (!file) return;
try {
const text = await file.text();
const mode = confirm(
'「确定」= 覆盖现有数据\n「取消」= 合并到现有数据'
) ? 'overwrite' : 'merge';
const n = await Store.importData(text, mode);
alert(`已导入 ${n} 个收藏集`);
selectedOrg = null;
selectedSpace = null;
showStarred = false;
await load();
} catch (e) {
alert('导入失败:' + e.message);
}
});
/* ---------- 启动 ---------- */
function render() {
renderRail();
renderSpaces();
renderMain(searchInput.value);
}
async function load() {
collections = await Store.getAll();
render();
}
load();
renderOpenTabs();
})();