forked from maxko87/hnsubmit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomment_shortcuts.js
114 lines (92 loc) · 3.13 KB
/
comment_shortcuts.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
$(document).ready(function () {
var first = $('td.default:first').parents('tr:first').parents('tr:first');
var last = $('td.default:last').parents('tr:first').parents('tr:first');
var current = first;
current.find('tr:first').prepend("<div class='selector'>|</div>");
//add leading space to all comments to prevent the bump glitch
var sibs = current.siblings('tr');
sibs.each(function (){
$(this).find('tr:first').prepend("<div class='selector'></div>");
});
$(window).keypress(function(e) {
//if focus not on text box
if (!$("*:focus").is("textarea")){
var key = e.which;
//focus on text box
if (key == "/".charCodeAt(0)){
$('textarea').focus();
e.preventDefault();
}
//move down
else if (key == "j".charCodeAt(0)){
if (current.text() != last.text()){
current.find('div.selector').replaceWith("<div class='selector'> </div>");
var next = current.next();
next.find('div.selector').text('|');
current = next;
scroll(current);
}
}
//move up
else if (key == "k".charCodeAt(0)){
if (current.text() != first.text()){
current.find('div.selector').replaceWith("<div class='selector'> </div>");
var prev = current.prev();
prev.find('div.selector').text('|');
current = prev;
scroll(current);
}
}
//reply
else if (key == "r".charCodeAt(0)){
var link = current.find('td.default').find('u').children('a').attr('href');
open_link(link);
}
//upvote
else if (key == "v".charCodeAt(0) ){
current.find('a[id^="up"]:first').click();
}
//downvote
else if (key == "d".charCodeAt(0) ){
current.find('a[id^="down"]').click();
}
}
});
//accounts for scrolling both up and down
function scroll(current){
//scroll down if element goes below window
if (current.offset().top + current.height() > $(window).scrollTop() + window.innerHeight){
var drop = current.height();
$('body').animate({
scrollTop: $(window).scrollTop() + drop
}, 0);
}
//scroll if element goes above window
if (current.offset().top < $(window).scrollTop()){
$('body').animate({
scrollTop: current.offset().top
}, 0);
}
//scroll down if element goes below window
if (current.offset().top + current.height() > $(window).scrollTop() + window.innerHeight){
var drop = current.height() + current.next().height() + current.next().next().height();
$('body').animate({
scrollTop: $(window).scrollTop() + drop
}, 0);
}
//scroll up if element goes above window
else if (current.offset().top < $(window).scrollTop()){
$('body').animate({
scrollTop: current.offset().top
}, 0);
}
}
//opens the link currently selected in same window
function open_link(link) {
document.location.href = link;
}
//opens the link currently selected in new tab
function open_link_new_tab(link) {
chrome.extension.sendMessage({link: link});
}
});