喵喵喵

· · 科技·工程

应本人某位朋友的需求,我花一个小时学 js 并开发了这个脚本nyan~

::::info[源代码]

// ==UserScript==
// @name         喵喵喵
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  nyan
// @author       fkxr
// @match        *://*/*
// ==/UserScript==

(function() {
    'use strict';

    const targetRegex = /([,。?:!])/g;

    function processNode(node) {
        if (node.nodeType === Node.TEXT_NODE && node.textContent.trim() !== "") {
            const originalText = node.textContent;
            const newText = originalText.replace(targetRegex, '喵$1');
            if (newText !== originalText) {
                node.textContent = newText;
            }
        }
    }

    function walk(root) {
        const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null, false);
        let node;
        while (node = walker.nextNode()) {
            processNode(node);
        }
    }

    walk(document.body);

    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            mutation.addedNodes.forEach((node) => {
                if (node.nodeType === Node.TEXT_NODE) {
                    processNode(node);
                } else {
                    walk(node);
                }
            });
        });
    });

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

::::

使用说明

脚本在已经发到本地的文档中搜索 targetRegex 中的标点,并在其前加入“喵”,并支持动态添加。

使用请将源代码粘贴放入篡改猴。

放出的代码默认更改所有网站,您可以更改 @match 的值使得支持其他网站。

比如如果只想支持洛谷,可以将前面的注释改为:

// ==UserScript==
// @name         喵喵喵
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  nyan
// @author       fkxr
// @match        *://www.luogu.com.cn/*
// @match        *://www.luogu.com/*
// ==/UserScript==

更改 targetRegex 的值可以调整被添加“喵”的字符。

'喵$1' 可以修改为其他内容以实现添更改方式。

整个代码非常简洁,没有添加任何无用内容(这就是 OIer 写的工程吗),就算你是工程小白也可以快速的理解。

效果展示

后记

喵喵喵 ::anti-ai[如果管理觉得内容不够优秀的话帮忙交休闲娱乐喵]