-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathclipboard-backup.html
More file actions
867 lines (786 loc) · 30.3 KB
/
clipboard-backup.html
File metadata and controls
867 lines (786 loc) · 30.3 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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clipboard Backup</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/3.0.6/purify.min.js"></script>
<style>
* {
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
max-width: 900px;
margin: 0 auto;
padding: 20px;
background: #f8f9fa;
min-height: 100vh;
}
h1 {
margin: 0 0 8px 0;
font-size: clamp(24px, 4vw, 32px);
color: #333;
}
.subtitle {
color: #666;
margin-bottom: 20px;
font-size: 14px;
}
.paste-zone {
border-radius: 12px;
padding: 16px;
text-align: center;
background: white;
transition: all 0.2s ease;
margin-bottom: 24px;
border: 1px solid #e0e0e0;
}
.paste-zone.active {
border-color: #0066ff;
background: #e8f4ff;
}
.paste-zone textarea {
width: 100%;
min-height: 80px;
padding: 16px;
border: 3px dashed #ccc;
border-radius: 8px;
font-size: 16px;
font-family: inherit;
resize: vertical;
background: #fafafa;
transition: all 0.2s ease;
}
.paste-zone textarea:focus {
outline: none;
border-color: #0066ff;
background: white;
}
.paste-zone textarea::placeholder {
color: #999;
}
.paste-zone .hint {
font-size: 13px;
color: #999;
margin: 8px 0 0 0;
}
.backups-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
}
.backups-header h2 {
margin: 0;
font-size: 18px;
color: #333;
}
.clear-all-btn {
background: #dc3545;
color: white;
border: none;
padding: 8px 16px;
border-radius: 6px;
cursor: pointer;
font-size: 13px;
}
.clear-all-btn:hover {
background: #c82333;
}
.clear-all-btn:disabled {
background: #ccc;
cursor: not-allowed;
}
.backups-list {
display: flex;
flex-direction: column;
gap: 12px;
}
.backup-item {
background: white;
border: 1px solid #e0e0e0;
border-radius: 8px;
overflow: hidden;
}
.backup-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 16px;
background: #fafafa;
border-bottom: 1px solid #e0e0e0;
gap: 12px;
flex-wrap: wrap;
}
.backup-meta {
display: flex;
flex-direction: column;
gap: 4px;
min-width: 0;
flex: 1;
}
.backup-time {
font-size: 14px;
color: #333;
font-weight: 500;
}
.backup-formats {
font-size: 12px;
color: #666;
display: flex;
flex-wrap: wrap;
gap: 4px;
}
.format-badge {
background: #e9ecef;
padding: 2px 6px;
border-radius: 4px;
font-family: monospace;
font-size: 11px;
}
.backup-actions {
display: flex;
gap: 8px;
flex-shrink: 0;
}
.copy-btn {
background: #0066ff;
color: white;
border: none;
padding: 8px 16px;
border-radius: 6px;
cursor: pointer;
font-size: 13px;
display: flex;
align-items: center;
gap: 6px;
}
.copy-btn:hover {
background: #0052cc;
}
.copy-btn.copied {
background: #28a745;
}
.delete-btn {
background: white;
color: #dc3545;
border: 1px solid #dc3545;
padding: 8px 12px;
border-radius: 6px;
cursor: pointer;
font-size: 13px;
}
.delete-btn:hover {
background: #dc3545;
color: white;
}
.backup-preview {
max-height: 200px;
overflow-y: auto;
padding: 12px 16px;
}
.preview-text {
white-space: pre-wrap;
word-break: break-word;
font-family: monospace;
font-size: 13px;
color: #333;
margin: 0;
}
.preview-html {
font-size: 14px;
line-height: 1.5;
}
.preview-image {
max-width: 100%;
max-height: 180px;
display: block;
}
.png-note {
margin: 8px 0 0 0;
font-size: 12px;
color: #666;
font-style: italic;
}
.unsupported-note {
margin: 0;
padding: 8px 16px;
font-size: 12px;
color: #856404;
background: #fff3cd;
border-bottom: 1px solid #e0e0e0;
}
.download-buttons {
display: flex;
flex-wrap: wrap;
gap: 6px;
padding: 8px 16px;
background: #fafafa;
border-bottom: 1px solid #e0e0e0;
}
.download-btn {
background: #6c757d;
color: white;
border: none;
padding: 4px 10px;
border-radius: 4px;
cursor: pointer;
font-size: 11px;
display: inline-flex;
align-items: center;
gap: 4px;
}
.download-btn:hover {
background: #5a6268;
}
.preview-tabs {
display: flex;
gap: 4px;
padding: 8px 16px 0;
border-bottom: 1px solid #e0e0e0;
background: #fafafa;
}
.preview-tab {
padding: 8px 12px;
border: none;
background: none;
cursor: pointer;
font-size: 12px;
color: #666;
border-bottom: 2px solid transparent;
margin-bottom: -1px;
}
.preview-tab:hover {
color: #333;
}
.preview-tab.active {
color: #0066ff;
border-bottom-color: #0066ff;
background: white;
}
.empty-state {
text-align: center;
padding: 40px 20px;
color: #999;
}
.empty-state p {
margin: 0;
}
@media (max-width: 600px) {
body {
padding: 12px;
}
.paste-zone {
padding: 30px 16px;
}
.backup-header {
padding: 10px 12px;
}
.backup-preview {
padding: 10px 12px;
}
.backup-actions {
width: 100%;
justify-content: flex-end;
}
}
</style>
</head>
<body>
<h1>Clipboard Backup</h1>
<p class="subtitle">Preserve your clipboard content across all formats and restore it later</p>
<div class="paste-zone" id="pasteZone">
<textarea id="pasteArea" placeholder="Tap here and paste"></textarea>
<p class="hint">Preserves text, HTML, RTF, images, and other clipboard formats. Images are copied back as PNG.</p>
</div>
<div class="backups-header">
<h2>Saved Backups</h2>
<button class="clear-all-btn" id="clearAllBtn" disabled>Clear All</button>
</div>
<div class="backups-list" id="backupsList">
<div class="empty-state">
<p>No backups yet. Paste something to get started.</p>
</div>
</div>
<script>
const DB_NAME = 'ClipboardBackupDB';
const DB_VERSION = 1;
const STORE_NAME = 'backups';
let db = null;
async function initDB() {
return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onerror = () => reject(request.error);
request.onsuccess = () => {
db = request.result;
resolve(db);
};
request.onupgradeneeded = (event) => {
const database = event.target.result;
if (!database.objectStoreNames.contains(STORE_NAME)) {
const store = database.createObjectStore(STORE_NAME, {
keyPath: 'id',
autoIncrement: true
});
store.createIndex('timestamp', 'timestamp', { unique: false });
}
};
});
}
async function saveBackup(backup) {
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_NAME], 'readwrite');
const store = transaction.objectStore(STORE_NAME);
const request = store.add(backup);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
async function getAllBackups() {
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_NAME], 'readonly');
const store = transaction.objectStore(STORE_NAME);
const request = store.getAll();
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
async function deleteBackup(id) {
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_NAME], 'readwrite');
const store = transaction.objectStore(STORE_NAME);
const request = store.delete(id);
request.onsuccess = () => resolve();
request.onerror = () => reject(request.error);
});
}
async function clearAllBackups() {
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_NAME], 'readwrite');
const store = transaction.objectStore(STORE_NAME);
const request = store.clear();
request.onsuccess = () => resolve();
request.onerror = () => reject(request.error);
});
}
function formatTimestamp(timestamp) {
const date = new Date(timestamp);
const now = new Date();
const diffMs = now - date;
const diffMins = Math.floor(diffMs / 60000);
const diffHours = Math.floor(diffMs / 3600000);
const diffDays = Math.floor(diffMs / 86400000);
let relative;
if (diffMins < 1) {
relative = 'Just now';
} else if (diffMins < 60) {
relative = `${diffMins} minute${diffMins === 1 ? '' : 's'} ago`;
} else if (diffHours < 24) {
relative = `${diffHours} hour${diffHours === 1 ? '' : 's'} ago`;
} else if (diffDays < 7) {
relative = `${diffDays} day${diffDays === 1 ? '' : 's'} ago`;
} else {
relative = date.toLocaleDateString();
}
const timeStr = date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const dateStr = date.toLocaleDateString();
return `${relative} (${dateStr} ${timeStr})`;
}
function getDisplayName(mimeType) {
const names = {
'text/plain': 'Plain Text',
'text/html': 'HTML',
'text/rtf': 'RTF',
'text/uri-list': 'URLs',
'image/png': 'Image (PNG)',
'image/jpeg': 'Image (JPEG)',
'image/gif': 'Image (GIF)',
'image/webp': 'Image (WebP)',
'application/rtf': 'RTF',
};
return names[mimeType] || mimeType;
}
function getFileExtension(mimeType) {
const extensions = {
'text/plain': 'txt',
'text/html': 'html',
'text/rtf': 'rtf',
'text/uri-list': 'txt',
'image/png': 'png',
'image/jpeg': 'jpg',
'image/gif': 'gif',
'image/webp': 'webp',
'application/rtf': 'rtf',
};
return extensions[mimeType] || 'bin';
}
function downloadFormat(data, mimeType, timestamp) {
const ext = getFileExtension(mimeType);
const date = new Date(timestamp);
const dateStr = date.toISOString().slice(0, 10);
const filename = `clipboard-${dateStr}.${ext}`;
let blob;
if (mimeType.startsWith('image/')) {
// Data URL to blob
const byteString = atob(data.split(',')[1]);
const arrayBuffer = new ArrayBuffer(byteString.length);
const uint8Array = new Uint8Array(arrayBuffer);
for (let i = 0; i < byteString.length; i++) {
uint8Array[i] = byteString.charCodeAt(i);
}
blob = new Blob([uint8Array], { type: mimeType });
} else {
blob = new Blob([data], { type: mimeType });
}
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Clipboard API only supports these types (text/uri-list works via text/plain fallback)
const SUPPORTED_CLIPBOARD_TYPES = ['text/plain', 'text/html', 'text/uri-list', 'image/png', 'image/jpeg', 'image/gif', 'image/webp'];
function hasUnsupportedFormats(formats) {
return Object.keys(formats).some(type =>
!SUPPORTED_CLIPBOARD_TYPES.includes(type) && !type.startsWith('image/')
);
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function createPreviewContent(backup, activeFormat) {
const data = backup.formats[activeFormat];
const container = document.createElement('div');
container.className = 'backup-preview';
if (activeFormat.startsWith('image/')) {
const img = document.createElement('img');
img.className = 'preview-image';
img.src = data;
container.appendChild(img);
if (activeFormat !== 'image/png') {
const note = document.createElement('p');
note.className = 'png-note';
note.textContent = 'Will be copied as PNG';
container.appendChild(note);
}
} else if (activeFormat === 'text/html') {
const div = document.createElement('div');
div.className = 'preview-html';
div.innerHTML = DOMPurify.sanitize(data);
container.appendChild(div);
} else {
const pre = document.createElement('pre');
pre.className = 'preview-text';
pre.textContent = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
container.appendChild(pre);
}
return container;
}
function renderBackupItem(backup) {
const item = document.createElement('div');
item.className = 'backup-item';
item.dataset.id = backup.id;
const formats = Object.keys(backup.formats);
const defaultFormat = formats.includes('text/plain') ? 'text/plain' :
formats.includes('text/html') ? 'text/html' :
formats[0];
// Header
const header = document.createElement('div');
header.className = 'backup-header';
const meta = document.createElement('div');
meta.className = 'backup-meta';
const time = document.createElement('div');
time.className = 'backup-time';
time.textContent = formatTimestamp(backup.timestamp);
meta.appendChild(time);
const formatsDiv = document.createElement('div');
formatsDiv.className = 'backup-formats';
formats.forEach(format => {
const badge = document.createElement('span');
badge.className = 'format-badge';
badge.textContent = getDisplayName(format);
formatsDiv.appendChild(badge);
});
meta.appendChild(formatsDiv);
header.appendChild(meta);
const actions = document.createElement('div');
actions.className = 'backup-actions';
const copyBtn = document.createElement('button');
copyBtn.className = 'copy-btn';
const hasImage = formats.some(f => f.startsWith('image/'));
copyBtn.innerHTML = '<span>📋</span> Copy';
if (hasImage) {
copyBtn.title = 'Images will be copied as PNG';
}
copyBtn.onclick = () => copyBackup(backup, copyBtn);
actions.appendChild(copyBtn);
const deleteBtn = document.createElement('button');
deleteBtn.className = 'delete-btn';
deleteBtn.textContent = 'Delete';
deleteBtn.onclick = async () => {
await deleteBackup(backup.id);
item.remove();
updateClearAllButton();
checkEmptyState();
};
actions.appendChild(deleteBtn);
header.appendChild(actions);
item.appendChild(header);
// Note about unsupported formats
if (hasUnsupportedFormats(backup.formats)) {
const note = document.createElement('p');
note.className = 'unsupported-note';
const unsupported = Object.keys(backup.formats)
.filter(type => !SUPPORTED_CLIPBOARD_TYPES.includes(type) && !type.startsWith('image/'))
.map(getDisplayName);
note.textContent = `${unsupported.join(', ')} cannot be copied to clipboard. Use download instead.`;
item.appendChild(note);
}
// Download buttons
const downloadSection = document.createElement('div');
downloadSection.className = 'download-buttons';
const downloadLabel = document.createElement('span');
downloadLabel.textContent = 'Download: ';
downloadLabel.style.fontSize = '11px';
downloadLabel.style.color = '#666';
downloadSection.appendChild(downloadLabel);
formats.forEach(format => {
const btn = document.createElement('button');
btn.className = 'download-btn';
btn.innerHTML = `⬇ ${getDisplayName(format)}`;
btn.onclick = () => downloadFormat(backup.formats[format], format, backup.timestamp);
downloadSection.appendChild(btn);
});
item.appendChild(downloadSection);
// Preview tabs (if multiple formats)
if (formats.length > 1) {
const tabs = document.createElement('div');
tabs.className = 'preview-tabs';
formats.forEach(format => {
const tab = document.createElement('button');
tab.className = 'preview-tab' + (format === defaultFormat ? ' active' : '');
tab.textContent = getDisplayName(format);
tab.onclick = () => {
tabs.querySelectorAll('.preview-tab').forEach(t => t.classList.remove('active'));
tab.classList.add('active');
const oldPreview = item.querySelector('.backup-preview');
const newPreview = createPreviewContent(backup, format);
oldPreview.replaceWith(newPreview);
};
tabs.appendChild(tab);
});
item.appendChild(tabs);
}
// Preview content
item.appendChild(createPreviewContent(backup, defaultFormat));
return item;
}
// Convert any image data URL to PNG blob (required for clipboard API)
async function convertImageToPngBlob(dataUrl) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
canvas.toBlob((blob) => {
if (blob) {
resolve(blob);
} else {
reject(new Error('Failed to convert image to PNG'));
}
}, 'image/png');
};
img.onerror = () => reject(new Error('Failed to load image'));
img.src = dataUrl;
});
}
async function copyBackup(backup, button) {
// Clipboard API only supports: text/plain, text/html, image/png
const SUPPORTED_TYPES = ['text/plain', 'text/html', 'image/png'];
async function doCopy(blobParts) {
const clipboardItem = new ClipboardItem(blobParts);
await navigator.clipboard.write([clipboardItem]);
button.innerHTML = '<span>✓</span> Copied!';
button.classList.add('copied');
setTimeout(() => {
button.innerHTML = '<span>📋</span> Copy';
button.classList.remove('copied');
}, 2000);
}
try {
const blobParts = {};
// Handle image formats - convert to PNG
const imageFormat = Object.keys(backup.formats).find(k => k.startsWith('image/'));
if (imageFormat) {
const pngBlob = await convertImageToPngBlob(backup.formats[imageFormat]);
blobParts['image/png'] = pngBlob;
}
// Handle text formats - only include supported ones
if (backup.formats['text/plain']) {
blobParts['text/plain'] = new Blob([backup.formats['text/plain']], { type: 'text/plain' });
}
if (backup.formats['text/html']) {
blobParts['text/html'] = new Blob([backup.formats['text/html']], { type: 'text/html' });
}
await doCopy(blobParts);
} catch (err) {
console.error('Failed to copy with all formats:', err);
// Fallback: try image only
const imageFormat = Object.keys(backup.formats).find(k => k.startsWith('image/'));
if (imageFormat) {
try {
const pngBlob = await convertImageToPngBlob(backup.formats[imageFormat]);
await doCopy({ 'image/png': pngBlob });
return;
} catch (e) {
console.error('Failed to copy image:', e);
}
}
// Fallback: try plain text only
if (backup.formats['text/plain']) {
try {
await navigator.clipboard.writeText(backup.formats['text/plain']);
button.innerHTML = '<span>✓</span> Copied (text only)';
button.classList.add('copied');
setTimeout(() => {
button.innerHTML = '<span>📋</span> Copy';
button.classList.remove('copied');
}, 2000);
return;
} catch (e) {
console.error('Failed to copy text:', e);
}
}
alert('Failed to copy to clipboard. Please try again.');
}
}
function updateClearAllButton() {
const list = document.getElementById('backupsList');
const clearBtn = document.getElementById('clearAllBtn');
const hasItems = list.querySelectorAll('.backup-item').length > 0;
clearBtn.disabled = !hasItems;
}
function checkEmptyState() {
const list = document.getElementById('backupsList');
const items = list.querySelectorAll('.backup-item');
const emptyState = list.querySelector('.empty-state');
if (items.length === 0 && !emptyState) {
list.innerHTML = '<div class="empty-state"><p>No backups yet. Paste something to get started.</p></div>';
} else if (items.length > 0 && emptyState) {
emptyState.remove();
}
}
async function handlePaste(event) {
event.preventDefault();
const clipboardData = event.clipboardData;
if (!clipboardData) return;
const formats = {};
// Get all available types
for (const type of clipboardData.types) {
if (type.startsWith('image/')) {
// Handle images
const file = clipboardData.files[0];
if (file) {
const reader = new FileReader();
const dataUrl = await new Promise((resolve) => {
reader.onload = () => resolve(reader.result);
reader.readAsDataURL(file);
});
formats[type] = dataUrl;
}
} else {
// Handle text-based formats
const data = clipboardData.getData(type);
if (data) {
formats[type] = data;
}
}
}
// Also check for files that might be images
if (Object.keys(formats).length === 0 && clipboardData.files.length > 0) {
for (const file of clipboardData.files) {
if (file.type.startsWith('image/')) {
const reader = new FileReader();
const dataUrl = await new Promise((resolve) => {
reader.onload = () => resolve(reader.result);
reader.readAsDataURL(file);
});
formats[file.type] = dataUrl;
}
}
}
if (Object.keys(formats).length === 0) {
return;
}
const backup = {
timestamp: Date.now(),
formats: formats
};
const id = await saveBackup(backup);
backup.id = id;
const list = document.getElementById('backupsList');
const emptyState = list.querySelector('.empty-state');
if (emptyState) {
emptyState.remove();
}
const item = renderBackupItem(backup);
list.insertBefore(item, list.firstChild);
updateClearAllButton();
// Visual feedback
const pasteZone = document.getElementById('pasteZone');
pasteZone.classList.add('active');
setTimeout(() => pasteZone.classList.remove('active'), 300);
}
async function loadBackups() {
const backups = await getAllBackups();
const list = document.getElementById('backupsList');
if (backups.length === 0) {
return;
}
list.innerHTML = '';
// Sort by timestamp descending (newest first)
backups.sort((a, b) => b.timestamp - a.timestamp);
backups.forEach(backup => {
list.appendChild(renderBackupItem(backup));
});
updateClearAllButton();
}
// Initialize
document.addEventListener('DOMContentLoaded', async () => {
await initDB();
await loadBackups();
const pasteZone = document.getElementById('pasteZone');
const pasteArea = document.getElementById('pasteArea');
pasteArea.addEventListener('paste', async (e) => {
await handlePaste(e);
// Clear the textarea after successful paste
setTimeout(() => { pasteArea.value = ''; }, 0);
});
// Also allow paste anywhere on the page
document.addEventListener('paste', (e) => {
if (e.target !== pasteArea) {
handlePaste(e);
}
});
// Clear all button
document.getElementById('clearAllBtn').addEventListener('click', async () => {
if (confirm('Are you sure you want to delete all backups?')) {
await clearAllBackups();
document.getElementById('backupsList').innerHTML =
'<div class="empty-state"><p>No backups yet. Paste something to get started.</p></div>';
updateClearAllButton();
}
});
});
</script>
</body>
</html>