Remove Em Dashes from span and div (Smart Focus)

Recursively remove em dashes and their escape forms from spans and divs. Watches DOM, cleans on page load, and runs every 2 seconds — but only when window is focused. Efficiency, baby.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Remove Em Dashes from span and div (Smart Focus)
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Recursively remove em dashes and their escape forms from spans and divs. Watches DOM, cleans on page load, and runs every 2 seconds — but only when window is focused. Efficiency, baby.
// @author       Shikaku (same username on Discord)
// @match        https://chatgpt.com/*
// @grant        none
// @license      BSD-4-Clause
// ==/UserScript==
//note: replace  https://chatgpt.com/* with *://*/* if you want it to work with every site

(function() {
    'use strict';

    const emDashRegex = /—|—|—|—|\u2014/g;

    function cleanTextNodes(node) {
        if (!node) return;

        if (node.nodeType === Node.TEXT_NODE && emDashRegex.test(node.textContent)) {
            node.textContent = node.textContent.replace(emDashRegex, '-'); //replace the '-' with whatever you want, like for example ', ' or even ' ' or ''
        }

        node.childNodes?.forEach(cleanTextNodes);
    }

    function cleanAll() {
        if (!document.hasFocus()) return; // Skip if tab is not active
        document.querySelectorAll('div, span').forEach(cleanTextNodes);
    }

    // Initial pass
    cleanAll();

    // Also run once on full load
    window.addEventListener('load', cleanAll);

    // Interval check (only if focused)
    setInterval(cleanAll, 2000);

    // DOM mutation observer
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === Node.ELEMENT_NODE) {
                    if (node.matches('div, span')) cleanTextNodes(node);
                    node.querySelectorAll?.('div, span').forEach(cleanTextNodes);
                }
            });

            if (mutation.type === 'characterData') {
                cleanTextNodes(mutation.target);
            }
        });
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true,
        characterData: true
    });

    // Patch clipboard writes to replace em dashes
    const originalWriteText = navigator.clipboard.writeText.bind(navigator.clipboard);

    navigator.clipboard.writeText = function(text) {
    const cleaned = text.replace(/—|—|—|—|\u2014/g, '-');
    return originalWriteText(cleaned);
};
})();