imgur scroll to gallery images keyboard shortcuts

use ↑/↓ to scroll to the next/previous image within an imgur gallery

  1. // ==UserScript==
  2. // @name imgur scroll to gallery images keyboard shortcuts
  3. // @namespace http://porath.org/
  4. // @version 0.13
  5. // @description use ↑/↓ to scroll to the next/previous image within an imgur gallery
  6. // @author porath
  7. // @match http://imgur.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. // thanks to moiph and CBenni and Brybry
  12.  
  13. var current = 0;
  14. var elems = $('.album-image');
  15. var numElems = elems.length;
  16.  
  17. $(document).on('keydown', function (key) {
  18. if (key.which == 40) {
  19. key.preventDefault();
  20. if (current + 1 == numElems) {
  21. if ($('#album-truncated')) {
  22. $('#album-truncated > a').click();
  23. window.setTimeout(function() { // wait 200 ms then update the album size
  24. elems = $('.album-image');
  25. numElems = elems.length;
  26. }, 200);
  27. }
  28. return;
  29. }
  30. elems[current + 1].scrollIntoView();
  31. $('body').scrollTop($('body').scrollTop() - 28);
  32. current = current + 1;
  33. }
  34. if (key.which == 38) {
  35. key.preventDefault();
  36. if (current == 0) {
  37. return;
  38. }
  39. elems[current - 1].scrollIntoView();
  40. $('body').scrollTop($('body').scrollTop() - 28);
  41. current = current - 1;
  42. }
  43. if (key.which == 36) { // "home" puts you at the top of the gallery
  44. current = 0;
  45. }
  46. if (key.which == 35) { // "end" puts you at the bottom of the gallery
  47. current = numElems;
  48. }
  49. if (key.which == 37 || key.which == 39) { // when a user presses left or right to go to the prev/next page
  50. current = 0;
  51. elems = $('.album-image');
  52. numElems = elems.length;
  53. }
  54. });