ChatGPT DOM Cleanup (Forceful Querying)

Forcefully query and clean up ChatGPT DOM, keeping only the last 20 messages.

目前为 2024-12-28 提交的版本。查看 最新版本

// ==UserScript==
// @name         ChatGPT DOM Cleanup (Forceful Querying)
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Forcefully query and clean up ChatGPT DOM, keeping only the last 20 messages.
// @author       YourName
// @match        https://chatgpt.com/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // Configuration
    const MAX_VISIBLE_MESSAGES = 20; // Number of messages to keep in the DOM
    const CLEANUP_INTERVAL = 2000; // Interval for cleanup in milliseconds

    // 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.');
        }
    }

    // Repeated cleanup process
    function startRepeatedCleanup() {
        console.log('Starting repeated cleanup process...');
        setInterval(() => {
            cleanUpMessages(); // Forcefully query and clean up messages
        }, CLEANUP_INTERVAL);
    }

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址