2

when running this userscript, which simply finds and replaces certain letters and words, it doesn't replace the main texts, and only replaces text that is loaded after the webpage has been fully loaded. how can i make it replace all text on a webpage? i've tried all the different @run-at types but nothing has worked so far.

// ==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)
    })
})()
Senko
  • 21
  • 1
  • Several minor issues, and `DOMNodeInserted` is deprecated. But the main thing is you need to add `recurse(document.body);` to the end of the code. See also [here](https://stackoverflow.com/questions/24417791/replace-many-text-terms-using-tampermonkey-without-affecting-urls-and-not-look) and [here](https://stackoverflow.com/questions/24448837/how-to-replace-lots-of-words-in-ajax-driven-page-text-and-in-select-attributes) . Might add more later, if nobody beats me to it. – Brock Adams Sep 14 '19 at 03:31

0 Answers0