JS autoclick button after 4 sec. (Greasemonkey)
Hello how to make a开发者_如何学运维 simple code in JS to Greasemonkey which click button on page after 4 sec? I'm fully beginner in JS.
Here is code of this button ;):
<a class="doIt" href="#" onClick="func_x()"></a>
Thanks for help.
setTimeout(function() {
unsafeWindow.func_x(); // onclick event function
}, 4000);
or better yet
setTimeout(function() {
location.assign("javascript:func_x()"); // onclick event function
}, 4000);
setTimeout(function() {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0,
false, false, false, false,
0, null);
var link = document.querySelector("a.doIt");
link.dispatchEvent(evt);
}, 4000);
References:
- https://developer.mozilla.org/en/DOM/document.createEvent
- https://developer.mozilla.org/en/DOM/event.initMouseEvent
- https://developer.mozilla.org/En/DOM/Document.querySelector
- https://developer.mozilla.org/en/DOM/element.dispatchEvent
Live example:
- https://developer.mozilla.org/samples/domref/dispatchEvent.html
精彩评论