ChatGPT DOM Cleanup

Cleans up the DOM of ChatGPT chats to the last 20 messages.

  1. // ==UserScript==
  2. // @name ChatGPT DOM Cleanup
  3. // @namespace ChatGPT Tools by Vishanka
  4. // @version 1.6
  5. // @description Cleans up the DOM of ChatGPT chats to the last 20 messages.
  6. // @author Vishanka
  7. // @license Proprietary
  8. // @match https://chatgpt.com/*
  9. // @grant none
  10. // @supportURL https://greasyfork.org/scripts/522100-chatgpt-dom-cleanup
  11. // ==/UserScript==
  12.  
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. // Configuration
  18. const MAX_VISIBLE_MESSAGES = 20; // Number of messages to keep in the DOM
  19.  
  20. // Function to clean up the conversation DOM
  21. function cleanUpMessages() {
  22. const messageElements = document.querySelectorAll('[data-testid^="conversation-turn"]');
  23. console.log(`Found ${messageElements.length} messages in the DOM.`);
  24.  
  25. // If there are more messages than allowed, remove the oldest ones
  26. if (messageElements.length > MAX_VISIBLE_MESSAGES) {
  27. const excessMessages = Array.from(messageElements).slice(0, messageElements.length - MAX_VISIBLE_MESSAGES);
  28. excessMessages.forEach(el => el.remove());
  29. console.log(`Removed ${excessMessages.length} old messages.`);
  30. } else {
  31. console.log('No messages need to be removed.');
  32. }
  33. }
  34.  
  35. // Function to manage cleanup intervals
  36. function manageIntervals() {
  37. const intervals = [
  38. { duration: 10000, interval: 2000 }, // First 10 seconds: 2-second interval
  39. { duration: 15000, interval: 3000 }, // Next 15 seconds: 3-second interval
  40. { duration: 10000, interval: 5000 }, // Next 10 seconds: 5-second interval
  41. ];
  42.  
  43. let elapsedTime = 0;
  44. let currentTimeout;
  45.  
  46. function scheduleNextInterval(index) {
  47. if (index < intervals.length) {
  48. const { duration, interval } = intervals[index];
  49. console.log(`Starting interval of ${interval}ms for ${duration}ms.`);
  50.  
  51. currentTimeout = setTimeout(() => {
  52. elapsedTime += duration;
  53. scheduleNextInterval(index + 1);
  54. }, duration);
  55.  
  56. const intervalId = setInterval(cleanUpMessages, interval);
  57. setTimeout(() => clearInterval(intervalId), duration);
  58. } else {
  59. console.log('Switching to final 30-second interval.');
  60. setInterval(cleanUpMessages, 30000); // Final 30-second interval
  61. }
  62. }
  63.  
  64. scheduleNextInterval(0);
  65. }
  66.  
  67. // Wait for the page to fully load
  68. window.addEventListener('load', () => {
  69. console.log('Page fully loaded. Starting initialization...');
  70. manageIntervals();
  71. });
  72.  
  73. })();