问题
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