XPath not working to click a button from a Greasemonkey script

有些话、适合烂在心里 提交于 2019-12-01 13:26:22
Brock Adams

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!