Next Previous Image Key Navigation

Quick scroll to next/previous image on a page with f/r buttons

  1. // ==UserScript==
  2. // @name Next Previous Image Key Navigation
  3. // @author waka <me@waka.name>
  4. // @namespace http://waka.name/
  5. // @version 1.0.1
  6. // @description Quick scroll to next/previous image on a page with f/r buttons
  7. // @include *
  8. // ==/UserScript==
  9.  
  10. (function(){
  11. var sizeLimit = 200;
  12. var rButton = 114;
  13. var fButton = 102;
  14. var positions = [];
  15. var offsetshift = 0;
  16. //shift pixels down to avoid floating bars - format: domain,pixels,domain,pixels,...
  17. //example: http://foo.example.com/ -> domain=example - assume 25 pixels shift -> ,"example","25"
  18. var shift=["facebook","40","sankakucomplex","25","soup","34","reddit","50"];
  19. document.addEventListener('keypress', keypressHandler, false);
  20. function keypressHandler(event){
  21. if (event.ctrlKey || event.shiftKey || event.altKey) return;
  22. if (event.target.tagName && event.target.tagName.match(/input|select|textarea/i)) return;
  23.  
  24. var code = event.keyCode || event.which;
  25. if (code != rButton && code != fButton) return;
  26.  
  27. if (positions.length < document.images.length) {
  28. positions = [];
  29. for (var index = 0; index < document.images.length; index++) {
  30. var image = document.images[index];
  31. if (Math.min(image.width, image.height) < sizeLimit) continue;
  32. positions.push(getYOffset(image));
  33. }
  34. }
  35.  
  36. var scroll = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
  37.  
  38. positions = positions.sort(sort);
  39. var next = true;
  40.  
  41. if (code == rButton) {
  42. positions = positions.reverse();
  43. next = false;
  44. }
  45. var domain = window.location.host.split('.')[1];
  46. var length = shift.length,
  47. element = null;
  48. for (var i = 0; i < length; i++) {
  49. element = shift[i];
  50. // Do something with element i.
  51. if (domain == element) {
  52. p = i + 1;
  53. offsetshift = shift[p];
  54. }
  55. i++;
  56. }
  57.  
  58. for (index = 0; index < positions.length; index++) {
  59. var offset = positions[index] - offsetshift;
  60. if ((next && offset <= scroll) || (!next && offset >= scroll)) continue;
  61. scrollTo(offset, scroll);
  62. return;
  63. }
  64. }
  65. function scrollTo(offset, currentScroll) {
  66. if (currentScroll == document.documentElement.scrollTop) {
  67. document.documentElement.scrollTop = offset;
  68. } else {
  69. document.body.scrollTop = offset;
  70. }
  71. }
  72. function getYOffset(node) {
  73. for (var offset = 0; node; offset += node.offsetTop, node = node.offsetParent);
  74. return offset;
  75. }
  76. function sort(a, b) { return a < b ? -1 : 1; }
  77. })()

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址