-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1118 lines (962 loc) · 35.8 KB
/
Copy pathindex.html
File metadata and controls
1118 lines (962 loc) · 35.8 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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Create, edit, and manage browser bookmarklets in your browser. Free, open-source tool with syntax highlighting, code formatting, and local storage.">
<title>Bookmarklets by Brian Cantoni</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.1/beautify.min.js"></script>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
display: flex;
height: 100vh;
overflow: hidden;
}
/* Left Panel */
.left-panel {
width: 250px;
min-width: 200px;
background: #f5f5f5;
border-right: 1px solid #ddd;
display: flex;
flex-direction: column;
height: 100vh;
}
.left-panel-header {
padding: 14px;
border-bottom: 1px solid #ddd;
background: #fff;
}
.left-panel-header h1 {
font-size: 18px;
margin-top: 0.5em;
margin-bottom: 4px;
color: #333;
}
.left-panel-header .byline {
font-size: 13px;
color: #333;
margin-bottom: 10px;
}
.left-panel-header .byline a {
color: #4a90d9;
text-decoration: none;
}
.left-panel-header .byline a:hover {
text-decoration: underline;
}
.privacy-note {
font-size: 13px;
color: #333;
margin-bottom: 10px;
line-height: 1.4;
}
.btn-new {
width: 100%;
padding: 8px 12px;
background: #4a90d9;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 15px;
}
.btn-new:hover {
background: #3a7bc8;
}
.bookmarklet-list {
flex: 1;
overflow-y: auto;
padding: 8px 0;
}
.bookmarklet-item {
padding: 10px 12px;
cursor: pointer;
border-bottom: 1px solid #eee;
font-size: 15px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.bookmarklet-item:hover {
background: #e8e8e8;
}
.bookmarklet-item.selected {
background: #4a90d9;
color: white;
}
.left-panel-footer {
padding: 14px;
border-top: 1px solid #ddd;
background: #fff;
display: flex;
gap: 8px;
}
.btn-io {
flex: 1;
padding: 6px 8px;
background: #6c757d;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 15px;
}
.btn-io:hover {
background: #5a6268;
}
/* Right Panel */
.right-panel {
flex: 1;
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
}
.editor-container {
flex: 1;
padding: 20px;
overflow-y: auto;
}
.getting-started {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
color: #333;
text-align: center;
}
.getting-started h2 {
margin-bottom: 16px;
color: #333;
}
.getting-started p {
max-width: 400px;
line-height: 1.6;
}
.editor {
display: none;
max-width: 900px;
}
.editor.visible {
display: block;
}
.field-group {
margin-bottom: 20px;
}
.field-group label {
display: block;
font-weight: 600;
margin-bottom: 6px;
color: #333;
font-size: 14px;
}
.field-group input[type="text"] {
width: 100%;
padding: 10px 12px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
}
.field-group input[type="text"]:focus {
outline: none;
border-color: #4a90d9;
box-shadow: 0 0 0 2px rgba(74, 144, 217, 0.2);
}
/* Code editor with syntax highlighting */
.code-editor-wrapper {
position: relative;
border: 1px solid #ccc;
border-radius: 4px;
overflow: hidden;
}
.code-editor-wrapper:focus-within {
border-color: #4a90d9;
box-shadow: 0 0 0 2px rgba(74, 144, 217, 0.2);
}
.code-editor {
width: 100%;
min-height: 200px;
padding: 12px;
border: none;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 13px;
line-height: 1.5;
resize: vertical;
background: transparent;
position: relative;
z-index: 1;
color: transparent;
caret-color: #333;
}
.code-editor:focus {
outline: none;
}
.code-editor::placeholder {
color: #999;
}
.code-highlight {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: 0;
padding: 12px;
pointer-events: none;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 13px;
line-height: 1.5;
white-space: pre-wrap;
word-wrap: break-word;
overflow: auto;
background: #fff;
z-index: 0;
}
.code-highlight code {
font-family: inherit;
font-size: inherit;
line-height: inherit;
background: transparent;
padding: 0;
}
.output-textarea {
width: 100%;
min-height: 80px;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 12px;
line-height: 1.4;
resize: vertical;
background: #fff;
color: #555;
}
.char-count {
margin-top: 6px;
font-size: 12px;
color: #333;
}
.char-count.warning {
color: #dc3545;
font-weight: 600;
}
/* Buttons */
.button-row {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 20px;
}
.btn {
padding: 8px 16px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 13px;
font-weight: 500;
}
.btn-primary {
background: #4a90d9;
color: white;
}
.btn-primary:hover {
background: #3a7bc8;
}
.btn-secondary {
background: #6c757d;
color: white;
}
.btn-secondary:hover {
background: #5a6268;
}
.btn-success {
background: #28a745;
color: white;
}
.btn-success:hover {
background: #218838;
}
.btn-danger {
background: #dc3545;
color: white;
}
.btn-danger:hover {
background: #c82333;
}
.btn-warning {
background: #ffc107;
color: #212529;
}
.btn-warning:hover {
background: #e0a800;
}
/* Help icon */
.help-icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
border-radius: 50%;
background: #6c757d;
color: white;
font-size: 14px;
font-weight: 400;
cursor: default;
user-select: none;
position: relative;
flex-shrink: 0;
}
.help-popup {
display: none;
position: absolute;
bottom: calc(100% + 8px);
right: 0;
background: white;
color: #212529;
border: 1px solid #ccc;
border-radius: 6px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
padding: 12px 16px;
min-width: 340px;
z-index: 100;
}
.help-popup.visible {
display: block;
}
.help-popup table {
border-collapse: collapse;
width: 100%;
font-size: 13px;
}
.help-popup td {
padding: 4px 8px 4px 0;
vertical-align: top;
}
.help-popup td:first-child {
font-weight: 600;
white-space: nowrap;
padding-right: 12px;
}
.help-popup tr:not(:last-child) td {
border-bottom: 1px solid #f0f0f0;
}
/* Bookmarklet link */
.bookmarklet-link-section {
padding: 16px;
background: #f0f7ff;
border: 1px solid #b8daff;
border-radius: 4px;
}
.bookmarklet-link-section label {
display: block;
font-weight: 600;
margin-bottom: 8px;
color: #333;
font-size: 14px;
}
.bookmarklet-link {
display: inline-block;
padding: 10px 20px;
background: #4a90d9;
color: white;
text-decoration: none;
border-radius: 4px;
font-size: 14px;
cursor: grab;
}
.bookmarklet-link:hover {
background: #3a7bc8;
}
.bookmarklet-link.disabled {
background: #ccc;
cursor: not-allowed;
pointer-events: none;
}
.drag-hint {
margin-top: 8px;
font-size: 12px;
color: #333;
}
/* Hidden file input */
#import-input {
display: none;
}
/* Toast notification */
.toast {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
background: #333;
color: white;
padding: 12px 24px;
border-radius: 4px;
font-size: 14px;
opacity: 0;
transition: opacity 0.3s ease;
pointer-events: none;
z-index: 1000;
}
.toast.visible {
opacity: 1;
}
/* Timestamps */
.timestamps {
margin-top: 20px;
padding-top: 16px;
border-top: 1px solid #eee;
font-size: 12px;
color: #888;
}
.timestamps span {
margin-right: 20px;
}
</style>
</head>
<body>
<div class="left-panel">
<div class="left-panel-header">
<h1>Bookmarklets</h1>
<p class="byline">by Brian Cantoni (<a href="https://github.com/bcantoni/bookmarklets" target="_blank" rel="noopener">GitHub repo</a>)</p>
<p class="privacy-note">100% client-side. Your data stays in your browser.</p>
<button class="btn-new" onclick="createNew()">+ New</button>
</div>
<div class="bookmarklet-list" id="bookmarklet-list"></div>
<div class="left-panel-footer">
<button class="btn-io" onclick="importBookmarklets()">Import</button>
<button class="btn-io" onclick="exportBookmarklets()">Export</button>
</div>
</div>
<main class="right-panel">
<div class="editor-container">
<div class="getting-started" id="getting-started">
<h2>Getting Started</h2>
<p>Welcome to Bookmarklets! Click the <strong>+ New</strong> button in the left panel to create your first bookmarklet.</p>
<p style="margin-top: 12px;">Bookmarklets are small JavaScript programs that run in your browser. You can drag them to your bookmarks bar for quick access.</p>
</div>
<div class="editor" id="editor">
<div class="field-group">
<label for="title">Title</label>
<input type="text" id="title" placeholder="My Bookmarklet" oninput="markDirty()">
</div>
<div class="field-group">
<label for="code">Code</label>
<div class="code-editor-wrapper">
<pre class="code-highlight" aria-hidden="true"><code class="language-javascript" id="code-highlight"></code></pre>
<textarea class="code-editor" id="code" placeholder="// Enter your JavaScript code here" oninput="markDirty(); updateHighlight()" onscroll="syncScroll()"></textarea>
</div>
</div>
<div class="field-group">
<label for="output">Output</label>
<textarea class="output-textarea" id="output" placeholder="Click 'Generate Bookmarklet' to see the output, or paste a bookmarklet here and click 'Reverse'" oninput="updateCharCount()"></textarea>
<div class="char-count" id="char-count">0 characters</div>
</div>
<div class="button-row">
<button class="btn btn-primary" onclick="generateBookmarklet()">Generate Bookmarklet</button>
<button class="btn btn-primary" onclick="reverseBookmarklet()">Reverse</button>
<button class="btn btn-warning" onclick="runCode()">Run Code</button>
<button class="btn btn-secondary" onclick="prettifyCode()">Pretty</button>
<button class="btn btn-secondary" onclick="clearCode()">Clear</button>
<button class="btn btn-success" onclick="saveBookmarklet()">Save</button>
<button class="btn btn-danger" onclick="deleteBookmarklet()">Delete</button>
<button class="btn btn-secondary" onclick="copyToClipboard()">Copy to Clipboard</button>
<div class="help-icon" id="help-icon">?
<div class="help-popup" id="help-popup">
<table>
<tr><td>Generate Bookmarklet</td><td>Converts your code into a <code>javascript:</code> bookmarklet link (Code → Output)</td></tr>
<tr><td>Reverse</td><td>Extracts source code from an existing bookmarklet link (Output → Code)</td></tr>
<tr><td>Run Code</td><td>Executes the current code directly in this browser tab</td></tr>
<tr><td>Pretty</td><td>Formats and beautifies the code for readability</td></tr>
<tr><td>Clear</td><td>Clears the code editor</td></tr>
<tr><td>Save</td><td>Saves the current bookmarklet to local storage</td></tr>
<tr><td>Delete</td><td>Deletes the current bookmarklet</td></tr>
<tr><td>Copy to Clipboard</td><td>Copies the generated bookmarklet link to the clipboard</td></tr>
</table>
</div>
</div>
</div>
<div class="bookmarklet-link-section">
<label>Drag to Bookmarks Bar</label>
<a class="bookmarklet-link disabled" id="bookmarklet-drag-link" href="javascript:void(0)">Bookmarklet</a>
<p class="drag-hint">Drag this link to your bookmarks bar to install the bookmarklet.</p>
</div>
<div class="timestamps" id="timestamps" style="display: none;">
<span id="created-time"></span>
<span id="modified-time"></span>
</div>
</div>
</div>
</main>
<input type="file" id="import-input" accept=".json" onchange="handleImport(event)">
<div class="toast" id="toast"></div>
<script>
// State
let bookmarklets = [];
let currentId = null;
let isDirty = false;
// Local Storage Key
const STORAGE_KEY = 'bookmarklets';
// Initialize
document.addEventListener('DOMContentLoaded', () => {
loadFromStorage();
renderList();
// Warn before leaving page with unsaved changes
window.addEventListener('beforeunload', (e) => {
if (isDirty) {
e.preventDefault();
e.returnValue = '';
}
});
});
// Storage functions
function loadFromStorage() {
const data = localStorage.getItem(STORAGE_KEY);
if (data) {
try {
bookmarklets = JSON.parse(data);
} catch (e) {
console.error('Failed to parse bookmarklets:', e);
bookmarklets = [];
}
}
}
function saveToStorage() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(bookmarklets));
}
// Toast notification
function showToast(message, duration = 2000) {
const toast = document.getElementById('toast');
toast.textContent = message;
toast.classList.add('visible');
setTimeout(() => {
toast.classList.remove('visible');
}, duration);
}
// UUID generator
function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// Render bookmarklet list
function renderList() {
const list = document.getElementById('bookmarklet-list');
list.innerHTML = '';
bookmarklets.forEach(bm => {
const item = document.createElement('div');
item.className = 'bookmarklet-item' + (bm.id === currentId ? ' selected' : '');
item.textContent = bm.title || 'Untitled';
item.onclick = () => selectBookmarklet(bm.id);
list.appendChild(item);
});
}
// Check for unsaved changes before navigating
function checkUnsavedChanges() {
if (isDirty) {
return confirm('You have unsaved changes. Do you want to discard them?');
}
return true;
}
// Create new bookmarklet
function createNew() {
if (!checkUnsavedChanges()) return;
currentId = null;
isDirty = false;
document.getElementById('title').value = '';
document.getElementById('code').value = '';
document.getElementById('output').value = '';
updateCharCount();
updateHighlight();
updateDragLink('');
document.getElementById('getting-started').style.display = 'none';
document.getElementById('editor').classList.add('visible');
document.getElementById('timestamps').style.display = 'none';
renderList();
document.getElementById('title').focus();
}
// Select existing bookmarklet
function selectBookmarklet(id) {
if (!checkUnsavedChanges()) return;
const bm = bookmarklets.find(b => b.id === id);
if (!bm) return;
currentId = id;
isDirty = false;
document.getElementById('title').value = bm.title || '';
document.getElementById('code').value = bm.code || '';
document.getElementById('output').value = bm.output || '';
updateCharCount();
updateDragLink(bm.output || '');
updateHighlight();
document.getElementById('getting-started').style.display = 'none';
document.getElementById('editor').classList.add('visible');
// Show timestamps
const timestamps = document.getElementById('timestamps');
if (bm.created || bm.modified) {
timestamps.style.display = 'block';
document.getElementById('created-time').textContent = bm.created ?
'Created: ' + new Date(bm.created).toLocaleString() : '';
document.getElementById('modified-time').textContent = bm.modified ?
'Modified: ' + new Date(bm.modified).toLocaleString() : '';
} else {
timestamps.style.display = 'none';
}
renderList();
}
// Mark as dirty (unsaved changes)
function markDirty() {
isDirty = true;
}
// Simple JavaScript minifier that preserves strings
function minifyJS(code) {
// Extract strings and replace with placeholders
const strings = [];
const stringPattern = /(["'`])(?:(?!\1|\\).|\\.)*\1/g;
let preserved = code.replace(stringPattern, (match) => {
strings.push(match);
return `__STRING_${strings.length - 1}__`;
});
// Now minify the code without strings
preserved = preserved
// Remove single-line comments
.replace(/\/\/.*$/gm, '')
// Remove multi-line comments
.replace(/\/\*[\s\S]*?\*\//g, '')
// Remove leading/trailing whitespace from lines
.split('\n')
.map(line => line.trim())
.filter(line => line.length > 0)
.join(' ')
// Collapse multiple spaces
.replace(/\s+/g, ' ')
// Remove spaces around operators and punctuation
.replace(/\s*([{}();,=+\-*/<>!&|?:])\s*/g, '$1')
// Add back necessary spaces around keywords
.replace(/\b(var|let|const|return|if|else|for|while|function|new|typeof|instanceof|in|of)\b/g, ' $1 ')
// Clean up extra spaces
.replace(/\s+/g, ' ')
.trim();
// Restore strings
preserved = preserved.replace(/__STRING_(\d+)__/g, (match, index) => {
return strings[parseInt(index)];
});
return preserved;
}
// Generate bookmarklet
function generateBookmarklet() {
const code = document.getElementById('code').value;
if (!code.trim()) {
document.getElementById('output').value = '';
updateCharCount();
updateDragLink('');
return;
}
// Minify
const minified = minifyJS(code);
// Wrap in IIFE
const wrapped = '(function(){' + minified + '})()';
// URL encode and add javascript: prefix
const encoded = 'javascript:' + encodeURIComponent(wrapped);
document.getElementById('output').value = encoded;
updateCharCount();
updateDragLink(encoded);
}
// Reverse bookmarklet back to code
function reverseBookmarklet() {
let output = document.getElementById('output').value.trim();
if (!output) {
alert('Nothing to reverse. Paste a bookmarklet in the Output field first.');
return;
}
try {
// Remove javascript: prefix
if (output.startsWith('javascript:')) {
output = output.slice(11);
}
// URL decode
let decoded = decodeURIComponent(output);
// Unwrap IIFE patterns:
// (function(){...})()
// (function(){...}())
// !function(){...}()
// void function(){...}()
const iifePatterns = [
/^\(function\(\)\{([\s\S]*)\}\)\(\);?$/,
/^\(function\(\)\{([\s\S]*)\}\(\)\);?$/,
/^!function\(\)\{([\s\S]*)\}\(\);?$/,
/^void function\(\)\{([\s\S]*)\}\(\);?$/
];
for (const pattern of iifePatterns) {
const match = decoded.match(pattern);
if (match) {
decoded = match[1];
break;
}
}
document.getElementById('code').value = decoded;
markDirty();
prettifyCode();
} catch (e) {
alert('Error reversing bookmarklet: ' + e.message);
}
}
// Update character count
function updateCharCount() {
const output = document.getElementById('output').value;
const count = output.length;
const charCount = document.getElementById('char-count');
charCount.textContent = count + ' characters';
if (count > 2000) {
charCount.classList.add('warning');
} else {
charCount.classList.remove('warning');
}
}
// Update drag link
function updateDragLink(href) {
const link = document.getElementById('bookmarklet-drag-link');
const title = document.getElementById('title').value || 'Bookmarklet';
if (href && href.startsWith('javascript:')) {
link.href = href;
link.textContent = title;
link.classList.remove('disabled');
} else {
link.href = 'javascript:void(0)';
link.textContent = 'Bookmarklet';
link.classList.add('disabled');
}
}
// Run code in current page context
function runCode() {
generateBookmarklet();
const output = document.getElementById('output').value;
if (!output) {
alert('Please generate the bookmarklet first.');
return;
}
try {
// Extract the code from javascript: prefix and decode
const encoded = output.replace(/^javascript:/, '');
const decoded = decodeURIComponent(encoded);
eval(decoded);
} catch (e) {
alert('Error running code: ' + e.message);
}
}
// Prettify code using js-beautify
function prettifyCode() {
const code = document.getElementById('code').value;
if (!code.trim()) return;
try {
const formatted = js_beautify(code, {
indent_size: 2,
space_in_empty_paren: false,
preserve_newlines: true,
max_preserve_newlines: 2,
end_with_newline: false
});
document.getElementById('code').value = formatted;
markDirty();
updateHighlight();
} catch (e) {
showToast('Error formatting code: ' + e.message);
}
}
// Clear code
function clearCode() {
if (document.getElementById('code').value && !confirm('Clear all code?')) {
return;
}
document.getElementById('code').value = '';
document.getElementById('output').value = '';
updateCharCount();
updateDragLink('');
updateHighlight();
markDirty();
}
// Save bookmarklet
function saveBookmarklet() {
const title = document.getElementById('title').value.trim() || 'Untitled';
const code = document.getElementById('code').value;
const output = document.getElementById('output').value;
const now = new Date().toISOString();
if (currentId) {
// Update existing
const index = bookmarklets.findIndex(b => b.id === currentId);
if (index !== -1) {
bookmarklets[index].title = title;
bookmarklets[index].code = code;
bookmarklets[index].output = output;
bookmarklets[index].modified = now;
}
} else {
// Create new
const newBookmarklet = {
id: generateUUID(),
title: title,
code: code,
output: output,
created: now,
modified: now
};
bookmarklets.push(newBookmarklet);
currentId = newBookmarklet.id;
}
saveToStorage();
isDirty = false;
renderList();
// Update timestamps display
const bm = bookmarklets.find(b => b.id === currentId);
if (bm) {
const timestamps = document.getElementById('timestamps');
timestamps.style.display = 'block';
document.getElementById('created-time').textContent = 'Created: ' + new Date(bm.created).toLocaleString();
document.getElementById('modified-time').textContent = 'Modified: ' + new Date(bm.modified).toLocaleString();
}
// Update drag link title
updateDragLink(output);
}
// Delete bookmarklet
function deleteBookmarklet() {
if (!currentId) {
// Just clear the form for unsaved bookmarklet
document.getElementById('getting-started').style.display = 'flex';
document.getElementById('editor').classList.remove('visible');
isDirty = false;
return;
}
if (!confirm('Are you sure you want to delete this bookmarklet?')) {
return;
}
bookmarklets = bookmarklets.filter(b => b.id !== currentId);
saveToStorage();
currentId = null;
isDirty = false;
document.getElementById('getting-started').style.display = 'flex';
document.getElementById('editor').classList.remove('visible');
renderList();
}
// Copy to clipboard
async function copyToClipboard() {
const output = document.getElementById('output').value;
if (!output) {
alert('Nothing to copy. Generate a bookmarklet first.');
return;
}
try {