FDWorlds work bot

try to take over the world!

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);

})();