Skip to content

Instantly share code, notes, and snippets.

@sourrust
Last active September 30, 2015 01:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sourrust/1703552 to your computer and use it in GitHub Desktop.
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, 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment