Lock Scroll to Bottom

Automatically keep the scroll locked to the bottom of the chat container on t3.chat

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Lock Scroll to Bottom
// @namespace    https://github.com/prudentbird
// @version      1.1
// @description  Automatically keep the scroll locked to the bottom of the chat container on t3.chat
// @author       Prudent Bird
// @match        https://t3.chat/*
// @match        https://beta.t3.chat/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=t3.chat
// @grant        none
// @license      MIT
// ==/UserScript==
(function() {
    'use strict';

    function waitForElement(selector, callback) {
        const el = document.querySelector(selector);
        if (el) {
            callback(el);
        } else {
            setTimeout(() => waitForElement(selector, callback), 500);
        }
    }

    waitForElement('div.absolute.inset-0.overflow-y-scroll', (el) => {
        const threshold = 50;
        let autoScrollEnabled = true;

        const scrollToBottom = () => {
            setTimeout(() => {
                el.scrollTop = el.scrollHeight;
            autoScrollEnabled = true;
        }, 0);
        };

        el.addEventListener('scroll', () => {
            const distanceToBottom = el.scrollHeight - el.clientHeight - el.scrollTop;
            autoScrollEnabled = distanceToBottom <= threshold;
        });

        scrollToBottom();

        const observer = new MutationObserver(() => {
            if (autoScrollEnabled) {
                scrollToBottom();
            }
        });

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