Code Block Wrapping Enhancer For ChatGPT

Replaces or adds the "!whitespace-pre-wrap" class to <code> elements.

  1. // ==UserScript==
  2. // @name Code Block Wrapping Enhancer For ChatGPT
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.3
  5. // @description Replaces or adds the "!whitespace-pre-wrap" class to <code> elements.
  6. // @match *://chatgpt.com/*
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. (function() {
  11. 'use strict';
  12.  
  13. // Function to process <code> elements
  14. function updateCodeClass() {
  15. document.querySelectorAll('code').forEach(code => {
  16. if (code.classList.contains('!whitespace-pre')) {
  17. // Replace "!whitespace-pre" with "!whitespace-pre-wrap"
  18. code.classList.replace('!whitespace-pre', '!whitespace-pre-wrap');
  19. } else {
  20. // Add "!whitespace-pre-wrap" if it doesn't exist
  21. code.classList.add('!whitespace-pre-wrap');
  22. }
  23. });
  24. }
  25.  
  26. // Create a MutationObserver to handle dynamically added elements
  27. const observer = new MutationObserver(() => updateCodeClass());
  28.  
  29. // Start observing the document for changes
  30. observer.observe(document.body, { childList: true, subtree: true });
  31.  
  32. // Initial execution
  33. updateCodeClass();
  34. })();