Inspired by <http://siasia.github.com/backbone-reddit/>, keyboard shortcuts for reddit.com itself with a GreaseMonkey script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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() { | |
'use strict'; | |
var doc, table, tables, articles, lastClick, selected, searchbar | |
, moveArticle, minArticle, maxArticle, openLink, inHistory | |
, keybindFn, goToSubReddit, win; | |
win = this; | |
doc = win.document; | |
tables = doc.querySelectorAll('.sitetable'); | |
searchbar = doc.querySelector('#search').children[0]; | |
table = (tables.length > 1) ? tables[1] : tables[0]; | |
articles = table.children; | |
lastClick = ' last-clicked'; | |
minArticle = 0; | |
maxArticle = articles.length - 2; | |
selected = { | |
current: articles[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 = articles[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; | |
}; | |
keybindFn = function(e) { | |
var key = e.keyCode; | |
if(e.shiftKey || e.altKey || e.ctrlKey || e.metaKey) { | |
return; | |
} | |
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(win.prompt('Sub-reddit?: ')); | |
break; | |
default: | |
break; | |
} | |
}; | |
win.addEventListener('keydown', keybindFn, false); | |
// on search bar focus remove keybindings | |
searchbar.addEventListener('focus', function() { | |
win.removeEventListener('keydown', keybindFn, false); | |
}, false); | |
// on search bar blur add back keybindings | |
searchbar.addEventListener('blur', function() { | |
win.addEventListener('keydown', keybindFn, false); | |
}, false); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment