Greasemonkey script Auto click javascript button (ajax?) [duplicate]

微笑、不失礼 提交于 2019-12-23 04:25:19

问题


Ok Here is the source of the page.

<div id="socialBox"></div>
<div class="friendButton addFriend">
    <a>+friend</a>
</div>

Here is my greasemonkey code

// ==UserScript==
// @name        Auto click
// @namespace   Auto click
// @description Auto click
// @include     https://*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant       GM_addStyle
// ==/UserScript==

waitForKeyElements ("#friendButton addFriend", triggerMostButtons);

function triggerMostButtons (jNode) {
    triggerMouseEvent (jNode[0], "mouseover");
    triggerMouseEvent (jNode[0], "mousedown");
    triggerMouseEvent (jNode[0], "click");
    triggerMouseEvent (jNode[0], "mouseup");
}


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

greasemonkey says its executing the code so it clearly doesn't work. Let me know if you need more info


回答1:


I wonder if the code below is what you want.

document.querySelector('.friendButton.addFriend a').click()

Demo

You'd better provide a link to one of the pages on which you want your script executed, so that I can make sure the script will work as expected.



来源:https://stackoverflow.com/questions/22703564/greasemonkey-script-auto-click-javascript-button-ajax

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