Skip to content

Instantly share code, notes, and snippets.

@ianthekirkland
Forked from horimislime/fb_jk_scroll.user.js
Created August 12, 2012 01:13
Show Gist options
  • Save ianthekirkland/3328605 to your computer and use it in GitHub Desktop.
Save ianthekirkland/3328605 to your computer and use it in GitHub Desktop.
Scrolls Facebook wall posts with j/k key
// ==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