在Greasemonkey/Tampermonkey脚本中,单击按钮不起作用.已经使用waitForKeyElements()尝试过标准鼠标事件 [英] Button click doesn't work in Greasemonkey/Tampermonkey script. Already tried standard mouse events with waitForKeyElements()

查看:268
本文介绍了在Greasemonkey/Tampermonkey脚本中,单击按钮不起作用.已经使用waitForKeyElements()尝试过标准鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试点击URL http://www上的满载"按钮.sparhandy.de/handy-kaufen/与Tampermonkey.该按钮位于Smartphone图片的下方.这是我到目前为止编写的脚本:

I'm trying to click the "Mehr Laden" Button on the URL http://www.sparhandy.de/handy-kaufen/ with Tampermonkey. The Button is located underneath the Smartphone pictures. Here is the script that I wrote so far:

// ==UserScript==
// @name         SparhandyScript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       Nigel
// @match        http://www.sparhandy.de/handy-kaufen/
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==

waitForKeyElements ("[data\\-behavior=result\\-paging]", triggerMostButtons);

function triggerMostButtons (jNode) {
    triggerMouseEvent (jNode[0], "mouseover");
    triggerMouseEvent (jNode[0], "mousedown");
    triggerMouseEvent (jNode[0], "mouseup");
    triggerMouseEvent (jNode[0], "click");
    //alert(jNode[0].className);
    //alert(jNode[0].parentNode.className);
}

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

推荐答案

在运行该按钮的jQuery准备触发之前,该按钮已加载.因此,在这种情况下仅等待按钮是不够的.您还必须等待附加的JS初始化.

That button is loaded before the jQuery that runs it is ready to fire. So it is not enough to wait for the button in this case. You must also wait for the attached JS to initialize.

一个快速而肮脏的测试表明,对我而言,该按钮直到页面加载后一秒钟才准备就绪! (实际时间可能会有所不同,我没有尝试找到合适的状态标记(您可以这样做;).)

A quick and dirty test shows that, for me, the button is not ready until over a second after page load! (Actual time may vary and I did not attempt to find a suitable state marker (that's for you to do ;). )

因此,调整代码以允许按钮初始化对我而言有效:

So, adjusting the code to allow for the button to initialize, works for me:

// ==UserScript==
// @name        SparhandyScript
// @match       http://www.sparhandy.de/handy-kaufen/
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant       GM_addStyle
// ==/UserScript==

waitForKeyElements ("[data\\-behavior=result\\-paging]", clickButtonAfterDelay);

function clickButtonAfterDelay (jNode) {
    if (document.readyState != "complete")  return true;
    //triggerMouseEvent (jNode[0], "click");
    //-- WARNING: Fixed time may not always work unless it is ridiculously long.
    setTimeout (triggerMouseEvent, 2222, jNode[0], "click");
}

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



注意:此代码将获取结果的第一页,然后停止.它不会获取所有可能的页面.您需要一个更高级的状态机.
这超出了这个问题的范围.如果您需要有关该部分的帮助,请打开一个新问题(经过诚实的尝试自己解决之后).



NOTE: This code will fetch the first additional page of results and then stop. It will not fetch all possible pages; you need a fancier state machine for that.
It's beyond the scope of this question. If you need help with that part, open a new question (after an honest attempt to solve it yourself).

这篇关于在Greasemonkey/Tampermonkey脚本中,单击按钮不起作用.已经使用waitForKeyElements()尝试过标准鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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