在Tampermonkey中模拟mousedown,click,mouseup序列? [英] Simulating a mousedown, click, mouseup sequence in Tampermonkey?

查看:638
本文介绍了在Tampermonkey中模拟mousedown,click,mouseup序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想模拟整个点击而不仅仅是

I would like to simulate a whole click not just

document.getElementsByClassName()[0].click();

我该怎么做?搜索结果似乎都是关于处理此类事件,而不是触发它们。

How do I do that? Search results all seem to be about handling such events, not triggering them.

推荐答案

发送鼠标事件。像这样:

Send mouse events. Like so:

//--- Get the first link that has "stackoverflow" in its URL.
var targetNode = document.querySelector ("a[href*='stackoverflow']");
if (targetNode) {
    //--- Simulate a natural mouse-click sequence.
    triggerMouseEvent (targetNode, "mouseover");
    triggerMouseEvent (targetNode, "mousedown");
    triggerMouseEvent (targetNode, "mouseup");
    triggerMouseEvent (targetNode, "click");
}
else
    console.log ("*** Target node not found!");

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}






如果网络有效页面已静态加载。如果网页是AJAX驱动的,请使用以下技术:

  • "Normal" button-clicking approaches are not working in Greasemonkey script?
  • Choosing and activating the right controls on an AJAX-driven site

注意:

问题代码有错误。您需要将类名传递给该函数。像这样:

Beware:
The question code has an error. You need to pass a class name to that function. Like so:

document.getElementsByClassName ("SomeClassName")[0].click();

这篇关于在Tampermonkey中模拟mousedown,click,mouseup序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆