ChatGPT DOM Cleanup (Refined Intervals)

Forcefully query and clean up ChatGPT DOM with refined intervals.

目前為 2024-12-28 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         ChatGPT DOM Cleanup (Refined Intervals)
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  Forcefully query and clean up ChatGPT DOM with refined intervals.
// @author       Vishanka
// @match        https://chatgpt.com/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // Configuration
    const MAX_VISIBLE_MESSAGES = 20; // Number of messages to keep in the DOM

    // Function to clean up the conversation DOM
    function cleanUpMessages() {
        const messageElements = document.querySelectorAll('[data-testid^="conversation-turn"]');
        console.log(`Found ${messageElements.length} messages in the DOM.`);

        // If there are more messages than allowed, remove the oldest ones
        if (messageElements.length > MAX_VISIBLE_MESSAGES) {
            const excessMessages = Array.from(messageElements).slice(0, messageElements.length - MAX_VISIBLE_MESSAGES);
            excessMessages.forEach(el => el.remove());
            console.log(`Removed ${excessMessages.length} old messages.`);
        } else {
            console.log('No messages need to be removed.');
        }
    }

    // Function to manage cleanup intervals
    function manageIntervals() {
        const intervals = [
            { duration: 10000, interval: 2000 }, // First 10 seconds: 2-second interval
            { duration: 15000, interval: 3000 }, // Next 15 seconds: 3-second interval
            { duration: 10000, interval: 5000 }, // Next 10 seconds: 5-second interval
        ];

        let elapsedTime = 0;
        let currentTimeout;

        function scheduleNextInterval(index) {
            if (index < intervals.length) {
                const { duration, interval } = intervals[index];
                console.log(`Starting interval of ${interval}ms for ${duration}ms.`);

                currentTimeout = setTimeout(() => {
                    elapsedTime += duration;
                    scheduleNextInterval(index + 1);
                }, duration);

                const intervalId = setInterval(cleanUpMessages, interval);
                setTimeout(() => clearInterval(intervalId), duration);
            } else {
                console.log('Switching to final 30-second interval.');
                setInterval(cleanUpMessages, 30000); // Final 30-second interval
            }
        }

        scheduleNextInterval(0);
    }

    // Wait for the page to fully load
    window.addEventListener('load', () => {
        console.log('Page fully loaded. Starting initialization...');
        manageIntervals();
    });

})();