Next Image/Previous Image with Alignment and Large Image Support

Scroll down/up by aligning images

目前為 2025-01-23 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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;
    }
  });
})();