跳到正文

个人记录

喵喵喵

Slimu约 9 分钟
原文

这是 2026年9月29日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年9月29日
收录
2026年9月29日
更新
2026年11月14日
篇幅
约 9 分钟
获赞
1

版本

  1. v7当前
  2. v6
  3. v5
  4. v4
更早的 3 个版本
  1. v3
  2. v2
  3. v1

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

介绍

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

目前暂时不支持识别犇犇内的每个人的话的结尾添加“喵”,因此有时未添加“喵”属于正常现象。

代码

请安装篡改猴使用。

JavaScript
// ==UserScript==
// @name         喵化网页
// @namespace    https://tampermonkey.net/
// @version      1.1.0
// @description  给网页文本的标点前和结尾文字后加“喵”,若前面已是“喵”则不重复加
// @author       You
// @match        *://*/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
    'use strict';

    const PUNCT_CHARS = ',。?!~“';
    const MIAO = '喵';

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

    // 结尾是标点 / 符号 / 空白时,不算“文字”,不补喵
    const NON_WORD_END = /[\p{P}\p{S}\s]$/u;

    // 标点前一个字符不是“喵”才匹配(负向后顾)
    // 这样已加过喵的标点不会被重复处理,也天然幂等
    const PUNCT_RE = new RegExp(
        '(?<!' + MIAO + ')([' + PUNCT_CHARS + '])',
        'g'
    );

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

    function miaoify(text) {
        // 1. 标点前加喵(前一个字符已是喵则跳过)
        let out = text.replace(PUNCT_RE, MIAO + '$1');

        // 2. 结尾是文字且末尾不是喵时补一个喵
        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();
    }
})();