public
Last active

Inspired by <http://siasia.github.com/backbone-reddit/>, keyboard shortcuts for reddit.com itself with a GreaseMonkey script.

  • Download Gist
keyit.user.js
JavaScript
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
// ==UserScript==
// @name Keyit
// @description Keyboard based navigation for reddit
// @include http*://www.reddit.com/
// @include http*://www.reddit.com/r/*
// ==UserScript==
 
// Keyboard shortcuts:
//
// j => move down
// k => move up
// o => open link
// c => open comments
// n => next in history (forward)
// p => previous in history (back)
// / => go to a sub-reddit
 
(function(win, doc) {
'use strict';
var table, tables, article, lastClick, selected, moveArticle,
minArticle, maxArticle, openLink, inHistory, goToSubReddit;
 
tables = doc.getElementsByClassName('sitetable');
table = (tables.length > 1) ? tables[1] : tables[0];
article = table.children;
lastClick = ' last-clicked';
 
minArticle = 0;
maxArticle = article.length - 2;
 
selected = {
current: article[0],
last: null,
index: 0
};
 
/**
* @function
* @name moveArticle
* @description move up and down across all listed articles.
* @param bool down If true, the function will move down and
* vise-versa for false
* @return void
*/
moveArticle = function(down) {
var trimClass, classList;
 
selected.last = selected.current;
 
if(down) {
selected.index = (selected.index < maxArticle) ?
selected.index + 2 : selected.index;
}else {
selected.index = (selected.index > minArticle) ?
selected.index - 2 : selected.index;
}
 
selected.current = article[selected.index];
 
if(selected.current !== selected.last) {
trimClass = lastClick.trim();
selected.current.className += lastClick;
classList = selected.last.className.split(' ');
selected.last.className =
classList.filter(function(x) {
return (x != '') && (x != trimClass);
}).join(' ');
selected.current.scrollIntoViewIfNeeded();
}
};
 
/**
* @function
* @name openLink
* @description open the currently selected article or comment in a
* separate tab.
* @param bool title Open up the title link if it is true and
* if false it opens the comments.
* @return void
*/
openLink = function(title) {
var htmlStr, links, linkmatch, link, regLink, tempLink;
 
htmlStr = selected.current.innerHTML;
links = htmlStr.split(' ')
.filter(function(x) {return /href/.test(x);});
regLink = /http.+"/;
if(title) {
tempLink = links[0];
}else {
tempLink = links.filter(function(x) { return /comments/.test(x); })
}
linkmatch = regLink.exec(tempLink)[0];
link = linkmatch.replace('"', '');
win.open(link);
};
 
/**
* @function
* @name inHistory
* @description handles going back and forth in history.
* @param bool forward If true, you get moved forward in
* history and back if false.
* @return void
*/
inHistory = function(forward) {
var history = win.history;
if(history) {
if(forward) history.forward();
else history.back();
}
};
 
/**
* @function
* @name goToSubReddit
* @description redirect to a current sub-reddit that is typed in by
* the user.
* @param string reddit Open up sub-reddit from the value
* @return void
*/
goToSubReddit = function(reddit) {
var base = "http://www.reddit.com/";
 
win.location.href = base + 'r/' + reddit;
};
 
win.addEventListener('keydown', function(e) {
var key = e.keyCode;
switch(key) {
case 74: // j
moveArticle(1);
break;
case 75: // k
moveArticle(0);
break;
case 79: // o
openLink(1);
break;
case 67: // c
openLink(0);
break;
case 80: // p
inHistory(0);
break;
case 78: // n
inHistory(1);
break;
case 82: // r
win.location.reload();
break;
case 191: // /
goToSubReddit(prompt('Sub-reddit?: '));
break;
default:
break;
};
},false);
})(unsafeWindow, unsafeWindow.document);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.