Stop Scrolling Instagram

Simple script to prevent scrolling beyond "You've completely caught up" element and hide suggested posts reliably.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Stop Scrolling Instagram
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Simple script to prevent scrolling beyond "You've completely caught up" element and hide suggested posts reliably.
// @author       Ulysses
// @match        https://www.instagram.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=instagram.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    function hideElement(selector) {
        const elements = document.querySelectorAll(selector);
        elements.forEach(el => el.style.display = 'none');
    }

    function hideSpecificElements() {
        // Mobile selectors
        hideElement("div:nth-of-type(3) > .x1qrby5j.x7ja8zs ..."); // Keep your original selectors
        hideElement("div.x1nhvcw1.x1oa3qoh.x1qjc9v5...");
        hideElement(".xl56j7k.x1oa3qoh.x6s0dn4...");
        // Desktop selectors
        hideElement("div:nth-of-type(4) > .x1qrby5j.x7ja8zs ...");
        hideElement("div:nth-of-type(4) > .x1qrby5j.x7ja8zs ... > .x1n2onr6");
    }

    // --- Updated method: Find "You've completely caught up" by text ---
    function findCaughtUpElement() {
        const textMatch = "You've completely caught up";
        const spans = document.querySelectorAll('span');
        for (const span of spans) {
            if (span.textContent.trim().includes(textMatch)) {
                return span.closest('div.xvbhtw8') || span.parentElement;
            }
        }
        return null;
    }

    function disableScroll(target) {
        target.style.marginBottom = '900px';

        window.onscroll = function () {
            const rect = target.getBoundingClientRect();
            if (rect.bottom < window.innerHeight) {
                const scrollTop = window.scrollY;
                const limit = rect.bottom + scrollTop - window.innerHeight;
                window.scrollTo(0, limit);
            }
        };
    }

    function enableScroll() {
        window.onscroll = function () {};
    }

    function stopAtCaughtUp() {
        const element = findCaughtUpElement();
        if (element) {
            disableScroll(element);
        } else {
            enableScroll();
        }
    }

    // Observe DOM changes for dynamic loading
    const observer = new MutationObserver(() => {
        stopAtCaughtUp();
        hideSpecificElements();
    });

    observer.observe(document.body, { childList: true, subtree: true });

    // Initial run
    stopAtCaughtUp();
    hideSpecificElements();
})();