javascript - 查找替换用户脚本不替换主网页文本
问题描述
运行此用户脚本时,它只是查找和替换某些字母和单词,它不会替换主要文本,而只会替换网页完全加载后加载的文本。我怎样才能让它替换网页上的所有文本?我已经尝试了所有不同的@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)
})
})()
解决方案
推荐阅读
- javascript - 如何制作一个只为至少有 12 点的人发送邮件的邮件系统?
- node.js - 如何在 .goto(url) puppeteer 中使用多个链接?
- typescript - 如何更正错误 win 指的是一个值,但在这里被用作一个类型
- http - 发出 HEAD 请求是否仍需要 3 次握手?
- macos-high-sierra - 是否可以在 High Sierra (10.13.6) 上安装 Xcode 11?
- discord.js - 当我尝试运行不和谐机器人时,它说“未定义消息”
- c++ - C++ WinRT - 如何动态缩放 WriteableBitmap 以匹配当前屏幕分辨率?
- linux - “缺少重定向名称。” 在 CSH 环境中运行命令时出错
- mongodb - 什么是 mongodb 归档格式?
- dart - Dart 使用 getter 方法从类中获取私有字段值