FDWorlds work bot

try to take over the world!

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         FDWorlds work bot
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  try to take over the world!
// @author       You
// @include      http://www.fdworlds.net/*
// @include      https://www.fdworlds.net/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var config = {
        MS_IN_SECOND: 1000,
        SECONDS_TIMEOUT: 60,
        SECONDS_TIMEOUT_PAGE: 10,
        loginInputId: 'login',
        passwordInputId: 'pass',
        submitInputId: 'subm',
        nickname: '***',			// your nickname here
        password: '***',			// your password here
    };

    setTimeout(() => {
        auth();
        var doc = getGameDocument();
        doc && hideBuildingImg(doc);
        doc && startWork(doc);
    }, config.SECONDS_TIMEOUT_PAGE * config.MS_IN_SECOND);

    function auth() {
        var loginInput = document.getElementById(config.loginInputId);
        var submitInput = document.getElementById(config.submitInputId);
        if (!loginInput || !submitInput) {
            return;
        }

        var passwordInput = document.getElementById(config.passwordInputId);
        !!passwordInput ? auth2() : auth1();

        function auth1() {
            loginInput.value = config.nickname;
            submitInput.click();
        }

        function auth2() {
            loginInput.value = config.nickname;
            passwordInput.value = config.password;
            submitInput.click();
        }
    }

    function getGameDocument() {
        var outerFrame,
            outerFrameDoc,
            innerFrame,
            innerFrameDoc;

        outerFrame = document.querySelector('frame[src="/main_frame.php"]');
        outerFrame && (outerFrameDoc = outerFrame.contentWindow.document);
        outerFrameDoc && (innerFrame = outerFrameDoc.querySelector('iframe[src="building/index.php"]'));
        innerFrame && (innerFrameDoc = innerFrame.contentWindow.document);

        return innerFrameDoc;
    }

    function hideBuildingImg(doc) {
        var imgElem = doc.querySelector('img[src*="/img/locations/nd/"][src*="stroyka"]');
        imgElem && (imgElem.hidden = true);
    }

    function startWork(doc) {
        var formElem = doc.querySelector('form[action="/building/index.php"]');
        logger('Elem: <form>');
        var selectElement = formElem.querySelector('select[name="kind"]');
        if (selectElement) {
            [].slice
                .call(selectElement.children)
                .reverse()
                .find(e => e.style.color === 'darkgreen')
                .setAttribute('selected', true);

            logger('Elem: <select>');
        }
        var beginWorkBtn = formElem.querySelector('input[value="Начать работать"]');
        beginWorkBtn && beginWorkBtn.click && beginWorkBtn.click() && logger('Elem: <input>');
    }

    function logger(data) {
        console.log(`${new Date().toLocaleString()} |`, data);
    }

    setTimeout(() => {
        location.reload();
    }, config.SECONDS_TIMEOUT * config.MS_IN_SECOND);

})();