Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Inspired by <http://siasia.github.com/backbone-reddit/>, keyboard shortcuts for reddit.com itself with a GreaseMonkey script.
// ==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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.