PDA

View Full Version : [USERSCRIPT] How do I add randomised delays between actions? (safe guard)



Kaine
03-14-2019, 04:30 AM
Firstly I'm not a coder at all, just trying to solve a simple problem to get this script running the way I want :)

I noticed most scripts have the option to configure a MIN & MAX delay time, to emulate human inconsistency

I.e.

var DelayMax = 5000;
var DelayMin = 3000;

And this is used to delay a certain action by a randomized amount between those two times.


Looking at ODD's scripts, they seem to incorporate this delay like so:


timeoutID = setTimeout
(

function () { location.href = "Some link herel"; },
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);


My question is, how would I go about including this function in the script posted below?
Is it a case of including the above function at each point where a location.href is at?

[Only registered and activated users can see links]
(Script cred = Bruno)

(+rep for your time & assistance, if I'm allowed to offer it!)

Bat
03-14-2019, 08:09 AM
Hey, Kaine! You're on the right track. In order to implement delays into your scripts, you'll need to visit each section that looks like:


anchor.click();

form.submit();

location.href = "...";

And replace those with:


setTimeout
(

function () { anchor.click(); },
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);

setTimeout
(

function () { form.submit(); },
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);

setTimeout
(

function () { location.href = "..."; },
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);

Here's a modified version of the script you mentioned. I've wrapped each page navigation with a randomized delay that can be adjusted with the variables at the top of the script:


// ==UserScript==
// name NeoQuest II Trainer
// namespace [Only registered and activated users can see links]
// Description Automatically battles and moves left and right for you.
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
[Only registered and activated users can see links]
// ==/UserScript==

var DelayMax = 5000;
var DelayMin = 3000;

//script licensed under, GNU GPL V3 , see [Only registered and activated users can see links] for details
//uncomment these lines to initialize variables

//2019-03-04 Modified by Odd - added randomized time delays to each page navigation routine in order for the script to appear more human while playing

(function () {

//Check for annoying server hiccup before bothering to make any more variables
var i = 0;
var hiccup = 1;
var divs = document.getElementsByTagName('div');

for (i = 0; i < divs.length; i++) {

if (divs[i].className == "contentModuleHeader") {

hiccup = 0;
}
}

if (hiccup) {

setTimeout
(

function () { location.href = "[Only registered and activated users can see links]"; },
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);

return;
}
else {

//begin real battling!!
var useid = -1; // use special item??
var nxactor = 1; // who fightsS??' default =1: rohane
var fact = 3; // default is attack , will override for low health
var hitTarget = GM_getValue("hitTarget", 5); //hittargets 1-4 are reserved for allies
var healingItem = GM_getValue("healingItem", 30011); // get the healing item in case HP turns red or yellow
var isHasted = GM_getValue("isHasted", false);
var j = 0; // used for looping to find out whose turn is it
var elements = document.getElementsByTagName('img');

for (i = 0; i < elements.length; i++) {

switch (elements[i].src) {

case "[Only registered and activated users can see links]":
{

GM_setValue("hitTarget", 5);
GM_setValue("isHasted", false);

setTimeout
(

function () { location.href = "[Only registered and activated users can see links]"; },
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);

return;
}
case "[Only registered and activated users can see links]":
{

var texts = document.getElementsByTagName("font");
var doMultipleTargets = 0;

for (j = 0; j < texts.length; j++) {

//check to increment target
if (((texts[j].innerHTML.search(/for it has already been defeated!/)) != -1) || (texts[j].innerHTML == "You must select a valid target to cast on!<BR>")) {

hitTarget++;

if (hitTarget >= 9) {

GM_setValue("hitTarget", 5);
}
else {

GM_setValue("hitTarget", hitTarget);
}
}

//check character's status
switch (texts[j].innerHTML) {

case "<b>Rohane</b>":
{

if ((texts[j + 1].color == "#d0d000") || (texts[j + 1].color == "red")) {

fact = 5;
useid = healingItem;
}

break;
}
case "<b>Mipsy</b>":
{

nxactor = 2;
fact = 9201; //use direct damage

if (!isHasted) {

fact = 9203;

GM_setValue("isHasted", true);
}

if ((texts[j + 1].color == "#d0d000") || (texts[j + 1].color == "red")) {

fact = 5;
useid = healingItem;
}

break;
}
case "<b>Talinia</b>":
{

var multipleTargets = /Multiple Targets/;
var k = 0;
var links = document.getElementsByTagName('a');

for (k = 0; k < links.length; k++) {

if ((links[k].innerHTML.search(multipleTargets)) != -1) {

fact = 9302;
}
}

nxactor = 3;

if ((texts[j + 1].color == "#d0d000") || (texts[j + 1].color == "red")) {

fact = 5;
useid = healingItem;
}

break;
}
case "<b>Velm</b>":
{

var l = 0; // loops to see if velm is wasting his time healing
var fullhp = 0; //if its 4 then all 4 people are fully healed
var allies = false;

//loop through all pictures if it's velm's turn
for (l = 0; l < elements.length; l++) {

//makes sure the script isn't checking enemies hp
if (elements[l].src == "[Only registered and activated users can see links]") {

allies = true;
}

//if checking allies HP
if (allies) {

//is the picture a health bar?
if (elements[l].src == "[Only registered and activated users can see links]") {

if (elements[l].width == 45) { //45 is full health

fullhp++;
}
}
}
}

nxactor = 4;
fact = 9402; // velm heals, trust me you will need this

if (fullhp == 4) {

fact = GM_getValue("VelmAction", 9403);
}

if ((texts[j + 1].color == "#d0d000") || (texts[j + 1].color == "red")) {

fact = 5;
useid = healingItem;
}

break;
}
}
}

setTimeout
(

function () { location.href = ("[Only registered and activated users can see links]" + fact + "&target=" + hitTarget + "&use_id=" + useid + "&nxactor=" + nxactor); },
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);

return;
}
case "[Only registered and activated users can see links]":
{

setTimeout
(

function () {

unsafeWindow.setaction(1);

unsafeWindow.document.ff.submit();
},
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);

return;
}
case "[Only registered and activated users can see links]":
{

setTimeout
(

function () {

unsafeWindow.setaction(2);

unsafeWindow.document.ff.submit();
},
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);

return;
}
case "[Only registered and activated users can see links]":
{

GM_setValue("hitTarget", 5);

GM_setValue("isHasted", false);

setTimeout
(

function () { location.href = "[Only registered and activated users can see links]"; },
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);

return;
}
case "[Only registered and activated users can see links]":
{

if (GM_getValue("goLeft")) {

GM_setValue("goLeft", false);

setTimeout
(

function () { unsafeWindow.dosub(3); },
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);
}
else {

GM_setValue("goLeft", true);

setTimeout
(

function () { unsafeWindow.dosub(4); },
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);
}

return;
}
}
}
}

setTimeout
(

function () { location.href = "[Only registered and activated users can see links]"; },
(Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
);
})();

I also made adjustments to the script's life cycle, since everything that it does could previously be cancelled with a navigation to the NeoQuest II home page if the script didn't finish executing in 10 seconds.

Kaine
04-01-2019, 10:12 PM
Thanks ODD!

This is perfect! thanks so much for not only detailing how you added the delays, but also adding them into the script I was looking to incorporate them into

+Rep

Cheers