gingerbeardman

158521.user.js

Feb 12th, 2013
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           Instagram Stream Keyboard Navigation
  3. // @description    Adding key navigation to instagram's new online stream (J/K ./, right/left down/up) and highlights "current" photo
  4. // @version        1.1.0
  5. // @author         Pedro Gaspar & Matt Sephton
  6. // @include        http://www.instagram.com/*
  7. // @include        http://instagram.com/*
  8. // ==/UserScript==
  9.  
  10. // a function that loads jQuery and calls a callback function when jQuery has finished loading
  11. function addJQuery(callback) {
  12.   var script = document.createElement("script");
  13.   script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
  14.   script.addEventListener('load', function() {
  15.     var script = document.createElement("script");
  16.     script.textContent = "window.jQ=jQuery.noConflict(true);jQ(document).ready(function() {(" + callback.toString() + ")()});";
  17.     document.body.appendChild(script);
  18.   }, false);
  19.   document.body.appendChild(script);
  20. }
  21.  
  22. // the guts of this userscript
  23. function main() {
  24.   // Note, jQ replaces $ to avoid conflicts.
  25.  
  26.   var curr = jQ(".timelineItem").first();
  27.   var not_last = ":not(.timelineLast)";
  28.  
  29.   jQ(document).keydown(function(event) {
  30.     if (!event)
  31.       event = window.event;
  32.  
  33.     var code = event.keyCode ? event.keyCode : event.which;
  34.    
  35.     switch(code)
  36.     {
  37.       case 74: // J
  38.       case 190: // .
  39.         nextImage();
  40.       break;
  41.      
  42.       case 75: // K
  43.       case 188: // ,
  44.         prevImage();
  45.       break;
  46.      
  47.       case 76: // L
  48.         likeImage();
  49.       break;
  50.      
  51.       case 37: // left
  52.       case 38: // up
  53.         event.preventDefault();
  54.         prevImage();
  55.         return false;
  56.       break;
  57.      
  58.       case 39: // right
  59.       case 40: // down
  60.         event.preventDefault();
  61.         nextImage();
  62.         return false;
  63.       break;
  64.      
  65.       default:
  66.         // alert("key code: "+ event.which);
  67.      
  68.       return true;
  69.     }
  70.  
  71.     function likeImage() {
  72.       jQ(curr).find('.timelineLikes a').trigger('click');
  73.     }
  74.  
  75.     function nextImage() {
  76.         jQ("body").scrollTop(curr.next(not_last).offset().top);
  77.         jQ(curr).find('.timelineCard, .timelinePhoto::after').css({'border-color':'#C0C0C0', 'box-shadow': '0 1px 16px rgba(0, 0, 0, 0.1)'});
  78.         curr = curr.next(not_last);
  79.         jQ(curr).find('.timelineCard, .timelinePhoto::after').css({'border-color':'#808080', 'box-shadow': '0 1px 16px rgba(0, 0, 0, 0.5)'});
  80.     }
  81.    
  82.     function prevImage() {
  83.         jQ("body").scrollTop(curr.prev(not_last).offset().top);
  84.         jQ(curr).find('.timelineCard, .timelinePhoto::after').css({'border-color':'#C0C0C0', 'box-shadow': '0 1px 16px rgba(0, 0, 0, 0.1)'});
  85.         curr = curr.prev(not_last);
  86.         jQ(curr).find('.timelineCard, .timelinePhoto::after').css({'border-color':'#808080', 'box-shadow': '0 1px 16px rgba(0, 0, 0, 0.5)'});
  87.     }
  88.   });
  89. }
  90.  
  91. // load jQuery and execute the main function
  92. addJQuery(main);
Advertisement
Add Comment
Please, Sign In to add comment