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.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
};
})();