Stop Scrolling Instagram

Simple script to prevent scrolling beyond "You've completely caught up" element

当前为 2024-10-24 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Stop Scrolling Instagram
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Simple script to prevent scrolling beyond "You've completely caught up" element
// @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 to hide elements based on a selector
    function hideElement(selector) {
        const elements = document.querySelectorAll(selector);
        elements.forEach(element => {
            element.style.display = 'none';
        });
    }

    function hideSpecificElements() {
        // Mobile
        hideElement("div:nth-of-type(3) > .x1qrby5j.x7ja8zs.x1t2pt76.x1lytzrv.xedcshv.xarpa2k.x3igimt.x12ejxvf.xaigb6o.x1beo9mf.xv2umb2.x1jfb8zj.x1h9r5lt.x1h91t0o.x4k7w5x > .x1n2onr6 > ._a6hd.x1a2a7pz.xggy1nq.x1hl2dhg.x16tdsg8.xkhd6sd.x18d9i69.x4uap5.xexx8yu.x1mh8g0r.xat24cr.x11i5rnm.xdj266r.xe8uvvx.xt0psk2.x1ypdohk.x9f619.xm0m39n.x1qhh985.xcfux6l.x972fbf.x17r0tee.x1sy0etr.xd10rxx.x1ejq31n.xjbqb8w.x1i10hfl");
        hideElement("div.x1nhvcw1.x1oa3qoh.x1qjc9v5.xqjyukv.xdt5ytf.x2lah0s.x1c4vz4f.xryxfnj.x1plvlek.x1n2onr6.xo71vjh.x5pf9jr.x13lgxp2.x168nmei.x1lliih");
        hideElement(".xl56j7k.x1oa3qoh.x6s0dn4.xqjyukv.xdt5ytf.x2lah0s.x1c4vz4f.xryxfnj.x1plvlek.x1n2onr6.x1y1aw1k.xwib8y2.xo71vjh.x5pf9jr.x13lgxp2.x168");
        // Desktop
        hideElement(".xh8yej3.x1iyjqo2 > div:nth-of-type(3)");
        hideElement(".xh8yej3.x1iyjqo2 > div:nth-of-type(4)");
    }


    // Function to find "You've completely caught up" element
    function findSuggested() {
        const suggestedChild = document.querySelector('.xh8yej3.x1ye3gou.x1gan7if.xn6708d.x1miatn0.xdt5ytf.x78zum5.x9f619.xjbqb8w.x6s0dn4');

        if (suggestedChild) {
            const suggested = suggestedChild.parentElement;
            stopAtElement(suggested);
        } else {
            enableScroll();
        }
    }

    function disableScroll(suggested) {
        // Set the margin before disabling scroll
        suggested.style.marginBottom = '900px';

        window.onscroll = function () {
            const elementRect = suggested.getBoundingClientRect();
            if (elementRect.bottom < window.innerHeight) {
                const bodyRect = document.body.getBoundingClientRect();
                window.scrollTo(0, elementRect.bottom - bodyRect.bottom);
            }
        };
    }

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

    function stopAtElement(element) {
        // Disable scrolling when the element is reached
        disableScroll(element);
    }

    // Use MutationObserver to monitor DOM changes
    const observer = new MutationObserver(() => {
        findSuggested();
        hideSpecificElements(); // Call to hide elements on DOM changes
    });
    observer.observe(document.body, { childList: true, subtree: true });

    // Initial calls
    findSuggested();
    hideSpecificElements(); // Ensure elements are hidden on script load
})();