Is there a way to click a link only once using greasemonkey?
Is there a way to click a link only once using Greasemonkey? Script is (e.g.):
var evt = document.createEvent ("HTMLEvents");
evt.initEvent ("click", true, true);
doc开发者_如何学Goument.getElementById('DailyConsumtionTrigger').dispatchEvent(evt);
If the click is just time based (EG every 12 hours) then something like this will work:
var intervalInMillisecs = 12 * 3600 * 1000; //-- 12 hours
var currentDateTime = new Date ();
currentDateTime = currentDateTime.valueOf (); //-- Raw mS
//--- Get last click time, if it was set.
var lastClickTime = GM_getValue ('lastClickTime_DCT');
if (!lastClickTime)
lastClickTime = 0;
//--- If this is the first ever run or the interval has past, click the button.
if ( currentDateTime - lastClickTime > intervalInMillisecs )
{
GM_setValue ('lastClickTime_DCT', currentDateTime);
var evt = document.createEvent ("HTMLEvents");
evt.initEvent ("click", true, true);
document.getElementById ('DailyConsumtionTrigger').dispatchEvent (evt);
}
Note that GM_setValue stores information semi-permanently, per script.
If the click is state or event based (EG Only click when gauge X nears zero), then test for the appropriate condition instead.
IMPORTANT:
The above was based on the stated question of "clicking only once". It assumes that the user will be browsing the target (game) site frequently and doesn't want to over trigger the target button across page loads.
To trigger repeatedly, use setInterval();
精彩评论