Next Image/Previous Image with Alignment and Large Image Support

Scroll down/up by aligning images

当前为 2025-01-23 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Next Image/Previous Image with Alignment and Large Image Support
// @author       rekt
// @license      MIT
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Scroll down/up by aligning images
// @include      *
// @grant        unsafeWindow
// ==/UserScript==

(function() {
  const KEY_W = "w";
  const KEY_S = "s";

  let positions = [];

  function findNonTruncatedPosition(initialPosition, viewportHeight) {
    let currentPosition = initialPosition;
    let found = false;
    while (!found) {
      found = true;
      for (let i = 0; i < positions.length; i++) {
        const [, imgTop, imgBottom] = positions[i];
        if (imgTop < currentPosition && imgBottom > currentPosition) {
          currentPosition = imgTop;
          found = false;
          break;
        }
      }
    }
    return currentPosition;
  }

  function scrollToNextImage(currentScroll, viewportHeight) {
    const nextScrollPosition = currentScroll + viewportHeight;
    const currentBottomEdge = currentScroll + viewportHeight;

    // First check for large images that extend beyond viewport
    for (let i = 0; i < positions.length; i++) {
      if (positions[i][1] <= currentScroll && positions[i][2] > currentBottomEdge) {
        // Large image case: find next image after this one
        for (let j = i + 1; j < positions.length; j++) {
          if (positions[j][1] > positions[i][1]) {
            return findNonTruncatedPosition(positions[j][1], viewportHeight);
          }
        }
        // If no next image found, scroll by viewport height
        return findNonTruncatedPosition(nextScrollPosition, viewportHeight);
      }
    }

    // Normal case: handle truncated images
    for (let i = 0; i < positions.length; i++) {
      if (positions[i][2] > currentBottomEdge && positions[i][1] < nextScrollPosition) {
        return findNonTruncatedPosition(positions[i][1], viewportHeight);
      }
    }

    return findNonTruncatedPosition(nextScrollPosition, viewportHeight);
  }

  function scrollToPreviousImage(currentScroll, viewportHeight) {
    const previousScrollPosition = currentScroll - viewportHeight;
    const newTopCandidate = Math.max(0, previousScrollPosition);

    for (let i = positions.length - 1; i >= 0; i--) {
      if (positions[i][1] < currentScroll && positions[i][2] > newTopCandidate) {
        return findNonTruncatedPosition(positions[i][1], viewportHeight);
      }
    }
    return findNonTruncatedPosition(newTopCandidate, viewportHeight);
  }

  function scrollShiftUp(currentScroll, viewportHeight) {
    const prevViewportTop = Math.max(currentScroll - viewportHeight, 0);
    let newScroll = prevViewportTop;

    for (let i = positions.length - 1; i >= 0; i--) {
      const imgTop = positions[i][1];

      if (imgTop >= prevViewportTop && imgTop <= prevViewportTop + 20) {
        newScroll = imgTop;
        break;
      }

      if (imgTop < prevViewportTop && imgTop >= prevViewportTop - 100) {
        newScroll = imgTop;
        break;
      }
    }

    return findNonTruncatedPosition(newScroll, viewportHeight);
  }

  function getYOffset(node) {
    let offset = 0;
    while (node) {
      offset += node.offsetTop;
      node = node.offsetParent;
    }
    return offset;
  }

  document.addEventListener("keydown", function(event) {
    if (event.ctrlKey || event.altKey) return;

    const tagName = event.target.tagName;
    const contentEditable = event.target.getAttribute("contenteditable");
    if ((tagName && tagName.match(/input|select|textarea/i)) || contentEditable === "true") {
      return;
    }

    positions = [];
    const allImages = document.images;
    for (let idx = 0; idx < allImages.length; idx++) {
      const img = allImages[idx];
      if (img.width * img.height < 80 * 80) continue;
      const ytop = getYOffset(img);
      const ybottom = ytop + img.height;
      positions.push([idx, ytop, ybottom]);
    }
    positions.sort((a, b) => a[1] - b[1]);

    const currentScroll = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
    const viewportHeight = window.innerHeight;

    let newScrollPosition = currentScroll;

    const key = event.key;
    const lowerKey = key.toLowerCase();

    if (lowerKey === KEY_S && !event.shiftKey) {
      newScrollPosition = scrollToNextImage(currentScroll, viewportHeight);
    } else if (lowerKey === KEY_W && !event.shiftKey) {
      newScrollPosition = scrollToPreviousImage(currentScroll, viewportHeight);
    } else if (lowerKey === KEY_W && event.shiftKey) {
      newScrollPosition = scrollShiftUp(currentScroll, viewportHeight);
    } else {
      return;
    }

    if (newScrollPosition !== currentScroll) {
      event.preventDefault();
      document.body.scrollTop = newScrollPosition;
      document.documentElement.scrollTop = newScrollPosition;
    }
  });
})();