首页 > 解决方案 > 查找替换用户脚本不替换主网页文本

问题描述

运行此用户脚本时,它只是查找和替换某些字母和单词,它不会替换主要文本,而只会替换网页完全加载后加载的文本。我怎样才能让它替换网页上的所有文本?我已经尝试了所有不同的@run-at类型,但到目前为止没有任何效果。

// ==UserScript==
// @name         OwO Script
// @namespace
// @version      0.2
// @description
// @author       You
// @match        *://*/*
// @grant        none
// @run-at       document-end
// ==/UserScript==


(function () {
    let rules = [
        ['\\blove\\b', 'wuv'],
        ['\\bmr\\b', 'mister'],
        ['\\bbathroom\\b', 'potty'],
        ['\\bdog\\b', 'doggo'],
        ['\\bcat\\b', 'kitteh'],
        ['\\bhell\\b', 'heck'],
        ['\\bfuck|\\bfuk', 'frick'],
        ['\\bshit', 'shoot'],
        ['\\bfriend\\b', 'frend'],
        ['\\bstop\\b', 'stawp'],
        ['[rl]', 'w']
    ]
    let suffixes = [
        '(ノ´ з `)ノ',
        '( ´ ▽ ` ).。o♡',
        '(´,,•ω•,,)♡',
        ':3',
        '>:3',
        'OwO',
        'uwu',
        'hehe',
        'xox',
        '>3<',
        'murr~',
        'UwU',
        '-3-',
        'c:',
        '*nuzzles*',
        '*waises paw*',
        '*notices bulge*'
    ]
    function recurse (node) {
        if (node.nodeValue == null) {
            for (let child of node.childNodes) {
                recurse(child)
            }
        } else if (node.nodeType === 3) {
            let text = node.nodeValue
            for (let [find, replace] of rules) {
                text = text.replace(RegExp(find, 'g'), replace)
                text = text.replace(RegExp(find.toUpperCase().replace(/\\B/g, '\\b'), 'g'), replace.toUpperCase())
            }
            if (Math.random() < 0.2) {
                text += ` ${suffixes[Math.floor(Math.random() * suffixes.length)]}`
            }
            node.nodeValue = text
        }
    }
    document.body.addEventListener('DOMNodeInserted', event => {
        recurse(event.target)
    })
})()

标签: javascriptuserscripts

解决方案


推荐阅读