-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlemmy-keyboard-navigation.user.js
More file actions
1875 lines (1709 loc) · 60.2 KB
/
lemmy-keyboard-navigation.user.js
File metadata and controls
1875 lines (1709 loc) · 60.2 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
// ==UserScript==
// @name lemmy-keyboard-navigation
// @match https://*/*
// @include https://*/*
// @grant none
// @version 2.6
// @author vmavromatis
// @author InfinibyteF4
// @author aglidden
// @author howdy-tsc
// @license GPL3
// @icon https://raw.githubusercontent.com/vmavromatis/Lemmy-keyboard-navigation/main/icon.png?inline=true
// @homepageURL https://github.com/vmavromatis/Lemmy-keyboard-navigation
// @namespace https://github.com/vmavromatis/Lemmy-keyboard-navigation
// @description Easily navigate Lemmy with your keyboard
// @run-at document-end
// ==/UserScript==
/*global window,console,localStorage,sessionStorage,document,GM_addStyle,PRO_addStyle,addStyle,MutationObserver,location*/
//isLemmySite
if (document.querySelectorAll('.lemmy-site').length >= 1) {
// DEBUGGING (ignore me!)
//localStorage.clear();
//sessionStorage.clear();
//settings page (this is from lemmyTools)
const optionsKey = "lemmy-keyboard-navigation-Options";
function getSettingsFromLocalStorage() {
try {
return JSON.parse(localStorage.getItem(optionsKey) || "{}");
} catch (_) {
return {};
}
}
function checkedIfTrue(val) {
return val ? "checked" : "";
}
function options(open) {
const odiv = document.getElementById("lkOptions");
let userOptions = {};
if (open === "open") {
odiv.style.display = "block";
} else if (open === "set") {
//First run set defaults or pull from localstorage.
userOptions = Object.assign({}, {
pageOffset: 5,
optionsLink: true,
enableArrowKeyScrolling: true,
autoNext: false,
autoNextCollapse: false,
autoPage: false,
openNewTab: false,
smoothScroll: false,
scrollPosition: "middle",
expandOption: "both",
backgroundRGBA: "rgba(141, 141, 141, 0.1)",
kb_prevPage: "KeyH",
kb_nextPage: "KeyL",
kb_prevKey: "KeyK",
kb_nextKey: "KeyJ",
kb_expand: "KeyX",
kb_comments: "KeyC",
kb_openLink: "Enter",
kb_parent: "KeyP",
kb_upvote: "KeyA",
kb_downvote: "KeyZ",
kb_replyComm: "KeyR",
kb_save: "KeyS",
kb_context: "KeyQ",
kb_smallerImg: "Minus",
kb_largerImg: "Equal",
kb_user: "KeyU",
kb_edit: "KeyE",
kb_top: "KeyT",
m_dialog: "KeyG",
m_first: "Digit1",
m_second: "Digit2",
m_third: "Digit3",
m_fourth: "Digit4",
m_fifth: "Digit5",
m_frontpage: "KeyF",
m_saved: "KeyS",
m_userpage: "KeyU",
m_inbox: "KeyI",
m_options: "KeyO",
s_search: "Period"
},
getSettingsFromLocalStorage()
);
localStorage.setItem(optionsKey, JSON.stringify(userOptions));
} else if (open === "save") {
//save button
odiv.style.display = "none";
//general
userOptions.optionsLink =
document.getElementById("option_optionsLink").checked;
userOptions.enableArrowKeyScrolling =
document.getElementById("option_enableArrowKeyScrolling").checked;
userOptions.autoNext =
document.getElementById("option_autoNext").checked;
userOptions.autoNextCollapse =
document.getElementById("option_autoNextCollapse").checked;
userOptions.autoPage =
document.getElementById("option_autoPage").checked;
userOptions.openNewTab =
document.getElementById("option_openNewTab").checked;
userOptions.smoothScroll =
document.getElementById("option_smoothScroll").checked;
let offset = parseFloat(
document.getElementById("option_pageOffset").value
);
if (isNaN(offset) || offset < 0 || offset > 100) {
userOptions.pageOffset = 0;
} else {
userOptions.pageOffset = offset;
}
userOptions.scrollPosition =
document.getElementById("option_scrollPosition").value;
userOptions.expandOption =
document.getElementById("option_expandOption").value;
userOptions.backgroundRGBA =
document.getElementById("option_backgroundRGBA").value;
//keybinds
userOptions.kb_prevPage =
document.getElementById("option_kb_prevPage").value;
userOptions.kb_nextPage =
document.getElementById("option_kb_nextPage").value;
userOptions.kb_prevKey =
document.getElementById("option_kb_prevKey").value;
userOptions.kb_nextKey =
document.getElementById("option_kb_nextKey").value;
userOptions.kb_expand =
document.getElementById("option_kb_expand").value;
userOptions.kb_comments =
document.getElementById("option_kb_comments").value;
userOptions.kb_openLink =
document.getElementById("option_kb_openLink").value;
userOptions.kb_parent =
document.getElementById("option_kb_parent").value;
userOptions.kb_upvote =
document.getElementById("option_kb_upvote").value;
userOptions.kb_downvote =
document.getElementById("option_kb_downvote").value;
userOptions.kb_replyComm =
document.getElementById("option_kb_replyComm").value;
userOptions.kb_save =
document.getElementById("option_kb_save").value;
userOptions.kb_context =
document.getElementById("option_kb_context").value;
userOptions.kb_smallerImg =
document.getElementById("option_kb_smallerImg").value;
userOptions.kb_largerImg =
document.getElementById("option_kb_largerImg").value;
userOptions.kb_user =
document.getElementById("option_kb_user").value;
userOptions.kb_edit =
document.getElementById("option_kb_edit").value;
userOptions.kb_top =
document.getElementById("option_kb_top").value;
//dialog keybinds
userOptions.m_dialog =
document.getElementById("option_m_dialog").value;
userOptions.m_first =
document.getElementById("option_m_first").value;
userOptions.m_second =
document.getElementById("option_m_second").value;
userOptions.m_third =
document.getElementById("option_m_third").value;
userOptions.m_fourth =
document.getElementById("option_m_fourth").value;
userOptions.m_fifth =
document.getElementById("option_m_fifth").value;
userOptions.m_frontpage =
document.getElementById("option_m_frontpage").value;
userOptions.m_saved =
document.getElementById("option_m_saved").value;
userOptions.m_userpage =
document.getElementById("option_m_userpage").value;
userOptions.m_inbox =
document.getElementById("option_m_inbox").value;
if (document.getElementById("option_m_options").value === "") {
userOptions.m_options = "KeyO"; //if left blank, reset to default
} else {
userOptions.m_options = document.getElementById("option_m_options").value;
}
userOptions.s_search =
document.getElementById("option_s_search").value;
localStorage.setItem(optionsKey, JSON.stringify(userOptions));
window.location.reload();
}
userOptions = getSettingsFromLocalStorage();
return userOptions;
}
let settings = options("set");
let optionsLink = checkedIfTrue(settings.optionsLink);
let enableArrowKeyScrolling = checkedIfTrue(settings.enableArrowKeyScrolling);
let smoothScroll = checkedIfTrue(settings.smoothScroll);
let autoNext = checkedIfTrue(settings.autoNext);
let autoNextCollapse = checkedIfTrue(settings.autoNextCollapse);
let autoPage = checkedIfTrue(settings.autoPage);
let openNewTab = checkedIfTrue(settings.openNewTab);
let pageOffset = window.innerHeight * settings.pageOffset / 100;
let scrollPosition = settings.scrollPosition;
let backgroundRGBA = settings.backgroundRGBA;
let expandOption = settings.expandOption;
let majorversion;
try {
majorversion = parseInt(document.getElementsByClassName("app-footer container-lg")[0].outerText.match(/(?<=\.)\d+(?=\.)/g)[0]);
} catch {
majorversion = 19;
}
// Set selected entry colors
var backgroundColor = `${backgroundRGBA}`;
const nextPageKey = `${settings.kb_nextPage}`;
const prevPageKey = `${settings.kb_prevPage}`;
const nextKey = `${settings.kb_nextKey}`;
const prevKey = `${settings.kb_prevKey}`;
const expandKey = `${settings.kb_expand}`;
const openCommentsKey = `${settings.kb_comments}`;
const openLinkAndCollapseKey = `${settings.kb_openLink}`;
const parentCommentKey = `${settings.kb_parent}`;
const upvoteKey = `${settings.kb_upvote}`;
const downvoteKey = `${settings.kb_downvote}`;
const replyCommKey = `${settings.kb_replyComm}`;
const saveKey = `${settings.kb_save}`;
const contextKey = `${settings.kb_context}`;
const smallerImgKey = `${settings.kb_smallerImg}`;
const biggerImgKey = `${settings.kb_largerImg}`;
const userKey = `${settings.kb_user}`;
const editKey = `${settings.kb_edit}`;
const topKey = `${settings.kb_top}`;
const linkOneKey = 'Digit1';
const linkTwoKey = 'Digit2';
const linkThreeKey = 'Digit3';
const linkFourKey = 'Digit4';
const linkFiveKey = 'Digit5';
const linkSixKey = 'Digit6';
const linkSevenKey = 'Digit7';
const linkEightKey = 'Digit8';
const linkNineKey = 'Digit9';
const linkZeroKey = 'Digit0';
const modalPopupKey = `${settings.m_dialog}`;
const modalSortOneKey = `${settings.m_first}`;
const modalSortTwoKey = `${settings.m_second}`;
const modalSortThreeKey = `${settings.m_third}`;
const modalSortFourKey = `${settings.m_fourth}`;
const modalSortFiveKey = `${settings.m_fifth}`;
const modalFrontpageKey = `${settings.m_frontpage}`;
const modalSavedKey = `${settings.m_saved}`;
const modalProfileKey = `${settings.m_userpage}`;
const modalInboxKey = `${settings.m_inbox}`;
const modalOptionsKey = `${settings.m_options}`;
const searchKey = `${settings.s_search}`
const escapeKey = 'Escape';
let modalMode = 0;
console.log(`modalMode: ${modalMode}`);
// Stop arrows from moving the page if arrow key scrolling is disabled
window.addEventListener("keydown", function(e) {
if (["ArrowUp", "ArrowDown"].indexOf(e.code) > -1 && !enableArrowKeyScrolling) {
e.preventDefault();
}
}, false);
// Remove scroll animations
document.documentElement.style = "scroll-behavior: auto";
// Set CSS for selected entry
const css = `
.selected {
background-color: ${backgroundColor} !important;
}
`;
// add an options button in the nav bar
navbarLinks();
// dialog box
let myDialog = document.createElement("dialog");
document.body.appendChild(myDialog);
let para = document.createElement("p");
para.innerHTML = `
<h3><b>Sort by</b></h3>
<p>${modalSortOneKey} = N/A</br>
${modalSortTwoKey} = N/A</br>
${modalSortThreeKey} = N/A</br>
${modalSortFourKey} = N/A</br>
${modalSortFiveKey} = N/A</p>
<h3><b>Go To Page</b></h3>
<p>${modalFrontpageKey} = Frontpage</br>
${modalSavedKey} = Saved</br>
${modalProfileKey} = User Profile Page</br>
${modalInboxKey} = Inbox</br></p>
<h6>${modalOptionsKey} = Options Page</br></br></h6>
<button class="OPTIONSBUTTON1">Open Options Page</button>
<br/><br/>
<button class="CLOSEBUTTON1">Press ESC or ${modalPopupKey} to Close</button>
`;
myDialog.appendChild(para);
//draw search bar
const searchbar = document.createElement("div");
searchbar.setAttribute("id", "lkSearch");
searchbar.classList.add("lksearch");
searchbar.innerHTML = `
<div id="searchForm">
Press Enter to search or ESC to close</br>
<input id="searchInput" type="text" placeholder="Search Lemmy">
<button id="searchSubmit">Search</button>
<button id="searchCancel">Close</button>
<div>
`;
//draw settings page
const odiv = document.createElement("div");
odiv.setAttribute("id", "lkOptions");
odiv.classList.add("lkoptions", "border-secondary", "card");
odiv.innerHTML = `
<h4>Lemmy-keyboard-navigation Options</h4>
</hr>
<div>
<table>
<thead>
<td><b>Save and close settings</b></td>
<td><button id='LKsaveoptionsTop'>Save and Close</button></td>
</tr>
<tr>
<th><h3><b>General</b></h3></th><td><td/>
</thead>
</tr>
<tbody>
<tr>
<td><b>Link to Options in the navbar</b><br/>Show 'Keyboard Navigation Options' in<br/>the navbar at the top of the page.</br></br></td>
<td><input type='checkbox' id='option_optionsLink' ${optionsLink} /></td>
</tr>
<tr>
<td><b>Enable Arrow Key Scrolling</b><br/>Uncheck this option to disable the ability<br/>to use the arrow keys to scroll the page.</br></br></td>
<td><input type='checkbox' id='option_enableArrowKeyScrolling' ${enableArrowKeyScrolling} /></td>
</tr>
<tr>
<td><b>Skip to next selection after voting</b><br/>After upvoting/downvoting a post,</br>select the next one.</br></br></td>
<td><input type='checkbox' id='option_autoNext' ${autoNext} /></td>
</tr>
<tr>
<td><b>Skip to next selection after collapse</b><br/>After collapsing a comment,</br>select the next one.</br></br></td>
<td><input type='checkbox' id='option_autoNextCollapse' ${autoNextCollapse} /></td>
</tr>
<tr>
<td><b>Auto Next/Prev Page</b><br/>Skip to the next/previous page when the Next/Prev Page</br>Key is pressed when the last post is selected.</br></br></td>
<td><input type='checkbox' id='option_autoPage' ${autoPage} /></td>
</tr>
<tr>
<td><b>Always open comments/links in a new tab</b><br/>Automatically open comments and links</br>in a new tab without having to hold Shift.</br></br></td>
<td><input type='checkbox' id='option_openNewTab' ${openNewTab} /></td>
</tr>
<tr>
<td><b>Smooth scrolling</b><br/>Scroll smoothly to the current selection.</br></br></td>
<td><input type='checkbox' id='option_smoothScroll' ${smoothScroll} /></td>
</tr>
<tr>
<td><b>Page Offset</b><br/>Percent of page to offset selected entry when scrolling.<br/>0-20% recommended<br/>Default: 5</br></br></td>
<td><textarea id='option_pageOffset'>${settings.pageOffset}</textarea>%</td>
</tr>
<tr>
<td><b>Scrolling position</b><br/>middle: only scroll the page if selection is near the bottom.<br/>top: always scroll to keep the selection near the top.</br></br></td>
<td><select id="option_scrollPosition">
<option value='${settings.scrollPosition}'>${settings.scrollPosition}</option>
<option value='middle'>middle</option>
<option value='top'>top</option>
</select></td>
</tr>
<tr>
<td><b>Expand All Posts</b><br/>Pressing Shift + X will expand all posts.<br/>both: expand both text boxes and images.<br/>images: only expand images.<br/>text: only expand text boxes</br></br></td>
<td><select id="option_expandOption">
<option value='${settings.expandOption}'>${settings.expandOption}</option>
<option value='both'>both</option>
<option value='text'>text</option>
<option value='images'>images</option>
</select></td>
</tr>
<tr>
<td><b>Selected Background Color</b><br/>The background color of selected posts/comments.<br/>The first three values are red, green, and blue.<br/>The last value is alpha, which is transparency.<br/>Default: rgba(141, 141, 141, 0.1)</br></br></td>
<td><textarea id='option_backgroundRGBA'>${settings.backgroundRGBA}</textarea></td>
</tr>
<tr>
<td><h3><b>Rebind Keys</b></h3>Set keybinds with keycodes here:<br/><a href='https://www.toptal.com/developers/keycode'>https://www.toptal.com/developers/keycode</a></td><td><td/>
</tr>
<tr>
<tr>
<td><b>Next Page Key</b><br/>Go to the next page.<br/>Default: KeyL</br></br></td>
<td><textarea id='option_kb_nextPage'>${settings.kb_nextPage}</textarea></td>
</tr>
<tr>
<td><b>Previous Page Key</b><br/>Go to the previous page.<br/>Default: KeyH</br></br></td>
<td><textarea id='option_kb_prevPage'>${settings.kb_prevPage}</textarea></td>
</tr>
<tr>
<td><b>Next Selection Key</b><br/>Go to the next post/comment.<br/>Default: KeyJ</br></br></td>
<td><textarea id='option_kb_nextKey'>${settings.kb_nextKey}</textarea></td>
</tr>
<tr>
<td><b>Previous Selection Key</b><br/>Go to the previous post/comment.<br/>Default: KeyK</br></br></td>
<td><textarea id='option_kb_prevKey'>${settings.kb_prevKey}</textarea></td>
</tr>
<tr>
<td><b>Expand/Collapse</b><br/>Expand/collapse both post and comment content.<br/>Default: KeyX</br></br></td>
<td><textarea id='option_kb_expand'>${settings.kb_expand}</textarea></td>
</tr>
<tr>
<td><b>Open Comments</b><br/>Go to the comments of a post.<br/>Default: KeyC</br></br></td>
<td><textarea id='option_kb_comments'>${settings.kb_comments}</textarea></td>
</tr>
<tr>
<td><b>Open Links</b><br/>Open Links on a post.<br/>(can also be used to collapse comments!)<br/>Default: Enter</br></br></td>
<td><textarea id='option_kb_openLink'>${settings.kb_openLink}</textarea></td>
</tr>
<tr>
<td><b>Go to Parent Comment</b><br/>Goes one level up the comment chain.<br/>Default: KeyP</br></br></td>
<td><textarea id='option_kb_parent'>${settings.kb_parent}</textarea></td>
</tr>
<tr>
<td><b>Upvote</b><br/>:\)<br/>Default: KeyA</br></br></td>
<td><textarea id='option_kb_upvote'>${settings.kb_upvote}</textarea></td>
</tr>
<tr>
<td><b>Downvote</b><br/>:\(<br/>Default: KeyZ</br></br></td>
<td><textarea id='option_kb_downvote'>${settings.kb_downvote}</textarea></td>
</tr>
<tr>
<td><b>Reply/Go to community</b><br/>Posts: goes to the post's community<br/>Comments: replies to the selected comment<br/>Default: KeyR</br></br></td>
<td><textarea id='option_kb_replyComm'>${settings.kb_replyComm}</textarea></td>
</tr>
<tr>
<td><b>Save post/comment</b><br/>Saves the selected post/comment.<br/>Default: KeyS</br></br></td>
<td><textarea id='option_kb_save'>${settings.kb_save}</textarea></td>
</tr>
<tr>
<td><b>Get context of comment</b><br/>Goes to the context of the selected comment.<br/>Default: KeyQ</br></br></td>
<td><textarea id='option_kb_context'>${settings.kb_context}</textarea></td>
</tr>
<tr>
<td><b>Shrink expanded image</b><br/>Make an expanded image smaller.<br/>Default: Minus</br></br></td>
<td><textarea id='option_kb_smallerImg'>${settings.kb_smallerImg}</textarea></td>
</tr>
<tr>
<td><b>Grow expanded image</b><br/>Make an expanded image larger.<br/>Default: Equal</br></br></td>
<td><textarea id='option_kb_largerImg'>${settings.kb_largerImg}</textarea></td>
</tr>
<tr>
<td><b>Go to poster's profile</b><br/>Go to the profile of whoever posted the selected post/comment.<br/>Default: KeyU</br></br></td>
<td><textarea id='option_kb_user'>${settings.kb_user}</textarea></td>
</tr>
<tr>
<td><b>Edit the selected post/comment</b><br/>It only works on your own posts!<br/>Default: KeyE</br></br></td>
<td><textarea id='option_kb_edit'>${settings.kb_edit}</textarea></td>
</tr>
<tr>
<td><b>Scroll to top</b><br/>Scroll to the top of the page.<br/>Default: KeyT</br></br></td>
<td><textarea id='option_kb_top'>${settings.kb_top}</textarea></td>
</tr>
<tr>
<td><h3><b>Rebind Dialog Keys</b></h3></td><td><td/>
</tr>
<tr>
<td><b>Open/Close Dialog</b><br/>Default: KeyG</br></br></td>
<td><textarea id='option_m_dialog'>${settings.m_dialog}</textarea></td>
</tr>
<tr>
<tr>
<td><h4><b>Sort buttons</b></h4>For example: If you were to press G then 3 on the front page,<br/>it would sort by subscribed, but doing the same in a<br/>comment section would sort by new. The dialog<br/>text will change with what sort buttons are avaliable!</td><td><td/>
</tr>
<td><b>First Sort Button</b><br/>Default: Digit1</br></br></td>
<td><textarea id='option_m_first'>${settings.m_first}</textarea></td>
</tr>
<tr>
<td><b>Second Sort Button</b><br/>Default: Digit2</br></br></td>
<td><textarea id='option_m_second'>${settings.m_second}</textarea></td>
</tr>
<tr>
<td><b>Third Sort Button</b><br/>Default: Digit3</br></br></td>
<td><textarea id='option_m_third'>${settings.m_third}</textarea></td>
</tr>
<tr>
<td><b>Fourth Sort Button</b><br/>Default: Digit4</br></br></td>
<td><textarea id='option_m_fourth'>${settings.m_fourth}</textarea></td>
</tr>
<tr>
<td><b>Fifth Sort Button</b><br/>Default: Digit5</br></br></td>
<td><textarea id='option_m_fifth'>${settings.m_fifth}</textarea></td>
</tr>
<tr>
<td><h4><b>Go to page</b></h4></td><td><td/>
</tr>
<tr>
<td><b>Go to Frontpage</b><br/>Default: KeyF</br></br></td>
<td><textarea id='option_m_frontpage'>${settings.m_frontpage}</textarea></td>
</tr>
<tr>
<td><b>Go to Saved posts/comments</b><br/>Default: KeyS</br></br></td>
<td><textarea id='option_m_saved'>${settings.m_saved}</textarea></td>
</tr>
<tr>
<td><b>Go to Current User's Profile</b><br/>Default: KeyU</br></br></td>
<td><textarea id='option_m_userpage'>${settings.m_userpage}</textarea></td>
</tr>
<tr>
<td><b>Go to Inbox</b><br/>Default: KeyI</br></br></td>
<td><textarea id='option_m_inbox'>${settings.m_inbox}</textarea></td>
</tr>
<tr>
<td><b>Open options page</b><br/>Default: KeyO</br></br></td>
<td><textarea id='option_m_options'>${settings.m_options}</textarea></td>
</tr>
<tr>
<td><h3><b>Rebind Searchbar Keys</b></h3></td><td><td/>
</tr>
<tr>
<td><b>Open search bar</b><br/>Default: Period</br></br></td>
<td><textarea id='option_s_search'>${settings.s_search}</textarea></td>
</tr>
<tr>
<td><b>Save and close settings</b></td>
<td><button id='LKsaveoptions'>Save and Close</button></td>
</tr>
<tr>
<td><b style='color:red;'>WARNING:<br/>The button below will reset all your settings to default.<br/>This cannot be undone.</b></td><td></td>
</tr>
<tr>
<td><button id='LKresetoptions'>Reset All Settings</button></td><td></td>
</tr>
</tbody>
</table>
</div>
<hr/>
<p>lemmy-keyboard-navigation links:</p>
<a
href='https://github.com/vmavromatis/Lemmy-keyboard-navigation'>Github</a><br/><a
href='https://greasyfork.org/en/scripts/470498-lemmy-keyboard-navigation'>GreasyFork</a><br/><a
href='https://chrome.google.com/webstore/detail/lemmy-keyboard-navigator/lamoeoaekeeklbcekclbceaeafjkdhbi'>Chrome Extension</a><br/></p>
<p>This settings page was taken from the <a href='https://github.com/howdy-tsc/LemmyTools'>LemmyTools</a> Userscript.</p>
`;
let styleString = `
.lkoptions {
position: fixed;
min-width: auto;
min-height: auto;
width: auto;
height: 100%;
top: 0;
display: none;
left: 0;
overflow: scroll;
z-index: 1000;
padding: 0.5%;
margin-top:35px;
}
.lksearch { /* from RES */
position: fixed;
top: 30px;
left: 50%;
margin-left: -275px;
z-index: 10800000;
width: 550px;
border: 3px solid ${backgroundColor};
border-radius: 10px;
padding: 10px;
background-color: #333;
color: #ccc;
}
`;
document.head.appendChild(document.createElement("style")).innerHTML = styleString;
document.body.appendChild(odiv); //options
document.body.appendChild(searchbar); //cmdline
document.getElementById('lkSearch').style.display = 'none';
document.getElementById("LKsaveoptions").addEventListener("click", (e) => {
e.preventDefault();
options("save");
});
document.getElementById("LKsaveoptionsTop").addEventListener("click", (e) => {
e.preventDefault();
options("save");
});
document.getElementById("LKresetoptions").addEventListener("click", (e) => {
e.preventDefault();
localStorage.clear();
window.location.reload();
});
document.getElementById("searchSubmit").addEventListener("click", (e) => {
goToSearch("search");
});
document.getElementById("searchCancel").addEventListener("click", (e) => {
goToSearch("close");
});
document.getElementById("searchInput").addEventListener('keydown', function onEvent(event) {
if (event.key === "Enter") {
goToSearch("search");
}
if (event.key === "Escape") {
goToSearch("close");
}
});
// Global variables
let currentEntry;
let commentBlock;
let entries = [];
let previousUrl = "";
let expand = false;
if (typeof GM_addStyle !== "undefined") {
GM_addStyle(css);
} else if (typeof PRO_addStyle !== "undefined") {
PRO_addStyle(css);
} else if (typeof addStyle !== "undefined") {
addStyle(css);
} else {
let node = document.createElement("style");
node.type = "text/css";
node.appendChild(document.createTextNode(css));
let heads = document.getElementsByTagName("head");
if (heads.length > 0) {
heads[0].appendChild(node);
} else {
// no head yet, stick it whereever
document.documentElement.appendChild(node);
}
}
const selectedClass = "selected";
const targetNode = document.documentElement;
const config = {
childList: true,
subtree: true
};
//Fix for FF only - check for entries and load init() on very first window load
window.onload = () => {
getEntries();
if (document.getElementById("LKoptionpagelink") === null) {
navbarLinks();
}
};
//Same as above but now do it via mutationobserver for any page changes
const observer = new MutationObserver(() => {
getEntries();
});
function getEntries() {
entries = document.querySelectorAll(".post-listing, .comment-node");
if (entries.length > 0) {
if (location.href !== previousUrl) {
previousUrl = location.href;
currentEntry = null;
}
init();
}
}
observer.observe(targetNode, config);
function init() {
sessionStorage.setItem('currentselection', 0); //reset the selection to the first post
// If jumping to comments
if (window.location.search.includes("scrollToComments=true") &&
entries.length > 1 &&
(!currentEntry || Array.from(entries).indexOf(currentEntry) < 0)
) {
selectEntry(entries[1], true);
}
// If jumping to comment from anchor link
else if (window.location.pathname.includes("/comment/") &&
(!currentEntry || Array.from(entries).indexOf(currentEntry) < 0)
) {
const commentId = window.location.pathname.replace("/comment/", "");
const anchoredEntry = document.getElementById("comment-" + commentId);
if (anchoredEntry) {
selectEntry(anchoredEntry, true);
}
}
// If no entries yet selected, default to last selected
else if (!currentEntry || Array.from(entries).indexOf(currentEntry) < 0) {
if (sessionStorage.getItem('currentselection') === null) {
selectEntry(entries[0]);
} else {
sessionCurrentEntry("restore");
}
}
Array.from(entries).forEach(entry => {
entry.removeEventListener("click", clickEntry, true);
entry.addEventListener('click', clickEntry, true);
});
let expandImageButtons = document.getElementsByClassName("thumbnail rounded overflow-hidden d-inline-block position-relative p-0 border-0 bg-transparent");
let expandTextButtons = document.getElementsByClassName("btn btn-sm btn-link link-dark link-opacity-75 link-opacity-100-hover py-0 align-baseline");
Array.from(expandImageButtons).forEach(entry => {
entry.addEventListener('mousedown', (e) => {expand = !isExpanded() ? true : false})
});
Array.from(expandTextButtons).forEach(entry => {
entry.addEventListener('mousedown', (e) => {expand = !isExpanded() ? true : false})
});
document.removeEventListener("keydown", handleKeyPress, true);
document.addEventListener("keydown", handleKeyPress, true);
}
function handleKeyPress(event) {
if (["TEXTAREA", "INPUT"].indexOf(event.target.tagName) > -1 || event.metaKey) {
return;
}
switch (modalMode) {
case modalMode = 0:
switch (event.code) {
case nextKey:
case prevKey:
previousKey(event);
break;
case upvoteKey:
upVote();
previousKey(event);
break;
case downvoteKey:
downVote();
previousKey(event);
break;
case expandKey:
if (event.shiftKey) {
expandAll();
} else {
toggleExpand();
expand = isExpanded() ? true : false;
}
break;
case smallerImgKey:
imgResize("smaller");
break;
case biggerImgKey:
imgResize("larger");
break;
case saveKey:
save();
break;
case editKey:
edit();
break;
case openCommentsKey:
comments(event);
break;
case searchKey:
goToSearch("open");
break;
case modalPopupKey:
dialogUpdate();
goToDialog("open");
break;
case modalOptionsKey:
options("open");
break;
case contextKey:
getContext(event);
break;
case replyCommKey:
// allow refresh with Ctrl + R
if (!event.ctrlKey) {
if (window.location.pathname.includes("/post/")) {
reply(event);
} else {
community(event);
}
}
break;
case userKey:
visitUser(event);
break;
case openLinkAndCollapseKey:
if (window.location.pathname.includes("/post/")) {
toggleExpand();
} else {
const linkElement = currentEntry.querySelector(".col.flex-grow-1>p>a");
if (linkElement) {
if (openNewTab || event.shiftKey) {
window.open(linkElement.href);
} else {
linkElement.click();
}
} else {
comments(event);
}
}
break;
case parentCommentKey: {
let targetBlock;
if (currentEntry.classList.contains("ms-1")) {
targetBlock = getPrevEntry(currentEntry);
} else if (currentEntry.parentElement.parentElement.parentElement.nodeName === "LI") {
targetBlock = currentEntry.parentElement.parentElement.parentElement.getElementsByTagName("article")[0];
}
if (targetBlock) {
if (expand) {
collapseEntry();
}
selectEntry(targetBlock, true);
if (expand) {
expandEntry();
}
}
}
break;
case topKey:
goToTop();
break;
case linkOneKey:
clickLink(1);
break;
case linkTwoKey:
clickLink(2);
break;
case linkThreeKey:
clickLink(3);
break;
case linkFourKey:
clickLink(4);
break;
case linkFiveKey:
clickLink(5);
break;
case linkSixKey:
clickLink(6);
break;
case linkSevenKey:
clickLink(7);
break;
case linkEightKey:
clickLink(8);
break;
case linkNineKey:
clickLink(9);
break;
case linkZeroKey:
clickLink(0);
break;
case nextPageKey:
case prevPageKey:
previousPageKey(event);
}
break;
case modalMode = 1:
switch (event.code) {
case escapeKey:
modalMode = 0;
console.log(`modalMode: ${modalMode}`);
break;
case modalPopupKey:
goToDialog("close");
break;
case modalSavedKey:
if (window.location.pathname.includes("/u/")) {
let savedelement = document.getElementsByClassName("btn btn-outline-secondary pointer")[3];
if (savedelement) {
savedelement.click();
goToDialog("close");
}
} else {
instanceAndUser("saved");
}
break;
case modalFrontpageKey:
frontpage();
break;
case modalProfileKey:
let profileelement = document.getElementsByClassName("dropdown-item px-2")[0];
if (profileelement) {
profileelement.click();
goToDialog("close");
} else {
instanceAndUser("profile");
}
break;
case modalInboxKey:
let notifelement = document.getElementsByClassName("nav-link d-inline-flex align-items-center d-md-inline-block")[2];
if (notifelement) {
notifelement.click();
goToDialog("close");
} else {
window.location.replace(window.location.origin + "/login");
}
break;
case modalSortOneKey:
let firstbutton = document.getElementsByClassName("btn btn-outline-secondary")[0];
firstbutton.click();
goToDialog("close");
break;
case modalSortTwoKey:
let secondbutton = document.getElementsByClassName("btn btn-outline-secondary")[1];
secondbutton.click();
goToDialog("close");
break;
case modalSortThreeKey:
let thirdbutton = document.getElementsByClassName("btn btn-outline-secondary")[2];
thirdbutton.click();
goToDialog("close");
break;
case modalSortFourKey:
let fourthbutton = document.getElementsByClassName("btn btn-outline-secondary")[3];
fourthbutton.click();
goToDialog("close");
break;
case modalSortFiveKey:
let fifthbutton = document.getElementsByClassName("btn btn-outline-secondary")[4];
fifthbutton.click();
goToDialog("close");
break;
case modalOptionsKey:
options("open");
goToDialog("close");
break;
}
break;
case modalMode = 2:
switch (event.code) {
case escapeKey:
goToSearch("close");
break;
}
}
}
function getNextEntry(e) {
const currentEntryIndex = Array.from(entries).indexOf(e);
var nextEntry = entries[currentEntryIndex + 1];
var nextIndex = currentEntryIndex + 1;
if (currentEntryIndex + 1 >= entries.length) {
nextEntry = e;
}
for (let i = 0; i < entries.length; i++) {
if (!nextEntry.isConnected || nextEntry.clientHeight === 0) {