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
// ==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, article, lastClick, selected, moveArticle, minArticle,
maxArticle, openLink, inHistory, goToSubReddit;
 
table = doc.getElementById('siteTable');
article = table.childNodes;
lastClick = ' last-clicked';
 
minArticle = 0;
maxArticle = article.length - 2;
 
selected = {
current: article[0],
last: null,
index: 0
};
 
moveArticle = function(down) {
var trimClass, classList;
if(down) {
selected.last = selected.current;
selected.index = (selected.index < maxArticle) ?
selected.index + 2 : selected.index;
selected.current = article[selected.index];
}else {
selected.last = selected.current;
selected.index = (selected.index > minArticle) ?
selected.index - 2 : selected.index;
selected.current = article[selected.index];
}
 
if(selected.current !== selected.last) {
selected.current.className += lastClick;
classList = selected.last.className.split(' ');
selected.last.className = classList
.filter(function(x) {
return (x != '') &&
(x != lastClick.trim());
}).join(' ');
selected.current.scrollIntoViewIfNeeded();
}
};
 
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);
};
 
inHistory = function(forward) {
var history = win.history;
if(history) {
if(forward) history.forward();
else history.back();
}
};
 
goToSubReddit = function(reddit) {
var currentLocation, link;
currentLocation = win.location.href;
 
if(currentLocation === 'http://www.reddit.com/') {
link = 'r/' + reddit;
}else {
link = reddit;
}
win.location.replace(link);
};
 
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.