ChatGPT Remove Special Characters on Copy

Auto replace special characters '[', ']', '(', ')' when copying text in ChatGPT

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         ChatGPT Remove Special Characters on Copy
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Auto replace special characters '[', ']', '(', ')' when copying text in ChatGPT
// @author       eternal-echo
// @match        https://chatgpt.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Wait until the page fully loads
    window.addEventListener('load', function() {

        // Function to replace special characters in the text
        function replaceSpecialCharacters(text) {
            // Replace the escaped math characters with desired symbols
            return text
                .replace(/\\\[/g, '$$$$')  // Replace \[ with $$
                .replace(/\\\]/g, '$$$$')  // Replace \] with $$
                .replace(/\\\(/g, '$')     // Replace \( with $
                .replace(/\\\)/g, '$');    // Replace \) with $
        }

        // Listen for any clicks on buttons with the 'data-testid="copy-turn-action-button"' attribute
        document.body.addEventListener('click', function(e) {
            if (e.target.closest('[data-testid="copy-turn-action-button"]')) {
                // Wait for the text to be copied to the clipboard
                setTimeout(() => {
                    navigator.clipboard.readText().then((text) => {
                        let modifiedText = replaceSpecialCharacters(text);
                        // Write the modified text back to the clipboard
                        navigator.clipboard.writeText(modifiedText);
                    });
                }, 100); // Delay to ensure the text is copied before we modify it
            }
        });
    });
})();