Results 1 to 3 of 3

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

  1. #1

    Joined
    Jul 2014
    Posts
    25
    Userbars
    0
    Thanks
    43
    Thanked
    26/8
    DL/UL
    1/0
    Mentioned
    4 times
    Time Online
    1d 21h 12m
    Avg. Time Online
    N/A

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

    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:

    Code:
                 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?

    (you need an account to see links)
    (Script cred = Bruno)

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

  2. The Following User Says Thank You to Kaine For This Useful Post:

    Woodpecker (03-20-2019)

  3. #2
    *squeak*
    Bat's Avatar
    Joined
    Nov 2012
    Posts
    4,040
    Userbars
    152
    Thanks
    2,147
    Thanked
    46,684/3,563
    DL/UL
    34/1
    Mentioned
    1,769 times
    Time Online
    644d 1h 41m
    Avg. Time Online
    3h 41m
    Hey, @(you need an account to see links)! You're on the right track. In order to implement delays into your scripts, you'll need to visit each section that looks like:

    Code:
    anchor.click();
    Code:
    form.submit();
    Code:
    location.href = "...";
    And replace those with:

    Code:
    setTimeout
    (
    
        function () { anchor.click(); },
        (Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
    );
    Code:
    setTimeout
    (
    
        function () { form.submit(); },
        (Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
    );
    Code:
    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:

    Code:
    // ==UserScript==
    // @(you need an account to see links)           NeoQuest II Trainer
    // @(you need an account to see links)space      (you need an account to see links)
    // @(you need an account to see links)cription    Automatically battles and moves left and right for you.
    // @grant          GM_log
    // @grant          GM_addStyle
    // @grant          GM_getValue
    // @grant          GM_setValue
    // @grant          GM_openInTab
    // @grant          GM_deleteValue
    // @grant          GM_xmlhttpRequest
    // @grant          GM_getResourceText
    // @include        (you need an account to see links)
    // @version 0.0.1.3
    // ==/UserScript==
    
    var DelayMax = 5000;
    var DelayMin = 3000;
    
    //script licensed under, GNU GPL V3 , see (you need an account to 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 = "http://www.neopets.com/games/nq2/nq2.phtml"; },
                (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 "http://images.neopets.com/nq2/x/com_begin.gif":
                        {
    
                            GM_setValue("hitTarget", 5);
                            GM_setValue("isHasted", false);
    
                            setTimeout
                            (
    
                                function () { location.href = "http://www.neopets.com/games/nq2/nq2.phtml?start=1"; },
                                (Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
                            );
    
                            return;
                        }
                    case "http://images.neopets.com/nq2/x/com_atk.gif":
                        {
    
                            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 == "http://images.neopets.com/nq2/x/donothing.gif") {
    
                                                    allies = true;
                                                }
    
                                                //if checking allies HP
                                                if (allies) {
    
                                                    //is the picture a health bar?
                                                    if (elements[l].src == "http://images.neopets.com/nq2/x/exp_green.gif") {
    
                                                        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 = ("http://www.neopets.com/games/nq2/nq2.phtml?&fact=" + fact + "&target=" + hitTarget + "&use_id=" + useid + "&nxactor=" + nxactor); },
                                (Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
                            );
    
                            return;
                        }
                    case "http://images.neopets.com/nq2/x/com_next.gif":
                        {
    
                            setTimeout
                            (
    
                                function () {
    
                                    unsafeWindow.setaction(1);
    
                                    unsafeWindow.document.ff.submit();
                                },
                                (Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
                            );
    
                            return;
                        }
                    case "http://images.neopets.com/nq2/x/com_end.gif":
                        {
    
                            setTimeout
                            (
    
                                function () {
    
                                    unsafeWindow.setaction(2);
    
                                    unsafeWindow.document.ff.submit();
                                },
                                (Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
                            );
    
                            return;
                        }
                    case "http://images.neopets.com/nq2/x/tomap.gif":
                        {
    
                            GM_setValue("hitTarget", 5);
    
                            GM_setValue("isHasted", false);
    
                            setTimeout
                            (
    
                                function () { location.href = "http://www.neopets.com/games/nq2/nq2.phtml?finish=1"; },
                                (Math.round(Math.random() * (DelayMax - DelayMin)) + DelayMin)
                            );
    
                            return;
                        }
                    case "http://images.neopets.com/nq2/x/nav.gif":
                        {
    
                            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 = "http://www.neopets.com/games/nq2/nq2.phtml"; },
            (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.

  4. The Following 2 Users Say Thank You to Bat For This Useful Post:

    Kaine (04-01-2019),Woodpecker (03-20-2019)

  5. #3

    Joined
    Jul 2014
    Posts
    25
    Userbars
    0
    Thanks
    43
    Thanked
    26/8
    DL/UL
    1/0
    Mentioned
    4 times
    Time Online
    1d 21h 12m
    Avg. Time Online
    N/A
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •