“Normal” button-clicking approaches are not working in Greasemonkey script?

前端 未结 1 837
青春惊慌失措
青春惊慌失措 2020-12-19 22:09

Here is the button on the page:



        
相关标签:
1条回答
  • 2020-12-19 22:34

    Not every button works off a click event. Also, it's not clear if this is a statically loaded button, or if it's loaded by AJAX. (Link to the target page!)

    A general approach is given in Choosing and activating the right controls on an AJAX-driven site.

    Something like this complete script will work in 99% of the cases:

    // ==UserScript==
    // @name     _YOUR_SCRIPT_NAME
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/*
    // @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==
    /*- The @grant directive is needed to work around a design change
        introduced in GM 1.0.   It restores the sandbox.
    */
    
    waitForKeyElements ("#dispatchOnJobButton", triggerMostButtons);
    
    function triggerMostButtons (jNode) {
        triggerMouseEvent (jNode[0], "mouseover");
        triggerMouseEvent (jNode[0], "mousedown");
        triggerMouseEvent (jNode[0], "mouseup");
        triggerMouseEvent (jNode[0], "click");
    }
    
    function triggerMouseEvent (node, eventType) {
        var clickEvent = document.createEvent('MouseEvents');
        clickEvent.initEvent (eventType, true, true);
        node.dispatchEvent (clickEvent);
    }
    


    If it doesn't work for you, Link to the target page, or post an SSCCE!

    0 讨论(0)
提交回复
热议问题