Created
December 6, 2011 14:12
-
-
Save horimislime/1438330 to your computer and use it in GitHub Desktop.
Scrolls Facebook wall posts with j/k key
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 fb_jk_scroll | |
// @version 0.1 | |
// @description Scrolls wall posts with j/k key | |
// @include http://www.facebook.com/* | |
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js | |
// ==/UserScript== | |
$(document).ready(function(){ | |
var posts=document.getElementById("home_stream").childNodes; | |
var barHeight=document.getElementById("blueBar").clientHeight; | |
$(window).keydown(function(e){ | |
if(posts[posts.length-1]<$(window).scrollTop()){ | |
posts=document.getElementById("home_stream").childNodes; | |
} | |
if(e.keyCode==74){//j key | |
scrollDown(); | |
} | |
else if(e.keyCode==75){//k key | |
scrollUp(); | |
} | |
}); | |
function scrollDown(){ | |
scrollTo(nearestIndex()+1); | |
} | |
function scrollUp(){ | |
scrollTo(nearestIndex()-1); | |
} | |
function scrollTo(index){ | |
if(index<0) index=0; | |
else if(posts.length<=index) index=posts.length-1; | |
$('html,body').animate({scrollTop: $("#"+posts[index].id).offset().top-barHeight+2},'fast'); | |
} | |
function nearestIndex(){ | |
var coord=$(window).scrollTop(); | |
if(coord<$("#"+posts[0].id).offset().top-barHeight){ | |
return -1; | |
} | |
for(var i=0;i<posts.length;i++){ | |
var top=$("#"+posts[i].id).offset().top-barHeight; | |
var bottom=top+$("#"+posts[i].id).height(); | |
if(top<=coord && coord<bottom) return i; | |
} | |
return posts.length-1; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment