喵喵喵

· · 休闲·娱乐

本插件为娱乐向喵,使用本插件可能导致使用时感受不良喵,实用性可能较小甚至为负喵。

介绍

本插件目前实现的功能就是将大部分标点前插入“喵”,以及文本末尾如果为文字则插入“喵”,并且当插入的位置前已经有了“喵”则不插入喵。

目前已支持识别犇犇内的每个人的话的结尾添加“喵”,但是有时未添加“喵”也属于正常现象喵。

欢迎大家改进代码喵!

代码

代码使用 DeepSeek V4.1 Flash 生成喵。

请安装篡改猴使用喵。

// ==UserScript==
// @name         喵化网页
// @namespace    https://tampermonkey.net/
// @version      1.3.0
// @description  给网页文本的标点前和结尾文字后加“喵”,标点包含普通标点、省略号,以及前面是汉字的空格;若前面已是“喵”则不重复加
// @author       You
// @match        *://*/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
    'use strict';

    const MIAO = '喵';

    // 这些标签里的文本不处理
    const SKIP_TAGS = new Set([
        'SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT',
        'OPTION', 'CODE', 'PRE', 'KBD', 'SAMP', 'SVG', 'MATH', 'IFRAME',
        'TITLE'
    ]);

    // 结尾是标点 / 符号 / 空白时,不算“文字”,不补喵
    // 注意:省略号属于 \p{P},空格属于 \s,所以会被涵盖
    const NON_WORD_END = /[\p{P}\p{S}\s0-9a-zA-Z]$/u;

    const processed = new WeakMap();
    let observer = null;

    function miaoify(text) {
        // 1. 普通标点前加喵(前面不是喵)
        let out = text.replace(/(?<![喵0-9a-zA-Z])([,。?!~”:])/g, '喵$1');

        // 2. 省略号前加喵(前面不是喵)
        //    中文省略号 …… (两个及以上 U+2026)
        //    英文省略号 ...  (三个及以上点)
        out = out.replace(/(?<![喵0-9a-zA-Z])(…{2,}|\.{3,})/g, '喵$1');

        // 3. 前面是汉字的空格前加喵
        //    使用 Unicode 属性匹配汉字,捕获空格
        out = out.replace(/(\p{Script=Han})((?<![喵0-9a-zA-Z])\s)([|])/gu, '$1喵 |');

        // 4. 结尾是文字(非标点/符号/空白)时补喵
        if (!out.endsWith(MIAO) && !NON_WORD_END.test(out)) {
            out += MIAO;
        }
        return out;
    }

    function shouldSkipElement(el) {
        if (!el || el.nodeType !== 1) return false;
        if (SKIP_TAGS.has(el.nodeName)) return true;
        if (el.isContentEditable) return true;
        return false;
    }

    function processTextNode(node) {
        if (!node || node.nodeType !== Node.TEXT_NODE) return;

        const parent = node.parentNode;
        if (!parent) return;
        if (parent.nodeType === Node.ELEMENT_NODE && shouldSkipElement(parent)) return;

        const text = node.nodeValue;
        if (!text || !text.trim()) return;
        if (processed.get(node) === text) return;

        const next = miaoify(text);
        if (next !== text) {
            processed.set(node, next);
            node.nodeValue = next;
        } else {
            processed.set(node, text);
        }
    }

    function processRoot(root) {
        if (!root) return;

        if (root.nodeType === Node.TEXT_NODE) {
            processTextNode(root);
            return;
        }

        if (
            root.nodeType !== Node.ELEMENT_NODE &&
            root.nodeType !== Node.DOCUMENT_NODE &&
            root.nodeType !== Node.DOCUMENT_FRAGMENT_NODE
        ) {
            return;
        }

        const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, {
            acceptNode(node) {
                const p = node.parentNode;
                if (!p) return NodeFilter.FILTER_REJECT;
                if (p.nodeType === Node.ELEMENT_NODE && shouldSkipElement(p)) {
                    return NodeFilter.FILTER_REJECT;
                }
                if (!node.nodeValue || !node.nodeValue.trim()) {
                    return NodeFilter.FILTER_REJECT;
                }
                return NodeFilter.FILTER_ACCEPT;
            }
        });

        const nodes = [];
        while (walker.nextNode()) nodes.push(walker.currentNode);
        for (const n of nodes) processTextNode(n);
    }

    function start() {
        if (!document.body) return;

        processRoot(document.body);

        observer = new MutationObserver((mutations) => {
            for (const m of mutations) {
                if (m.type === 'characterData') {
                    processTextNode(m.target);
                } else if (m.type === 'childList') {
                    m.addedNodes.forEach((node) => {
                        if (node.nodeType === Node.TEXT_NODE) {
                            processTextNode(node);
                        } else {
                            processRoot(node);
                        }
                    });
                }
            }
        });

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

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', start, { once: true });
    } else {
        start();
    }
})();