Scroll to Nearest Paragraph (← → keys)

Scroll to nearest paragraph using ← and → keys with 50px offset, starting from current scroll position if already scrolled down the page (no modifier keys required)

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

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Scroll to Nearest Paragraph (← → keys)
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  Scroll to nearest paragraph using ← and → keys with 50px offset, starting from current scroll position if already scrolled down the page (no modifier keys required)
// @author       Işık Barış Fidaner
// @match        *://zizekanalysis.wordpress.com/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    let paragraphs = [];
    let current = 0;
    let initialized = false;
    const offset = 50; // pixels above paragraph

    function init() {
        paragraphs = Array.from(document.querySelectorAll('p'))
            .filter(p => p.offsetHeight > 0 && p.offsetParent !== null);
        setInitialIndex();
        initialized = true;
    }

    function setInitialIndex() {
        const viewportTop = window.scrollY;
        for (let i = 0; i < paragraphs.length; i++) {
            const rect = paragraphs[i].getBoundingClientRect();
            const paragraphTop = window.scrollY + rect.top;
            if (paragraphTop - offset >= viewportTop) {
                current = i;
                return;
            }
        }
        current = paragraphs.length; // in case scrolled past all
    }

	function scrollToParagraph(index) {
		if (index >= 0 && index < paragraphs.length) {
			const rect = paragraphs[index].getBoundingClientRect();
			const scrollY = window.scrollY + rect.top - offset;

			window.scrollTo({
				top: Math.max(scrollY, 0),
				behavior: 'smooth'
			});

			current = index;
		}
	}

    // SCROLL EVENT: Manuel scroll, Ctrl+F ile arama, vb. her türlü scroll'u algılar.
    let scrollTimeout;
    window.addEventListener('scroll', () => {
        if (!initialized) init();
        clearTimeout(scrollTimeout);
        scrollTimeout = setTimeout(setInitialIndex, 100); // 100ms sonra güncelle
    });

	document.addEventListener('keydown', (e) => {
		// Ignore keypresses in input fields or editable areas
		const target = e.target;
		if (
			target.tagName === 'INPUT' ||
			target.tagName === 'TEXTAREA' ||
			target.isContentEditable
		) {
			return;
		}

		if (!initialized) init();

		if (e.key === 'ArrowRight') {
			e.preventDefault();
			if (current < paragraphs.length - 1) {
				scrollToParagraph(current + 1);
			}
		} else if (e.key === 'ArrowLeft') {
			e.preventDefault();
			if (current > 0) {
				scrollToParagraph(current - 1);
			}
		} else if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
			setTimeout(setInitialIndex, 0);
		}
	});
})();