XPath not working to click a button from a Greasemonkey script

后端 未结 1 668
臣服心动
臣服心动 2021-01-16 08:43

I would like to open a link that contains the word google. It looks like this:

 

        
相关标签:
1条回答
  • 2021-01-16 09:08

    You say the XPath works, but elm.href is undefined in the GM script. This suggests that the <input> is added via AJAX.

    Your script needs to use AJAX-aware techniques. Something like:

    // ==UserScript==
    // @name     _Clicking "Open" buttons
    // @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 ("input.submit[onclick*='open']", clickOpenBtn);
    
    function clickOpenBtn (jNode) {
        triggerMouseEvent (jNode[0], "click");
    }
    
    function triggerMouseEvent (node, eventType) {
        var clickEvent = document.createEvent ('MouseEvents');
        clickEvent.initEvent (eventType, true, true);
        node.dispatchEvent (clickEvent);
    }
    
    0 讨论(0)
提交回复
热议问题