Next Image/Previous Image

Quick scroll to next/previous image on a page with n/p buttons (now also looks for canvas and iframe elements)

当前为 2018-01-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Next Image/Previous Image
  3. // @author arty <me@arty.name>
  4. // @namespace http://arty.name/
  5. // @version 2.0.7
  6. // @description Quick scroll to next/previous image on a page with n/p buttons (now also looks for canvas and iframe elements)
  7. // @include *
  8. // ==/UserScript==
  9.  
  10. // This is a minor adaptation of arty's original, by joeytwiddle
  11.  
  12. // 2012/10 - Now sorting positions so out-of-order images do not break the sequence.
  13.  
  14. (function(){
  15. // Original keys: forward and back
  16. // var forwardButton = 102; // F
  17. // var backwardButton = 114; // R
  18. // Current keys: next and previous
  19. var forwardButton = 110; // N
  20. var backwardButton = 112; // P
  21. // For Vim / Facebook / 9gag lovers:
  22. // var forwardButton = 106; // J
  23. // var backwardButton = 107; // K
  24. var leeway = 2; // This is needed if you have zoomed out the page. (We might try to set scrollTop to 100, but it will only move to 99.)
  25.  
  26. var positions = [];
  27.  
  28. document.addEventListener('keypress', function(event){
  29. if (event.ctrlKey || event.shiftKey || event.altKey) return;
  30. var code = event.keyCode || event.which;
  31. if (code != backwardButton && code != forwardButton) return;
  32. if (event.target.tagName && event.target.tagName.match(/input|select|textarea/i) || event.target.getAttribute('contenteditable')==="true") return;
  33.  
  34. // We force a rescan of the page's images every time, for dynamic pages.
  35. positions = [];
  36. if (positions.length === 0) {
  37. //const images = document.images;
  38. const images = document.querySelectorAll('image, canvas, iframe');
  39. for (var index = 0; index < images.length; index++) {
  40. var image = images[index];
  41. if (image.clientWidth * image.clientHeight < 200*200) continue;
  42. var ytop = getYOffset(image);
  43. // Vertically centralise smaller images.
  44. if (image.clientHeight && image.clientHeight < window.innerHeight) {
  45. ytop -= (window.innerHeight - image.clientHeight)/2 | 0;
  46. }
  47. positions.push([index, ytop]);
  48. }
  49. }
  50. positions.sort(function(a,b) {
  51. return a[1] - b[1];
  52. });
  53.  
  54. var scroll = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
  55.  
  56. if (code === forwardButton) {
  57. for (index = 0; index < positions.length; index++) {
  58. if (positions[index][1] <= scroll + leeway) continue;
  59. // Hard to detect which one our browser is using when we are at the top of the document.
  60. // Because Chrome presents documentElement.scrollTop = 0 all the time!
  61. // Likewise Firefox presents document.body.scrollTop = 0 all the time!
  62. // Solution? Just set both of them!
  63. document.body.scrollTop = positions[index][1];
  64. document.documentElement.scrollTop = positions[index][1];
  65. return;
  66. }
  67. } else if (code === backwardButton) {
  68. for (index = positions.length - 1; index >= 0; index--) {
  69. if (positions[index][1] >= scroll - leeway) continue;
  70. document.body.scrollTop = positions[index][1];
  71. document.documentElement.scrollTop = positions[index][1];
  72. return;
  73. }
  74. }
  75.  
  76. }, false);
  77.  
  78. function getYOffset(node) {
  79. for (var offset = 0; node; offset += node.offsetTop, node = node.offsetParent);
  80. return offset;
  81. }
  82. })();

QingJ © 2025

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