跳到正文

个人记录

文本打乱器(排列组合)

__luo2031__约 3 分钟
原文
文章信息与版本

文章信息

收录
2026年2月10日
更新
2026年2月10日
篇幅
约 3 分钟

版本

  1. v1当前
JavaScript
// ==UserScript==
// @name         文本打乱器
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  nyan
// @author       __luo2031__
// @match        *://*/*
// ==/UserScript==


(function() {
    'use strict';
    //感谢@fxkr
    function processNode(node) {
        if (node.nodeType === Node.TEXT_NODE && node.textContent.trim() !== "") {
            const originalText = node.textContent;
            var newText = '';
            for(var i=0;i<originalText.length;i++)newText+=originalText[Math.max(Math.floor(Math.random()*originalText.length-0.1),0)];
            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
    });
})();

放到油猴里运行~

原文