/
keyboardNav.js
2133 lines (2086 loc) · 74.3 KB
/
keyboardNav.js
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
modules['keyboardNav'] = {
moduleID: 'keyboardNav',
moduleName: 'Keyboard Navigation',
category: 'UI',
options: {
// any configurable options you have go here...
// options must have a type and a value..
// valid types are: text, boolean (if boolean, value must be true or false)
// for example:
addFocusBGColor: {
type: 'boolean',
value: true,
description: 'Set a background color to highlight the currently focused element'
},
focusBGColor: {
type: 'color',
value: '#F0F3FC',
description: 'Background color of focused element',
advanced: true,
dependsOn: 'addFocusBGColor'
},
focusBGColorNight: {
type: 'color',
value: '#373737',
description: 'Background color of focused element in Night Mode',
advanced: true,
dependsOn: 'addFocusBGColor'
},
focusFGColorNight: {
type: 'color',
value: '#DDDDDD',
description: 'Foreground color of focused element in Night Mode',
advanced: true,
dependsOn: 'addFocusBGColor'
},
addFocusBorder: {
type: 'boolean',
value: true,
description: 'Set a border to highlight the currently focused element'
},
focusBorder: {
type: 'text',
value: '',
description: 'border style (e.g. 1px dashed gray) for focused element',
advanced: true,
dependsOn: 'addFocusBorder'
},
focusBorderNight: {
type: 'text',
value: '',
description: 'border style (e.g. 1px dashed gray) for focused element in Night Mode',
advanced: true,
dependsOn: 'addFocusBorder'
},
autoSelectOnScroll: {
type: 'boolean',
value: false,
description: 'Automatically select the topmost element for keyboard navigation on window scroll'
},
mediaBrowseMode: {
type: 'boolean',
value: true,
description: 'If media is open on the currently selected post when moving up/down one post, open media on the next post.'
},
scrollOnExpando: {
type: 'boolean',
value: true,
description: 'Scroll window to top of link when expando key is used (to keep pics etc in view)',
advanced: true
},
scrollStyle: {
type: 'enum',
values: [{
name: 'directional',
value: 'directional'
}, {
name: 'page up/down',
value: 'page'
}, {
name: 'lock to top',
value: 'top'
}],
value: 'directional',
description: 'When moving up/down with keynav, when and how should RES scroll the window?',
advanced: true
},
commentsLinkNumbers: {
type: 'boolean',
value: true,
description: 'Assign number keys (e.g. [1]) to links within selected comment'
},
commentsLinkNumberPosition: {
type: 'enum',
values: [{
name: 'Place on right',
value: 'right'
}, {
name: 'Place on left',
value: 'left'
}],
value: 'right',
description: 'Which side commentsLinkNumbers are displayed',
advanced: true
},
commentsLinkNewTab: {
type: 'boolean',
value: true,
description: 'Open number key links in a new tab',
advanced: true
},
clickFocus: {
type: 'boolean',
value: true,
description: 'Move keyboard focus to a link or comment when clicked with the mouse',
advanced: true
},
onHideMoveDown: {
type: 'boolean',
value: true,
description: 'After hiding a link, automatically select the next link',
advanced: true
},
onVoteMoveDown: {
type: 'boolean',
value: false,
description: 'After voting on a link, automatically select the next link',
advanced: true
},
onVoteCommentMoveDown: {
type: 'boolean',
value: false,
description: 'After voting on a comment, automatically select the next comment',
advanced: true
},
toggleHelp: {
type: 'keycode',
value: [191, false, false, true], // ? (note the true in the shift slot)
description: 'Show help for keyboard shortcuts'
},
useGoMode: {
type: 'boolean',
value: true,
description: 'Use go mode (require go mode before "go to" shortcuts are used, e.g. frontpage)'
},
goMode: {
type: 'keycode',
value: [71, false, false, false], // g
description: 'Enter "go mode" (next keypress goes to a location, e.g. frontpage)'
},
toggleCmdLine: {
type: 'keycode',
value: [190, false, false, false], // .
description: 'Show/hide commandline box'
},
hide: {
type: 'keycode',
value: [72, false, false, false], // h
description: 'Hide link'
},
moveUp: {
type: 'keycode',
value: [75, false, false, false], // k
description: 'Move up (previous link or comment)'
},
moveDown: {
type: 'keycode',
value: [74, false, false, false], // j
description: 'Move down (next link or comment)'
},
moveTop: {
type: 'keycode',
value: [75, false, false, true], // shift-k
description: 'Move to top of list (on link pages)'
},
moveBottom: {
type: 'keycode',
value: [74, false, false, true], // shift-j
description: 'Move to bottom of list (on link pages)'
},
moveUpSibling: {
type: 'keycode',
value: [75, false, false, true], // shift-k
description: 'Move to previous sibling (in comments) - skips to previous sibling at the same depth.'
},
moveDownSibling: {
type: 'keycode',
value: [74, false, false, true], // shift-j
description: 'Move to next sibling (in comments) - skips to next sibling at the same depth.'
},
moveUpThread: {
type: 'keycode',
value: [75, true, false, true], // shift-alt-k
description: 'Move to the topmost comment of the previous thread (in comments).'
},
moveDownThread: {
type: 'keycode',
value: [74, true, false, true], // shift-alt-j
description: 'Move to the topmost comment of the next thread (in comments).'
},
moveToTopComment: {
type: 'keycode',
value: [84, false, false, false], // t
description: 'Move to the topmost comment of the current thread (in comments).'
},
moveToParent: {
type: 'keycode',
value: [80, false, false, false], // p
description: 'Move to parent (in comments).'
},
showParents: {
type: 'keycode',
value: [80, false, false, true], // p
description: 'Display parent comments.'
},
followLink: {
type: 'keycode',
value: [13, false, false, false], // enter
description: 'Follow link (hold shift to open it in a new tab) (link pages only)'
},
followLinkNewTab: {
type: 'keycode',
value: [13, false, false, true], // shift-enter
description: 'Follow link in new tab (link pages only)'
},
followLinkNewTabFocus: {
type: 'boolean',
value: true,
description: 'When following a link in new tab - focus the tab?',
advanced: true
},
toggleExpando: {
type: 'keycode',
value: [88, false, false, false], // x
description: 'Toggle expando (image/text/video) (link pages only)'
},
imageSizeUp: {
type: 'keycode',
value: [187, false, false, false], // = -- 61 in firefox
description: 'Increase the size of image(s) in the highlighted post area'
},
imageSizeDown: {
type: 'keycode',
value: [189, false, false, false], // - -- 173 in firefox
description: 'Decrease the size of image(s) in the highlighted post area'
},
imageSizeUpFine: {
type: 'keycode',
value: [187, false, false, true], // shift-=
description: 'Increase the size of image(s) in the highlighted post area (finer control)'
},
imageSizeDownFine: {
type: 'keycode',
value: [189, false, false, true], // shift--
description: 'Decrease the size of image(s) in the highlighted post area (finer control)'
},
imageMoveUp: {
type: 'keycode',
value: [38, false, true, false], // ctrl-up
description: 'Move the image(s) in the highlighted post area up'
},
imageMoveDown: {
type: 'keycode',
value: [40, false, true, false], // ctrl-down
description: 'Move the image(s) in the highlighted post area down'
},
imageMoveLeft: {
type: 'keycode',
value: [37, false, true, false], // ctrl-left
description: 'Move the image(s) in the highlighted post area left'
},
imageMoveRight: {
type: 'keycode',
value: [39, false, true, false], // ctrl-right
description: 'Move the image(s) in the highlighted post area right'
},
previousGalleryImage: {
type: 'keycode',
value: [219, false, false, false], // [
description: 'View the previous image of an inline gallery.'
},
nextGalleryImage: {
type: 'keycode',
value: [221, false, false, false], // ]
description: 'View the next image of an inline gallery.'
},
toggleViewImages: {
type: 'keycode',
value: [88, false, false, true], // shift-x
description: 'Toggle "view images" button'
},
toggleChildren: {
type: 'keycode',
value: [13, false, false, false], // enter
description: 'Expand/collapse comments (comments pages only)'
},
followComments: {
type: 'keycode',
value: [67, false, false, false], // c
description: 'View comments for link (shift opens them in a new tab)'
},
followCommentsNewTab: {
type: 'keycode',
value: [67, false, false, true], // shift-c
description: 'View comments for link in a new tab'
},
followLinkAndCommentsNewTab: {
type: 'keycode',
value: [76, false, false, false], // l
description: 'View link and comments in new tabs'
},
followLinkAndCommentsNewTabBG: {
type: 'keycode',
value: [76, false, false, true], // shift-l
description: 'View link and comments in new background tabs'
},
upVote: {
type: 'keycode',
value: [65, false, false, false], // a
description: 'Upvote selected link or comment (or remove the upvote)'
},
downVote: {
type: 'keycode',
value: [90, false, false, false], // z
description: 'Downvote selected link or comment (or remove the downvote)'
},
upVoteWithoutToggling: {
type: 'keycode',
value: [65, false, false, true], // a
description: 'Upvote selected link or comment (but don\'t remove the upvote)'
},
downVoteWithoutToggling: {
type: 'keycode',
value: [90, false, false, true], // z
description: 'Downvote selected link or comment (but don\'t remove the downvote)'
},
savePost: {
type: 'keycode',
value: [83, false, false, false], // s
description: 'Save the current post to your reddit account. This is accessible from anywhere that you\'re logged in, but does not preserve the original text if it\'s edited or deleted.'
},
saveComment: {
type: 'keycode',
value: [83, false, false, true], // shift-s
description: 'Save the current comment to your reddit account. This is accessible from anywhere that you\'re logged in, but does not preserve the original text if it\'s edited or deleted.'
},
saveRES: {
type: 'keycode',
value: [83, false, false, false], // s
description: 'Save the current comment with RES. This does preserve the original text of the comment, but is only saved locally.'
},
reply: {
type: 'keycode',
value: [82, false, false, false], // r
description: 'Reply to current comment (comment pages only)'
},
followPermalink: {
type: 'keycode',
value: [89, false, false, false], // y
description: 'Open the current comment\'s permalink (comment pages only)'
},
followPermalinkNewTab: {
type: 'keycode',
value: [89, false, false, true], // shift-y
description: 'Open the current comment\'s permalink in a new tab (comment pages only)'
},
openBigEditor: {
type: 'keycode',
value: [69, false, true, false], // control-e
description: 'Open the current markdown field in the big editor. (Only when a markdown form is focused)'
},
followSubreddit: {
type: 'keycode',
value: [82, false, false, false], // r
description: 'Go to subreddit of selected link (link pages only)'
},
followSubredditNewTab: {
type: 'keycode',
value: [82, false, false, true], // shift-r
description: 'Go to subreddit of selected link in a new tab (link pages only)'
},
inbox: {
type: 'keycode',
value: [73, false, false, false], // i
description: 'Go to inbox',
isGoTo: true
},
inboxNewTab: {
type: 'keycode',
value: [73, false, false, true], // shift+i
description: 'Go to inbox in a new tab',
isGoTo: true
},
modmail: {
type: 'keycode',
value: [77, false, false, false], // m
description: 'Go to modmail',
isGoTo: true
},
modmailNewTab: {
type: 'keycode',
value: [77, false, false, true], // shift+m
description: 'Go to modmail in a new tab',
isGoTo: true
},
profile: {
type: 'keycode',
value: [85, false, false, false], // u
description: 'Go to profile',
isGoTo: true
},
profileNewTab: {
type: 'keycode',
value: [85, false, false, true], // shift+u
description: 'Go to profile in a new tab',
isGoTo: true
},
frontPage: {
type: 'keycode',
value: [70, false, false, false], // f
description: 'Go to front page',
isGoTo: true
},
subredditFrontPage: {
type: 'keycode',
value: [70, false, false, true], // shift-f
description: 'Go to subreddit front page',
isGoTo: true
},
random: {
type: 'keycode',
value: [89, true, false, false], // alt-y SO RANDOM
description: 'Go to a random subreddit',
isGoTo : true
},
nextPage: {
type: 'keycode',
value: [78, false, false, false], // n
description: 'Go to next page (link list pages only)',
isGoTo: true
},
prevPage: {
type: 'keycode',
value: [80, false, false, false], // p
description: 'Go to prev page (link list pages only)',
isGoTo: true
},
link1: {
type: 'keycode',
value: [49, false, false, false], // 1
description: 'Open first link within comment.',
noconfig: true
},
link2: {
type: 'keycode',
value: [50, false, false, false], // 2
description: 'Open link #2 within comment.',
noconfig: true
},
link3: {
type: 'keycode',
value: [51, false, false, false], // 3
description: 'Open link #3 within comment.',
noconfig: true
},
link4: {
type: 'keycode',
value: [52, false, false, false], // 4
description: 'Open link #4 within comment.',
noconfig: true
},
link5: {
type: 'keycode',
value: [53, false, false, false], // 5
description: 'Open link #5 within comment.',
noconfig: true
},
link6: {
type: 'keycode',
value: [54, false, false, false], // 6
description: 'Open link #6 within comment.',
noconfig: true
},
link7: {
type: 'keycode',
value: [55, false, false, false], // 7
description: 'Open link #7 within comment.',
noconfig: true
},
link8: {
type: 'keycode',
value: [56, false, false, false], // 8
description: 'Open link #8 within comment.',
noconfig: true
},
link9: {
type: 'keycode',
value: [57, false, false, false], // 9
description: 'Open link #9 within comment.',
noconfig: true
},
link10: {
type: 'keycode',
value: [48, false, false, false], // 0
description: 'Open link #10 within comment.',
noconfig: true
},
link1NumPad: {
type: 'keycode',
value: [97, false, false, false], // 1
description: 'Open first link within comment.',
noconfig: true
},
link2NumPad: {
type: 'keycode',
value: [98, false, false, false], // 2
description: 'Open link #2 within comment.',
noconfig: true
},
link3NumPad: {
type: 'keycode',
value: [99, false, false, false], // 3
description: 'Open link #3 within comment.',
noconfig: true
},
link4NumPad: {
type: 'keycode',
value: [100, false, false, false], // 4
description: 'Open link #4 within comment.',
noconfig: true
},
link5NumPad: {
type: 'keycode',
value: [101, false, false, false], // 5
description: 'Open link #5 within comment.',
noconfig: true
},
link6NumPad: {
type: 'keycode',
value: [102, false, false, false], // 6
description: 'Open link #6 within comment.',
noconfig: true
},
link7NumPad: {
type: 'keycode',
value: [103, false, false, false], // 7
description: 'Open link #7 within comment.',
noconfig: true
},
link8NumPad: {
type: 'keycode',
value: [104, false, false, false], // 8
description: 'Open link #8 within comment.',
noconfig: true
},
link9NumPad: {
type: 'keycode',
value: [105, false, false, false], // 9
description: 'Open link #9 within comment.',
noconfig: true
},
link10NumPad: {
type: 'keycode',
value: [96, false, false, false], // 0
description: 'Open link #10 within comment.',
noconfig: true
},
toggleCommentNavigator: {
type: 'keycode',
value: [78, false, false, false], // N
description: 'Open Comment Navigator'
},
commentNavigatorMoveUp: {
type: 'keycode',
value: [38, false, false, true], // shift+up arrow
description: 'Move up using Comment Navigator'
},
commentNavigatorMoveDown: {
type: 'keycode',
value: [40, false, false, true], // shift+down arrow
description: 'Move down using Comment Navigator'
}
},
description: 'Keyboard navigation for reddit!',
isEnabled: function() {
return RESUtils.options.getModulePrefs(this.moduleID);
},
include: [
'all'
],
isMatchURL: function() {
return RESUtils.isMatchURL(this.moduleID);
},
beforeLoad: function() {
if ((this.isEnabled()) && (this.isMatchURL())) {
if (modules['keyboardNav'].options.addFocusBGColor.value) {
modules['keyboardNav'].addFocusBGColor();
}
if (modules['keyboardNav'].options.addFocusBorder.value) {
modules['keyboardNav'].addFocusBorder();
}
RESUtils.addCSS(' \
.entry { padding-right: 5px; } \
#keyHelp { display: none; position: fixed; height: 90%; overflow-y: auto; right: 20px; top: 20px; z-index: 1000; border: 2px solid #aaa; border-radius: 5px; width: 300px; padding: 5px; background-color: #fff; } \
#keyHelp th { font-weight: bold; padding: 2px; border-bottom: 1px dashed #ddd; } \
#keyHelp td { padding: 2px; border-bottom: 1px dashed #ddd; } \
#keyHelp td:first-child { width: 70px; } \
.keyNavAnnotation { font-size: 9px; position: relative; top: -6px; } \
');
}
},
go: function() {
if ((this.isEnabled()) && (this.isMatchURL())) {
// get rid of antequated option we've removed
this.keyboardNavLastIndexCache = safeJSON.parse(RESStorage.getItem('RESmodules.keyboardNavLastIndex'), false, true);
var idx, now = Date.now();
if (!this.keyboardNavLastIndexCache) {
// this is a one time function to delete old keyboardNavLastIndex junk.
this.keyboardNavLastIndexCache = {};
for (idx in RESStorage) {
if (/keyboardNavLastIndex/.test(idx)) {
var url = idx.replace('RESmodules.keyboardNavLastIndex.', '');
this.keyboardNavLastIndexCache[url] = {
index: RESStorage[idx],
updated: now
};
RESStorage.removeItem(idx);
}
}
this.keyboardNavLastIndexCache.lastScan = now;
RESStorage.setItem('RESmodules.keyboardNavLastIndex', JSON.stringify(this.keyboardNavLastIndexCache));
} else {
// clean cache every 6 hours - delete any urls that haven't been visited in an hour.
if ((typeof this.keyboardNavLastIndexCache.lastScan === 'undefined') || (now - this.keyboardNavLastIndexCache.lastScan > 21600000)) {
for (idx in this.keyboardNavLastIndexCache) {
if ((typeof this.keyboardNavLastIndexCache[idx] === 'object') && (now - this.keyboardNavLastIndexCache[idx].updated > 3600000)) {
delete this.keyboardNavLastIndexCache[idx];
}
}
this.keyboardNavLastIndexCache.lastScan = now;
RESStorage.setItem('RESmodules.keyboardNavLastIndex', JSON.stringify(this.keyboardNavLastIndexCache));
}
}
if (this.options.autoSelectOnScroll.value) {
window.addEventListener('scroll', modules['keyboardNav'].handleScroll, false);
}
window.addEventListener('keydown', function(e) {
if (modules['keyboardNav'].handleKeyPress(e)) {
e.preventDefault();
}
}, true);
this.scanPageForKeyboardLinks();
// listen for new DOM nodes so that modules like autopager, never ending reddit, "load more comments" etc still get keyboard nav.
if (RESUtils.pageType() === 'comments') {
RESUtils.watchForElement('newComments', modules['keyboardNav'].scanPageForNewKeyboardLinks);
} else {
RESUtils.watchForElement('siteTable', modules['keyboardNav'].scanPageForNewKeyboardLinks);
}
this.registerCommandLine();
}
},
registerCommandLine: function() {
modules['commandLine'].registerCommand(/\d+/, '[number] - navigates to the link with that number (comments pages) or rank (link pages)',
function(command, val, match) {},
function(command, val, match, e) {
if (RESUtils.pageType() === 'comments') {
// comment link number? (integer)
modules['keyboardNav'].commentLink(parseInt(command, 10) - 1);
} else if (RESUtils.pageType() === 'linklist') {
modules['keyboardNav'].keyUnfocus(modules['keyboardNav'].keyboardLinks[modules['keyboardNav'].activeIndex]);
modules['keyboardNav'].activeIndex = parseInt(command, 10) - 1;
modules['keyboardNav'].keyFocus(modules['keyboardNav'].keyboardLinks[modules['keyboardNav'].activeIndex]);
modules['keyboardNav'].followLink();
}
}
);
},
// old style: .RES-keyNav-activeElement { '+borderType+': '+focusBorder+'; background-color: '+focusBGColor+'; } \
// this new pure CSS arrow will not work because to position it we must have .RES-keyNav-activeElement position relative, but that screws up image viewer's absolute positioning to
// overlay over the sidebar... yikes.
// .RES-keyNav-activeElement:after { content: ""; float: right; margin-right: -5px; border-color: transparent '+focusBorderColor+' transparent transparent; border-style: solid; border-width: 3px 4px 3px 0; } \
// why !important on .RES-keyNav-activeElement? Because some subreddits are unfortunately using !important for no good reason on .entry divs...
addFocusBGColor: function() {
var focusFGColorNight, focusBGColor, focusBGColorNight;
if (typeof this.options.focusBGColor === 'undefined') {
focusBGColor = '#F0F3FC';
} else {
focusBGColor = this.options.focusBGColor.value;
}
if (!(this.options.focusBGColorNight.value)) {
focusBGColorNight = '#666';
} else {
focusBGColorNight = this.options.focusBGColorNight.value;
}
if (!(this.options.focusFGColorNight.value)) {
focusFGColorNight = '#DDD';
} else {
focusFGColorNight = this.options.focusFGColorNight.value;
}
RESUtils.addCSS(' \
.RES-keyNav-activeElement, .commentarea .RES-keyNav-activeElement .md, .commentarea .RES-keyNav-activeElement.entry .noncollapsed { background-color: ' + focusBGColor + ' !important; } \
.res-nightmode .RES-keyNav-activeElement, .res-nightmode .RES-keyNav-activeElement .usertext-body, .res-nightmode .RES-keyNav-activeElement .usertext-body .md, .res-nightmode .RES-keyNav-activeElement .usertext-body .md p, .res-nightmode .commentarea .RES-keyNav-activeElement .noncollapsed, .res-nightmode .RES-keyNav-activeElement .noncollapsed .md, .res-nightmode .RES-keyNav-activeElement .noncollapsed .md p { background-color: ' + focusBGColorNight + ' !important; color: ' + focusFGColorNight + ' !important;} \
.res-nightmode .RES-keyNav-activeElement a.title:first-of-type {color: ' + focusFGColorNight + ' !important; } \
');
},
addFocusBorder: function() {
var focusBorder, focusBorderNight;
var borderType = BrowserStrategy.getOutlineProperty() || 'outline';
if (typeof this.options.focusBorder === 'undefined') {
focusBorder = '';
} else {
focusBorder = borderType + ': ' + this.options.focusBorder.value + ';';
}
if (typeof this.options.focusBorderNight === 'undefined') {
focusBorderNight = '';
} else {
focusBorderNight = borderType + ': ' + this.options.focusBorderNight.value + ';';
}
RESUtils.addCSS(' \
.RES-keyNav-activeElement { ' + focusBorder + ' } \
.res-nightmode .RES-keyNav-activeElement { ' + focusBorderNight + ' } \
');
},
scanPageForNewKeyboardLinks: function() {
modules['keyboardNav'].scanPageForKeyboardLinks(true);
},
setKeyIndex: function() {
var trimLoc = location.href;
// remove any trailing slash from the URL
if (trimLoc.substr(-1) === '/') {
trimLoc = trimLoc.substr(0, trimLoc.length - 1);
}
if (typeof this.keyboardNavLastIndexCache[trimLoc] === 'undefined') {
this.keyboardNavLastIndexCache[trimLoc] = {};
}
var now = Date.now();
this.keyboardNavLastIndexCache[trimLoc] = {
index: this.activeIndex,
updated: now
};
RESStorage.setItem('RESmodules.keyboardNavLastIndex', JSON.stringify(this.keyboardNavLastIndexCache));
},
handleScroll: function(e) {
if (modules['keyboardNav'].scrollTimer) {
clearTimeout(modules['keyboardNav'].scrollTimer);
}
modules['keyboardNav'].scrollTimer = setTimeout(modules['keyboardNav'].handleScrollAfterTimer, 300);
},
handleScrollAfterTimer: function() {
if ((!modules['keyboardNav'].recentKeyPress) && (!RESUtils.elementInViewport(modules['keyboardNav'].keyboardLinks[modules['keyboardNav'].activeIndex]))) {
for (var i = 0, len = modules['keyboardNav'].keyboardLinks.length; i < len; i++) {
if (RESUtils.elementInViewport(modules['keyboardNav'].keyboardLinks[i])) {
modules['keyboardNav'].keyUnfocus(modules['keyboardNav'].keyboardLinks[modules['keyboardNav'].activeIndex]);
modules['keyboardNav'].activeIndex = i;
modules['keyboardNav'].keyFocus(modules['keyboardNav'].keyboardLinks[modules['keyboardNav'].activeIndex]);
break;
}
}
}
},
scanPageForKeyboardLinks: function(isNew) {
if (typeof isNew === 'undefined') {
isNew = false;
}
var siteTable = document.getElementById('siteTable');
// check if we're on a link listing (regular page, subreddit page, etc) or comments listing...
this.pageType = RESUtils.pageType();
switch (this.pageType) {
case 'linklist':
case 'profile':
// get all links into an array...
var stMultiCheck = document.querySelectorAll('#siteTable');
// stupid sponsored links create a second div with ID of sitetable (bad reddit! you should never have 2 IDs with the same name! naughty, naughty reddit!)
if (stMultiCheck.length === 2) {
siteTable = stMultiCheck[1];
}
if (siteTable) {
this.keyboardLinks = document.body.querySelectorAll('div.linklisting .entry');
if (!isNew) {
if ((this.keyboardNavLastIndexCache[location.href]) && (this.keyboardNavLastIndexCache[location.href].index > 0)) {
this.activeIndex = this.keyboardNavLastIndexCache[location.href].index;
} else {
this.activeIndex = 0;
}
if ((this.keyboardNavLastIndexCache[location.href]) && (this.keyboardNavLastIndexCache[location.href].index >= this.keyboardLinks.length)) {
this.activeIndex = 0;
}
}
}
break;
case 'comments':
// get all links into an array...
this.keyboardLinks = document.body.querySelectorAll('#siteTable .entry, div.content > div.commentarea .entry');
if (!isNew) {
this.activeIndex = 0;
}
break;
case 'inbox':
if (siteTable) {
this.keyboardLinks = siteTable.querySelectorAll('.entry');
this.activeIndex = 0;
}
break;
}
// wire up keyboard links for mouse clicky selecty goodness...
if ((typeof this.keyboardLinks !== 'undefined') && (this.options.clickFocus.value)) {
for (var i = 0, len = this.keyboardLinks.length; i < len; i++) {
$(this.keyboardLinks[i]).parent().data('keyIndex', i)
.on('click', modules['keyboardNav'].updateKeyFocus);
}
this.keyFocus(this.keyboardLinks[this.activeIndex]);
}
},
updateKeyFocus: function(event) {
// we can't stop propagation because it breaks other buttons, so instead
// we will throttle this to only run once every 100ms. This prevents the
// issue where clicking a key highlight element also triggers click events
// on the parents, grandparents, etc due to event bubbling.
if (!modules['keyboardNav'].updateKeyFocusThrottle) {
modules['keyboardNav'].updateKeyFocusThrottle = setTimeout(function() {
delete modules['keyboardNav'].updateKeyFocusThrottle;
},100);
modules['keyboardNav'].doUpdateKeyFocus(event);
}
},
doUpdateKeyFocus: function(event) {
var thisIndex = parseInt($(event.currentTarget).data('keyIndex'), 10);
if (isNaN(thisIndex)) {
return;
}
if (modules['keyboardNav'].activeIndex !== thisIndex) {
modules['keyboardNav'].keyUnfocus(modules['keyboardNav'].keyboardLinks[modules['keyboardNav'].activeIndex]);
modules['keyboardNav'].activeIndex = thisIndex;
modules['keyboardNav'].keyFocus(modules['keyboardNav'].keyboardLinks[modules['keyboardNav'].activeIndex]);
}
},
recentKey: function() {
modules['keyboardNav'].recentKeyPress = true;
clearTimeout(modules['keyboardNav'].recentKey);
modules['keyboardNav'].recentKeyTimer = setTimeout(function() {
modules['keyboardNav'].recentKeyPress = false;
}, 1000);
},
keyFocus: function(obj) {
if ((typeof obj !== 'undefined') && (obj.classList.contains('RES-keyNav-activeElement'))) {
return false;
} else if (typeof obj !== 'undefined') {
if (this.options.mediaBrowseMode.value && this.fromKey && !modules['showImages'].haltMediaBrowseMode) {
if (this.lastLinkExpanded === true) {
var $expando = $(obj).find('.expando-button:not(.expanded)');
if ($expando.length > 0) {
this.toggleExpando();
}
}
}
delete this.fromKey;
obj.classList.add('RES-keyNav-activeElement');
this.activeElement = obj;
if ((this.pageType === 'linklist') || (this.pageType === 'profile')) {
this.setKeyIndex();
}
if ((this.pageType === 'comments') && (this.options.commentsLinkNumbers.value)) {
var links = this.getCommentLinks(obj);
var annotationCount = 0;
for (var i = 0, len = links.length; i < len; i++) {
var annotation = document.createElement('span');
annotationCount++;
$(annotation).text('[' + annotationCount + '] ');
if (annotationCount <= 9) {
annotation.title = 'press ' + annotationCount + ' to open link';
}
else if (annotationCount === 10) {
annotation.title = 'press 0 to open link';
}
else {
annotation.title = 'press ' + this.getNiceKeyCode('toggleCmdLine') + ' then ' +annotationCount + ' and Enter to open link';
}
annotation.classList.add('keyNavAnnotation');
if (modules['keyboardNav'].options.commentsLinkNumberPosition.value === 'right') {
RESUtils.insertAfter(links[i], annotation);
} else {
links[i].parentNode.insertBefore(annotation, links[i]);
}
}
}
}
},
handleKeyLink: function(link) {
var button = 0;
if (modules['keyboardNav'].options.commentsLinkNewTab.value) {
button = 1;
}
if (link.classList.contains('toggleImage')) {
RESUtils.click(link);
return false;
}
var thisURL = link.getAttribute('href'),
isLocalToPage = (thisURL.indexOf('reddit') !== -1) && (thisURL.indexOf('comments') !== -1) && (thisURL.indexOf('#') !== -1);
if ((!isLocalToPage) && (button === 1)) {
var thisJSON = {
requestType: 'keyboardNav',
linkURL: thisURL,
button: button
};
BrowserStrategy.sendMessage(thisJSON);
} else {
location.href = this.getAttribute('href');
}
},
keyUnfocus: function(obj) {
if (this.options.mediaBrowseMode.value && this.fromKey && !modules['showImages'].haltMediaBrowseMode) {
var $expando = $(obj).children('.expando-button');
if ($expando.length > 0) {
if ($expando.hasClass('expanded')) {
this.lastLinkExpanded = true;
this.toggleExpando();
} else {
this.lastLinkExpanded = false;
}
}
}
obj.classList.remove('RES-keyNav-activeElement');
if (this.pageType === 'comments') {
var annotations = obj.querySelectorAll('div.md .keyNavAnnotation');
for (var i = 0, len = annotations.length; i < len; i++) {
annotations[i].parentNode.removeChild(annotations[i]);
}
}
modules['hover'].close(false);
},
drawHelp: function() {
this.keyHelp = RESUtils.createElementWithID('div', 'keyHelp');
var helpTable = document.createElement('table');
this.keyHelp.appendChild(helpTable);
var helpTableHeader = document.createElement('thead');
var helpTableHeaderRow = document.createElement('tr');
var helpTableHeaderKey = document.createElement('th');
$(helpTableHeaderKey).text('Key');
helpTableHeaderRow.appendChild(helpTableHeaderKey);
var helpTableHeaderFunction = document.createElement('th');
$(helpTableHeaderFunction).text('Function');
helpTableHeaderRow.appendChild(helpTableHeaderFunction);
helpTableHeader.appendChild(helpTableHeaderRow);
helpTable.appendChild(helpTableHeader);
var helpTableBody = document.createElement('tbody');
var isLink = /^link[\d]+$/i;
for (var i in this.options) {
if ((this.options[i].type === 'keycode') && (!isLink.test(i))) {
var thisRow = document.createElement('tr');
var thisRowKey = document.createElement('td');
var thisKeyCode = this.getNiceKeyCode(i);
$(thisRowKey).text(thisKeyCode);
thisRow.appendChild(thisRowKey);
var thisRowDesc = document.createElement('td');
$(thisRowDesc).text(this.options[i].description);
thisRow.appendChild(thisRowDesc);
helpTableBody.appendChild(thisRow);
}
}
helpTable.appendChild(helpTableBody);
document.body.appendChild(this.keyHelp);
this.helpDrawn = true;
},
getNiceKeyCode: function(optionKey) {
var keyCodeArray = this.options[optionKey].value;
if (!keyCodeArray) {
return;
}
if (typeof keyCodeArray === 'string') {
keyCodeArray = parseInt(keyCodeArray, 10);
}
if (typeof keyCodeArray === 'number') {
keyCodeArray = [keyCodeArray, false, false, false, false];
}
var niceKeyCode = RESUtils.niceKeyCode(keyCodeArray);
return niceKeyCode;
},
handleKeyPress: function(e) {
var konamitest = (typeof konami === 'undefined') || (!konami.almostThere);
if ((document.activeElement.tagName === 'BODY') && (konamitest)) {
// comments page, or link list?