为什么模拟事件没有反应
想写一个自动打字的脚本 www.monkeytype.com!(data/attachment/forum/202202/12/173124hs29903209gjw0s9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")
我想模拟键盘的输入,就是这个input事件
这是网站的源码
!(data/attachment/forum/202202/12/174225uzpn3pofiombokvu.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")
```javascript
//我在浏览器里执行了这些代码, 改了addEventListener的回调函数,想模拟一次input事件,
const oldAddEv = window.EventTarget.prototype.addEventListener;
window.EventTarget.prototype.addEventListener = function (topic, callback) {
const callbackOld = arguments;
arguments = function (e) {
console.log("callback execute")
return callbackOld.apply(this, e)
}
return oldAddEv.apply(this, arguments);
};
```
```javascript
let ipt = document.querySelector("#wordsInput")
let input = new Event("input")
ipt.dispatchEvent(input)
```
!(data/attachment/forum/202202/12/173738ifyxyge95wk6fwvk.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")
发现并没有打印“callback execute"
本人JS小白,菜的一批,希望大佬们帮我看看怎么回事
https://bbs.tampermonkey.net.cn/thread-1741-1-1.html 剩下的体力活
let f = function (c){
let wordsInput = document.querySelector("#wordsInput")
let func = $._data(wordsInput).events.input.handler
let inputEvent = {
originalEvent: new Proxy(new InputEvent({data: c}), {
get: function(target, property){
if( property == "isTrusted"){
return true;
}else{
return Reflect.get(target, property)
}
}
}),
target:{
value:{
normalize: function(){
return " "+ c
}
}
}
}
func(inputEvent)
}
arr = []
function getNextWord(letters){
let s = " "
letters.forEach((item1)=>{
s += item1.innerText
})
s += " "
arr.push(s)
}
(function getInitialWords(){
let words = document.querySelectorAll(".word")
words.forEach((item)=>{
let letters = item.querySelectorAll("letter")
getNextWord(letters)
})
})()
function final(count){
count = (1 / (count / 60.0)) * 1000
console.log(count)
i = 0
setInterval(function(){
f(arr)
if(i > 1){
words = document.querySelectorAll(".word")
let lastword = words
let letters = lastword.querySelectorAll("letter")
getNextWord(letters)
}
}, count)
}
//一分钟打多少个字
final(100) shardemacheal 发表于 2022-2-13 11:07
剩下的体力活
哥哥牛逼!
页:
[1]