Created
September 2, 2011 02:01
-
-
Save sendos/1187770 to your computer and use it in GitHub Desktop.
HN comments timeline
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 HN comments timeline | |
// @namespace sendos | |
// @description Moves through HN comments chronologically, using the right and left arrow keys | |
// @include http://news.ycombinator.com/item?id=* | |
// ==/UserScript== | |
function selectPost(new_idx) | |
{ | |
prev_post_idx = current_post_idx; | |
current_post_idx = new_idx % sortedPostInfo.length; | |
if (current_post_idx < 0) { current_post_idx += sortedPostInfo.length; } | |
window.scroll(0,sortedPostInfo[current_post_idx].value); | |
var post = document.getElementById(sortedPostInfo[current_post_idx].key); | |
var idx = current_post_idx+1; | |
var descr = (idx==1) ? '' : (idx==2) ? '2nd ' : (idx==3) ? '3rd ' : idx+'th '; | |
post.innerHTML += '<span style="border: 2px solid rgb(255, 0, 0);margin-left:5px;padding-right:5px;"> ' + descr + 'most recent post </span>'; | |
var prev_post = document.getElementById(sortedPostInfo[prev_post_idx].key); | |
prev_post.innerHTML = prev_post.innerHTML.replace(/<span style=\"border: 2px solid rgb\(255, 0, 0\); *margin-left: *5px; *padding-right: *5px;\"> .*most recent post <\/span>/,""); | |
} | |
function detectArrows(evt) { | |
evt = (evt) ? evt : ((window.event) ? window.event : null); | |
if (evt) | |
{ | |
switch (evt.keyCode) | |
{ | |
case 37: // Left arrow key | |
selectPost(current_post_idx-1); | |
break; | |
case 39: // Right arrow key | |
selectPost(current_post_idx+1); | |
break; | |
} | |
} | |
}; | |
var sortedPostInfo; | |
var current_post_idx = -1; | |
var prev_post_idx = -1; | |
(function() | |
{ | |
//Finds y value of given object | |
function findPos(obj) | |
{ | |
var curtop = 0; | |
if (obj.offsetParent) | |
{ | |
do { curtop += obj.offsetTop; } | |
while (obj = obj.offsetParent); | |
return [curtop]; | |
} | |
} | |
function sortObj(object, sortFunc) | |
{ | |
var rv = []; | |
for (var k in object) | |
{ | |
if (object.hasOwnProperty(k)) rv.push({key: k, value: object[k]}); | |
} | |
rv.sort(function(o1, o2) { return sortFunc(o1.key, o2.key); }); | |
return rv; | |
} | |
var allPosts = document.getElementsByClassName('comhead'); | |
var postInfo = new Array(); | |
for (var i = 0; i < allPosts.length; i++) | |
{ | |
var post = allPosts[i]; | |
if (post.innerHTML.match(/a href=\"item\?id=(\d+)\"/)) | |
{ | |
// Store post y-location and item id | |
var itemid = RegExp.$1; | |
var ypos = findPos(post); | |
post.id = itemid; | |
postInfo[itemid] = ypos; | |
} | |
else | |
{ | |
//alert('problem with post ' + i + ': ' + post.innerHTML); | |
} | |
} | |
sortedPostInfo = sortObj(postInfo, function(a,b) {return b - a;}); | |
window.addEventListener('keyup', detectArrows, true); | |
//document.onkeyup = detectArrows; | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment