Repository navigation
Expand file tree
/
Copy pathpix.html
More file actions
839 lines (839 loc) · 30.6 KB
/
Copy pathpix.html
File metadata and controls
839 lines (839 loc) · 30.6 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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=yes" />
<title>书签管理</title>
<link rel="icon" href="https://s3.bmp.ovh/imgs/2025/04/24/c767656a890464c5.webp" alt="Pi Network Logo" />
<style>
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
min-height: 100vh;
background-color: #f9f9f9;
user-select: none;
}
h1 {
font-size: 1.5rem;
margin-bottom: 20px;
text-align: center;
user-select: none;
cursor: default;
}
section#groupsContainer {
max-width: 600px;
width: 100%;
}
.group {
background: #fff;
border-radius: 8px;
margin-bottom: 20px;
padding: 10px 15px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}
.group-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
user-select: none;
}
.group-title {
font-size: 1.2em;
font-weight: bold;
cursor: pointer;
user-select: none;
}
.bookmark-list {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.bookmark {
display: flex;
align-items: center;
padding: 10px;
background: white;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
cursor: pointer;
transition: background 0.2s;
width: calc(50% - 10px);
max-width: none;
position: relative;
user-select: none;
}
.bookmark:hover {
background: #8b1db1 !important; /* 深紫色 */
color: #fff;
}
.bookmark:active {
background: #2d0040 !important; /* 更深的紫色 */
color: #fff;
}
.bookmark img {
width: 40px;
height: 40px;
margin-right: 10px;
border-radius: 4px;
background: #eee;
flex-shrink: 0;
}
.bookmark a {
text-decoration: none;
color: inherit;
flex: 1;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
user-select: text;
-webkit-touch-callout: none;
}
@media (min-width: 768px) {
.bookmark {
width: calc(33.33% - 10px);
}
}
#contextMenu, #groupContextMenu, #importExportMenu {
position: fixed;
background: white;
border: 1px solid #ccc;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
border-radius: 6px;
padding: 5px 0;
width: 160px;
display: none;
z-index: 9999;
user-select: none;
}
#contextMenu button, #groupContextMenu button, #importExportMenu button {
width: 100%;
padding: 8px 15px;
border: none;
background: none;
text-align: left;
font-size: 14px;
cursor: pointer;
}
#contextMenu button:hover, #groupContextMenu button:hover, #importExportMenu button:hover {
background-color: #f0f0f0;
}
#groupContextMenu button.delete, #importExportMenu button#clearBtn {
color: #d33;
}
#modalOverlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
display: none;
justify-content: center;
align-items: center;
z-index: 10000;
}
#modal {
background: white;
border-radius: 8px;
padding: 20px;
width: 90%;
max-width: 400px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}
#modal h2 {
margin-top: 0;
margin-bottom: 15px;
font-size: 1.2em;
}
#modal label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
#modal input[type=text] {
width: 100%;
padding: 8px 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
}
#modal .buttons {
text-align: right;
}
#modal .buttons button {
padding: 6px 15px;
margin-left: 10px;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
}
#modal .buttons .cancel {
background: #ccc;
color: #333;
}
#modal .buttons .save {
background: #4CAF50;
color: white;
}
#importInput {
display: none;
}
</style>
</head>
<body>
<h1>书签导航</h1>
<section id="groupsContainer"></section>
<div id="importExportMenu">
<button id="exportBtn">导出数据</button>
<button id="importBtn">导入数据</button>
<button id="clearBtn">清空数据</button>
</div>
<input type="file" id="importInput" accept=".json" />
<div id="contextMenu">
<button id="addBookmarkBtn">添加书签</button>
<button id="editBookmarkBtn">修改书签</button>
<button id="deleteBookmarkBtn">删除书签</button>
</div>
<div id="groupContextMenu">
<button id="addGroupFromMenuBtn">添加分组</button>
<button id="editGroupBtn">修改分组</button>
<button id="deleteGroupBtn" class="delete">删除分组</button>
</div>
<div id="modalOverlay">
<div id="modal">
<h2 id="modalTitle"></h2>
<label id="bookmarkTitleLabel" for="bookmarkTitleInput">标题</label>
<input type="text" id="bookmarkTitleInput" placeholder="请输入标题" />
<label id="bookmarkUrlLabel" for="bookmarkUrlInput">链接</label>
<input type="text" id="bookmarkUrlInput" placeholder="请输入链接" style="display:none;" />
<label id="bookmarkFaviconLabel" for="bookmarkFaviconInput">缩略图链接(可选)</label>
<input type="text" id="bookmarkFaviconInput" placeholder="自定义缩略图链接" style="display:none;" />
<div class="buttons">
<button class="cancel" id="modalCancelBtn">取消</button>
<button class="save" id="modalSaveBtn">保存</button>
</div>
</div>
</div>
<script>
const groupsContainer = document.getElementById('groupsContainer');
const contextMenu = document.getElementById('contextMenu');
const addBookmarkBtn = document.getElementById('addBookmarkBtn');
const editBookmarkBtn = document.getElementById('editBookmarkBtn');
const deleteBookmarkBtn = document.getElementById('deleteBookmarkBtn');
const groupContextMenu = document.getElementById('groupContextMenu');
const addGroupFromMenuBtn = document.getElementById('addGroupFromMenuBtn');
const editGroupBtn = document.getElementById('editGroupBtn');
const deleteGroupBtn = document.getElementById('deleteGroupBtn');
const modalOverlay = document.getElementById('modalOverlay');
const modalTitle = document.getElementById('modalTitle');
const bookmarkTitleInput = document.getElementById('bookmarkTitleInput');
const bookmarkUrlInput = document.getElementById('bookmarkUrlInput');
const bookmarkFaviconInput = document.getElementById('bookmarkFaviconInput');
const bookmarkTitleLabel = document.getElementById('bookmarkTitleLabel');
const bookmarkUrlLabel = document.getElementById('bookmarkUrlLabel');
const bookmarkFaviconLabel = document.getElementById('bookmarkFaviconLabel');
const modalCancelBtn = document.getElementById('modalCancelBtn');
const modalSaveBtn = document.getElementById('modalSaveBtn');
const importExportMenu = document.getElementById('importExportMenu');
const exportBtn = document.getElementById('exportBtn');
const importBtn = document.getElementById('importBtn');
const clearBtn = document.getElementById('clearBtn');
const importInput = document.getElementById('importInput');
let groups = [];
let currentGroup = null;
let currentBookmark = null;
let isEditingBookmark = false;
const STORAGE_KEY = 'pi_groups_data';
const LONG_PRESS_DURATION = 600;
let longPressTimer = null;
let isLongPress = false;
let moved = false;
function generateId() {
return 'id-' + Date.now() + '-' + Math.floor(Math.random() * 1000);
}
function createDefaultBookmarks() {
return [
{ id: generateId(), title: '来购酷买', url: 'https://lgkm.net', favicon: '' },
{ id: generateId(), title: '314159 商城', url: 'https://314159u.com', favicon: '' },
{ id: generateId(), title: '元气山谷', url: 'https://pipet.me', favicon: '' }
];
}
function createDefaultGroup() {
return {
id: generateId(),
name: '默认分组',
bookmarks: createDefaultBookmarks()
};
}
function resetToDefault() {
groups = [createDefaultGroup()];
}
function loadData() {
let data = localStorage.getItem(STORAGE_KEY);
if (data) {
try {
groups = JSON.parse(data);
if (!Array.isArray(groups)) throw new Error('数据格式不正确');
} catch (error) {
console.error("从本地存储加载数据时出错:", error);
resetToDefault();
}
} else {
resetToDefault();
}
render();
}
function saveData() {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(groups));
} catch (error) {
console.error("保存数据失败:", error);
}
}
async function getFaviconUrl(url, customFavicon) {
return new Promise(resolve => {
const defaultFavicon = 'https://via.placeholder.com/40?text=?';
if (customFavicon) {
resolve(customFavicon.trim());
return;
}
try {
const domain = new URL(url).hostname;
const cccyunUrl = `https://favicon.cccyun.cc/${url}`;
const img = new Image();
img.onload = () => resolve(cccyunUrl);
img.onerror = () => resolve(`https://www.google.com/s2/favicons?domain=${domain}`);
img.src = cccyunUrl;
} catch {
resolve(defaultFavicon);
}
});
}
async function createBookmarkElement(data, group) {
const div = document.createElement('div');
div.className = 'bookmark';
div.dataset.bookmarkId = data.id;
div.dataset.groupId = group.id;
const img = document.createElement('img');
img.alt = '图标';
img.src = data.favicon || 'https://via.placeholder.com/40?text=?';
img.onerror = () => {
img.src = 'https://via.placeholder.com/40?text=?';
};
const a = document.createElement('a');
a.href = data.url;
a.target = '_blank';
a.rel = 'noopener noreferrer';
a.textContent = data.title;
div.appendChild(img);
div.appendChild(a);
bindLongPressBookmark(div, group, data);
return div;
}
async function render() {
try {
groupsContainer.innerHTML = '';
for (const group of groups) {
const groupDiv = document.createElement('div');
groupDiv.className = 'group';
const header = document.createElement('div');
header.className = 'group-header';
const title = document.createElement('div');
title.className = 'group-title';
title.textContent = group.name;
bindLongPressGroup(title, group);
header.appendChild(title);
groupDiv.appendChild(header);
const bookmarkList = document.createElement('div');
bookmarkList.className = 'bookmark-list';
for (const bm of group.bookmarks) {
bm.favicon = await getFaviconUrl(bm.url, bm.favicon);
const bmEl = await createBookmarkElement(bm, group);
bookmarkList.appendChild(bmEl);
}
groupDiv.appendChild(bookmarkList);
groupsContainer.appendChild(groupDiv);
}
} catch (error) {
console.error("渲染过程中出错:", error);
alert('渲染页面失败,请检查控制台错误信息!');
} finally {
saveData();
}
}
function bindLongPressBookmark(el, group, bookmark) {
let startX = 0, startY = 0, clickHandled = false;
const handleClick = (e) => {
if (!moved && !isLongPress && !clickHandled) {
clickHandled = true;
e.preventDefault();
e.stopPropagation();
window.open(bookmark.url, '_blank');
setTimeout(() => { clickHandled = false; }, 100);
}
};
el.addEventListener('touchstart', e => {
moved = false; clickHandled = false;
startX = e.touches[0].clientX; startY = e.touches[0].clientY;
longPressTimer = setTimeout(() => {
isLongPress = true; currentGroup = group; currentBookmark = bookmark;
showContextMenu(startX, startY);
}, LONG_PRESS_DURATION);
}, { passive: false });
el.addEventListener('touchmove', e => {
const dx = Math.abs(e.touches[0].clientX - startX);
const dy = Math.abs(e.touches[0].clientY - startY);
if (dx > 10 || dy > 10) { moved = true; clearTimeout(longPressTimer); }
}, { passive: true });
el.addEventListener('touchend', e => {
clearTimeout(longPressTimer);
if (isLongPress) { e.preventDefault(); e.stopPropagation(); }
else { handleClick(e); }
isLongPress = false; moved = false;
});
el.addEventListener('mousedown', e => {
if (e.button === 0) {
moved = false; clickHandled = false;
startX = e.clientX; startY = e.clientY;
longPressTimer = setTimeout(() => {
isLongPress = true; currentGroup = group; currentBookmark = bookmark;
showContextMenu(startX, startY);
}, LONG_PRESS_DURATION);
}
});
el.addEventListener('mousemove', e => {
const dx = Math.abs(e.clientX - startX);
const dy = Math.abs(e.clientY - startY);
if (dx > 5 || dy > 5) { clearTimeout(longPressTimer); moved = true; }
});
el.addEventListener('mouseup', e => {
if (e.button === 0) {
clearTimeout(longPressTimer);
if (isLongPress) { e.preventDefault(); e.stopPropagation(); }
else { handleClick(e); }
isLongPress = false; moved = false;
}
});
el.addEventListener('contextmenu', e => {
e.preventDefault(); currentGroup = group; currentBookmark = bookmark;
showContextMenu(e.clientX, e.clientY);
});
const link = el.querySelector('a');
if (link) {
link.addEventListener('click', e => { e.preventDefault(); e.stopPropagation(); });
}
}
function bindLongPressGroup(el, group) {
let startX = 0, startY = 0;
el.addEventListener('touchstart', e => {
moved = false; startX = e.touches[0].clientX; startY = e.touches[0].clientY;
longPressTimer = setTimeout(() => {
isLongPress = true; currentGroup = group; currentBookmark = null;
showGroupContextMenu(startX, startY);
}, LONG_PRESS_DURATION);
}, { passive: false });
el.addEventListener('touchmove', e => {
const dx = Math.abs(e.touches[0].clientX - startX);
const dy = Math.abs(e.touches[0].clientY - startY);
if (dx > 10 || dy > 10) { moved = true; clearTimeout(longPressTimer); }
}, { passive: true });
el.addEventListener('touchend', e => {
clearTimeout(longPressTimer);
if (isLongPress) { e.preventDefault(); e.stopPropagation(); }
isLongPress = false; moved = false;
});
el.addEventListener('mousedown', e => {
moved = false; startX = e.clientX; startY = e.clientY;
longPressTimer = setTimeout(() => {
isLongPress = true; currentGroup = group; currentBookmark = null;
showGroupContextMenu(startX, startY);
}, LONG_PRESS_DURATION);
});
el.addEventListener('mousemove', e => {
const dx = Math.abs(e.clientX - startX);
const dy = Math.abs(e.clientY - startY);
if (dx > 5 || dy > 5) { clearTimeout(longPressTimer); moved = true; }
});
el.addEventListener('mouseup', e => {
clearTimeout(longPressTimer);
if (isLongPress) { e.preventDefault(); e.stopPropagation(); }
isLongPress = false; moved = false;
});
el.addEventListener('contextmenu', e => {
e.preventDefault(); currentGroup = group; currentBookmark = null;
showGroupContextMenu(e.clientX, e.clientY);
});
}
function showContextMenu(x, y) {
const menu = document.getElementById('contextMenu');
menu.style.display = 'block';
menu.style.left = x + 'px';
menu.style.top = y + 'px';
const onClickOutside = (event) => {
if (!menu.contains(event.target)) {
menu.style.display = 'none';
document.removeEventListener('mousedown', onClickOutside);
}
};
document.addEventListener('mousedown', onClickOutside);
}
function showGroupContextMenu(x, y) {
const menu = document.getElementById('groupContextMenu');
menu.style.display = 'block';
menu.style.left = x + 'px';
menu.style.top = y + 'px';
const onClickOutside = (event) => {
if (!menu.contains(event.target)) {
menu.style.display = 'none';
document.removeEventListener('mousedown', onClickOutside);
}
};
document.addEventListener('mousedown', onClickOutside);
}
function showImportExportMenu(x, y) {
const menu = document.getElementById('importExportMenu');
menu.style.display = 'block';
menu.style.left = x + 'px';
menu.style.top = y + 'px';
const onClickOutside = (event) => {
if (!menu.contains(event.target)) {
menu.style.display = 'none';
document.removeEventListener('mousedown', onClickOutside);
}
};
document.addEventListener('mousedown', onClickOutside);
}
exportBtn.addEventListener('click', () => {
try {
const data = groups.map(group => ({
name: group.name,
bookmarks: group.bookmarks.map(bookmark => ({
title: bookmark.title,
url: bookmark.url,
favicon: bookmark.favicon
}))
}));
const dataStr = JSON.stringify(data, null, 2);
const dataUri = 'data:application/json;charset=utf-8,' + encodeURIComponent(dataStr);
const exportFileDefaultName = 'bookmarks.json';
let linkElement = document.createElement('a');
linkElement.setAttribute('href', dataUri);
linkElement.setAttribute('download', exportFileDefaultName);
document.body.appendChild(linkElement);
linkElement.click();
document.body.removeChild(linkElement);
} catch (error) {
console.error("导出数据失败:", error);
alert('导出数据失败,请检查控制台错误信息!');
}
});
importBtn.addEventListener('click', () => {
importInput.value = '';
importInput.click();
});
importInput.addEventListener('change', (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
try {
const importedData = JSON.parse(e.target.result);
if (typeof importedData === 'object' && importedData !== null) {
let newGroups = [];
if (Array.isArray(importedData)) {
importedData.forEach(groupData => {
if (groupData && typeof groupData === 'object' && groupData.name && Array.isArray(groupData.bookmarks)) {
const newGroup = {
id: generateId(),
name: groupData.name,
bookmarks: []
};
groupData.bookmarks.forEach(bookmarkData => {
if (bookmarkData && typeof bookmarkData === 'object' && bookmarkData.title && bookmarkData.url) {
newGroup.bookmarks.push({
id: generateId(),
title: bookmarkData.title,
url: bookmarkData.url,
favicon: bookmarkData.favicon || ''
});
}
});
newGroups.push(newGroup);
}
});
} else {
for (const groupName in importedData) {
if (importedData.hasOwnProperty(groupName)) {
const bookmarks = importedData[groupName];
if (Array.isArray(bookmarks)) {
const newGroup = {
id: generateId(),
name: groupName,
bookmarks: []
};
bookmarks.forEach(bookmark => {
if (bookmark && typeof bookmark === 'object' && (bookmark.name || bookmark.title) && bookmark.url) {
newGroup.bookmarks.push({
id: generateId(),
title: bookmark.name || bookmark.title,
url: bookmark.url,
favicon: bookmark.favicon || ''
});
}
});
newGroups.push(newGroup);
}
}
}
}
if (newGroups.length === 0) {
alert('导入失败:未检测到有效的书签分组和书签。');
return;
}
groups = groups.concat(newGroups);
render();
alert('导入成功!');
} else {
alert('导入数据格式不正确。');
}
} catch (error) {
console.error("导入数据失败:", error);
alert('导入数据失败,请检查文件格式是否正确。');
}
}
reader.readAsText(file);
}
});
clearBtn.addEventListener('click', () => {
if (confirm('确定要清空所有书签数据并恢复到初始状态吗?此操作不可撤销!')) {
hideContextMenus();
resetToDefault();
saveData();
render();
alert('书签数据已清空并恢复到初始状态。');
}
});
function hideContextMenus() {
contextMenu.style.display = 'none';
groupContextMenu.style.display = 'none';
importExportMenu.style.display = 'none';
}
addBookmarkBtn.addEventListener('click', () => {
hideContextMenus();
isEditingBookmark = false;
modalTitle.textContent = '添加书签';
bookmarkTitleLabel.style.display = 'block';
bookmarkTitleInput.style.display = 'block';
bookmarkUrlLabel.style.display = 'block';
bookmarkUrlInput.style.display = 'block';
bookmarkFaviconLabel.style.display = 'block';
bookmarkFaviconInput.style.display = 'block';
bookmarkTitleInput.value = '';
bookmarkUrlInput.value = '';
bookmarkFaviconInput.value = '';
modalOverlay.style.display = 'flex';
});
editBookmarkBtn.addEventListener('click', () => {
hideContextMenus();
if (currentBookmark) {
isEditingBookmark = true;
modalTitle.textContent = '修改书签';
bookmarkTitleLabel.style.display = 'block';
bookmarkTitleInput.style.display = 'block';
bookmarkUrlLabel.style.display = 'block';
bookmarkUrlInput.style.display = 'block';
bookmarkFaviconLabel.style.display = 'block';
bookmarkFaviconInput.style.display = 'block';
bookmarkTitleInput.value = currentBookmark.title;
bookmarkUrlInput.value = currentBookmark.url;
bookmarkFaviconInput.value = currentBookmark.favicon;
modalOverlay.style.display = 'flex';
} else {
alert('请先长按选择要修改的书签');
}
});
deleteBookmarkBtn.addEventListener('click', () => {
hideContextMenus();
if (currentBookmark && currentGroup) {
try {
currentGroup.bookmarks = currentGroup.bookmarks.filter(bm => bm.id !== currentBookmark.id);
currentBookmark = null;
currentGroup = null;
saveData();
render();
} catch (error) {
console.error("删除书签失败:", error);
alert('删除书签失败,请查看控制台');
}
} else {
alert('请先长按选择要删除的书签');
}
});
// 关键:添加分组按钮事件,直接插入默认分组
addGroupFromMenuBtn.addEventListener('click', () => {
hideContextMenus();
if (!currentGroup) {
groups.push(createDefaultGroup());
} else {
const idx = groups.findIndex(g => g.id === currentGroup.id);
if (idx !== -1) {
groups.splice(idx + 1, 0, createDefaultGroup());
} else {
groups.push(createDefaultGroup());
}
}
saveData();
render();
});
editGroupBtn.addEventListener('click', () => {
hideContextMenus();
if (currentGroup) {
isEditingBookmark = false;
modalTitle.textContent = '修改分组';
bookmarkTitleLabel.textContent = '分组名称';
bookmarkTitleLabel.style.display = 'block';
bookmarkTitleInput.style.display = 'block';
bookmarkUrlLabel.style.display = 'none';
bookmarkUrlInput.style.display = 'none';
bookmarkFaviconLabel.style.display = 'none';
bookmarkFaviconInput.style.display = 'none';
bookmarkTitleInput.value = currentGroup.name;
modalOverlay.style.display = 'flex';
} else {
alert('请先长按选择要修改的分组');
}
});
deleteGroupBtn.addEventListener('click', () => {
hideContextMenus();
if (currentGroup) {
try {
groups = groups.filter(g => g.id !== currentGroup.id);
currentGroup = null;
if (groups.length === 0) {
resetToDefault();
}
saveData();
render();
} catch (error) {
console.error("删除分组失败:", error);
alert('删除分组失败,请查看控制台');
}
} else {
alert('请先长按选择要删除的分组');
}
});
modalCancelBtn.addEventListener('click', () => {
modalOverlay.style.display = 'none';
});
modalSaveBtn.addEventListener('click', () => {
if (isEditingBookmark) {
if (currentBookmark && currentGroup) {
const titleVal = bookmarkTitleInput.value.trim();
const urlVal = bookmarkUrlInput.value.trim();
if (!titleVal || !urlVal) {
alert('标题和链接不能为空!');
return;
}
currentBookmark.title = titleVal;
currentBookmark.url = urlVal;
currentBookmark.favicon = bookmarkFaviconInput.value.trim();
}
} else {
if (bookmarkUrlLabel.style.display === 'none') {
const newName = bookmarkTitleInput.value.trim();
if (!newName) {
alert('分组名称不能为空!');
return;
}
if (groups.some(g => g.name === newName && g.id !== currentGroup.id)) {
alert('分组名称已存在!');
return;
}
currentGroup.name = newName;
} else {
const titleVal = bookmarkTitleInput.value.trim();
const urlVal = bookmarkUrlInput.value.trim();
if (!titleVal || !urlVal) {
alert('标题和链接不能为空!');
return;
}
if (!currentGroup) {
alert('当前分组不存在,无法添加书签');
return;
}
const newBookmark = {
id: generateId(),
title: titleVal,
url: urlVal,
favicon: bookmarkFaviconInput.value.trim()
};
const currentBookmarkIndex = currentGroup.bookmarks.findIndex(b => b.id === currentBookmark.id);
if (currentBookmarkIndex !== -1) {
currentGroup.bookmarks.splice(currentBookmarkIndex + 1, 0, newBookmark);
} else {
currentGroup.bookmarks.push(newBookmark);
}
}
}
modalOverlay.style.display = 'none';
saveData();
render();
});
const title = document.querySelector('h1');
bindLongPressTitle(title);
function bindLongPressTitle(el) {
let startX = 0, startY = 0;
el.addEventListener('touchstart', e => {
moved = false; startX = e.touches[0].clientX; startY = e.touches[0].clientY;
longPressTimer = setTimeout(() => {
isLongPress = true; showImportExportMenu(startX, startY);
}, LONG_PRESS_DURATION);
}, { passive: false });
el.addEventListener('touchmove', e => {
const dx = Math.abs(e.touches[0].clientX - startX);
const dy = Math.abs(e.touches[0].clientY - startY);
if (dx > 10 || dy > 10) { moved = true; clearTimeout(longPressTimer); }
}, { passive: true });
el.addEventListener('touchend', e => {
clearTimeout(longPressTimer);
if (isLongPress) { e.preventDefault(); e.stopPropagation(); }
isLongPress = false; moved = false;
});
el.addEventListener('mousedown', e => {
moved = false; startX = e.clientX; startY = e.clientY;
longPressTimer = setTimeout(() => {
isLongPress = true; showImportExportMenu(startX, startY);
}, LONG_PRESS_DURATION);
});
el.addEventListener('mousemove', e => {
const dx = Math.abs(e.clientX - startX);
const dy = Math.abs(e.clientY - startY);
if (dx > 5 || dy > 5) { clearTimeout(longPressTimer); moved = true; }
});
el.addEventListener('mouseup', e => {
clearTimeout(longPressTimer);
if (isLongPress) { e.preventDefault(); e.stopPropagation(); }
isLongPress = false; moved = false;
});
el.addEventListener('contextmenu', e => {
e.preventDefault();
});
}
loadData();
</script>
</body>
</html>