Discussions » Development

Why this doesn't work?

§
Posted: 2020-05-09
Edited: 2020-05-09

Why this doesn't work?

Hi, I'm trying to do a script to navigate into a page, execute a function going to especific page and then click a button, I cannot make it work, I tried to use async functions to wait one finishes the other. Any clue? Thanks!

// ==UserScript==
// @name Golden-Farm AutoCollectEggs
// @namespace
// @description Auto collect eggs
// @author DownloadAndDroid
// @include https://golden-farm.biz/account*
// @run-at document-end
// @version 0.0.4
// ==/UserScript==
var maxWait = 10000;
var minWait = 4000;

(async function() {
    'use strict';
    await collect_eggs();
    await sell_eggs();
    return;
});

async function collect_eggs() {
 'use strict';
    await daily_bonus();
            setTimeout(function(){
             window.location.href = 'https://golden-farm.biz/account/store';
        }, getRandomWait());
 document.getElementsByName("sbor")[0].click();
 console.log('Eggs collected');
 return;
}
wOxxOmMod
§
Posted: 2020-05-09

Changing location will reload the page and terminate all scripts including yours.

Basically you need to combine two scripts: one will redirect, the other one will do something in the redirected page.

if (location.pathname.startsWith('/account/store')) {
  document.getElementsByName("sbor")[0].click();
  console.log('Eggs collected');
} else {
  location.href = 'https://golden-farm.biz/account/store';
}

This is a simplified demo so adapt it to your needs accordingly.

§
Posted: 2020-05-09

Hi wOxxOm, Thanks for helping a newbie in scripts! And for the fast answer! Will try it!

Post reply

Sign in to post a reply.