imgur scroll to gallery images keyboard shortcuts

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

当前为 2015-06-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name imgur scroll to gallery images keyboard shortcuts
  3. // @namespace http://porath.org/
  4. // @version 0.12
  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 = $('div.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 ($('div#album-truncated')) {
  22. $('div#album-truncated > a').click();
  23. elems = $('div.image');
  24. numElems = elems.length;
  25. }
  26. return;
  27. }
  28. elems[current + 1].scrollIntoView();
  29. $('body').scrollTop($('body').scrollTop() - 28);
  30. current = current + 1;
  31. }
  32. if (key.which == 38) {
  33. key.preventDefault();
  34. if (current == 0) {
  35. return;
  36. }
  37. elems[current - 1].scrollIntoView();
  38. $('body').scrollTop($('body').scrollTop() - 28);
  39. current = current - 1;
  40. }
  41. if (key.which == 36) { // "home" puts you at the top of the gallery
  42. current = 0;
  43. }
  44. if (key.which == 35) { // "end" puts you at the bottom of the gallery
  45. current = numElems;
  46. }
  47. if (key.which == 37 || key.which == 39) { // when a user presses left or right to go to the prev/next page
  48. current = 0;
  49. elems = $('div.image');
  50. numElems = elems.length;
  51. }
  52. });