中英混用加空格

自动在中英文之间添加空格

// ==UserScript==
// @name         中英混用加空格
// @namespace    https://greasyfork.org/users/1171320
// @version      1.0
// @description  自动在中英文之间添加空格
// @author       Lama AI 辅助
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    function addSpaceBetweenChineseAndEnglish(node) {
        if (node.nodeType === Node.TEXT_NODE) {
            // 使用正则表达式来匹配中英文
            node.textContent = node.textContent.replace(/([\u4e00-\u9fa5])([a-zA-Z0-9])/g, '$1 $2').replace(/([a-zA-Z0-9])([\u4e00-\u9fa5])/g, '$1 $2');
        } else if (node.nodeType === Node.ELEMENT_NODE) {
            // 递归处理子节点
            for (let child of node.childNodes) {
                addSpaceBetweenChineseAndEnglish(child);
            }
        }
    }

    // 监听页面加载完成
    window.addEventListener('load', function() {
        // 获取 body 元素
        const body = document.body;
        if (body) {
            addSpaceBetweenChineseAndEnglish(body);

            // 监听 DOM 变化,动态更新
            const observer = new MutationObserver(mutations => {
                mutations.forEach(mutation => {
                  if (mutation.type === 'childList') {
                    mutation.addedNodes.forEach(node => {
                      addSpaceBetweenChineseAndEnglish(node);
                    });
                  }
                });
            });

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