forked from maxko87/hnsubmit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_shortcuts.js
187 lines (153 loc) · 5.53 KB
/
main_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
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
$(document).ready(function () {
$('.title').each(function(i, node) {
if ($(this).text().length == 2){
$(this).text("0" + $(this).text());
}
});
//don't load for creating account page
if ($('*:contains("Create Account")').length > 0){
return;
}
//initialization
var first = $('td.title:first');
var last = $('td.title:last').parent().prev().prev().prev().prev().children('.title:first');
var current = first;
current.addClass('selector');
current.removeClass('title');
current.text("|" + current.text());
var keys = new Array();
$(window).keypress(function(e) {
var key = e.which;
// array already exists, number was pressed
if(key >= "0".charCodeAt(0) && key <= "9".charCodeAt(0)) {
keys.push(key - "0".charCodeAt(0));
//if we have two numbers exactly, goto that item
if (keys.length == 2){
var goto_el = String(keys[0]) + String(keys[1]) + '.';
var next = $('td.title:contains("' + goto_el + '"):first');
if(next.length != 0){
move_selector(current, next);
current = next;
scroll(current);
}
keys = new Array();
}
}
else {
keys = new Array();
//move down
if (key == "j".charCodeAt(0)){
if (current.text() != last.text()){
var next = current.parent().next().next().next().children('td.title:first');
move_selector(current, next);
current = next;
scroll(current);
}
}
//move up
else if (key == "k".charCodeAt(0) ){
if (current.text() != first.text()){
var prev = current.parent().prev().prev().prev().children('.title:first');
move_selector(current, prev);
current = prev;
scroll(current);
}
}
//open link in same window
else if (key == "o".charCodeAt(0) || key=="13"){
var link = current.next().next().children('a').attr('href');
open_link(link);
}
//open link in new tab
else if (key == "O".charCodeAt(0) ){
var link = current.next().next().children('a').attr('href');
open_link_new_tab(link);
}
//open comments in same window
else if (key == "c".charCodeAt(0)){
var link = current.parent().next().children('td.subtext').children('a:last').attr('href');
open_link(link);
}
//open comments in new tab
else if (key == "C".charCodeAt(0) ){
var link = current.parent().next().children('td.subtext').children('a:last').attr('href');
open_link_new_tab("http://news.ycombinator.com/" + link);
}
//upvote
else if (key == "v".charCodeAt(0) ){
current.next().children('center').children('a[id^=up]:first').click();
}
//downvote
else if (key == "d".charCodeAt(0) ){
current.next().children('center').children('a[id^=down]:first').click();
}
// add to pocket
else if (key == "p".charCodeAt(0) ){
var POCKET_CONSUMER_KEY = "11476-982a9496933a2d06880c143e";
var POCKET_ADD_URL = "https://getpocket.com/v3/add";
var link = current.next().next().children('a').attr('href');
var title = current.next().next().children('a').text();
// TODO auth with pocket and persist http://getpocket.com/developer/docs/authentication
// http://api.jquery.com/jQuery.post/
$.post(POCKET_ADD_URL,
{"url":link, // Need to escape?
"title":title,
"tags":"HN",
"consumer_key":POCKET_CONSUMER_KEY,
"access_token":"5678defg-5678-defg-5678-defg56"},
function(data){
console.log(data); // John
}, "json");
// TODO visual cue that it's in pocket
// TODO elsewhere, check if link in pocket and show visual cue
}
//next page
else if (key == "m".charCodeAt(0) ){
var link = $('a:contains("More"):last').attr('href');
open_link(link);
}
}
});
//accounts for scrolling both up and down
function scroll(current){
//scroll down if element goes below window
var drop = current.height() + current.next().height() + current.next().next().height();
if (current.offset().top + drop > $(window).scrollTop() + window.innerHeight){
$('body').animate({
scrollTop: current.offset().top - window.innerHeight + drop
}, 0);
}
//scroll up if element goes above window
else if (current.offset().top < $(window).scrollTop()){
//var drop = current.height() + current.next().height() + current.next().next().height();
$('body').animate({
scrollTop: current.offset().top
}, 0);
}
}
//moves cursor up or down
function move_selector(current, node) {
current.addClass('title');
current.removeClass('selector');
current.text(current.text().substring(1,current.text().length));
node.addClass('selector');
node.removeClass('title');
node.text("|" + node.text().substring(0,node.text().length));
}
//opens the link currently selector 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});
}
function isScrolledIntoView(elem)
{
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elem).offset().top;
var elemBottom = elemTop + $(elem).height();
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
});