simplexe / Wofh attack export

// ==UserScript==
// @name         Wofh attack export
// @namespace    https://ru.waysofhistory.com/
// @version      0.1
// @description  Export attack list to Google
// @author       simplexe
// @match        https://*.waysofhistory.com/gen/html/*
// @grant        unsafeWindow
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    var $ = unsafeWindow.jQuery;
    if (!$) {
        return;
    }

    var global = unsafeWindow;

    if (!unsafeWindow.vArmy) {
          if (global.console) {
            global.console.log('Unknown window');
        }
        return;
    }

    var playerName = '';
    var currentHost = '';

    insertButtons();
    function _setValue(key, value) {
        window.localStorage.setItem(key + '_' + playerName + '_' + currentHost, value);
    }

    function _getValue(key, defaultValue) {
        var itm = window.localStorage.getItem(key + '_' + playerName + '_' + currentHost);
        return itm != null ? itm == 'false' ? false : itm : defaultValue;
    }

    function getActiveWindow() {
        var aWindow = global.wndMgr.getActiveWnd();
        if (aWindow && aWindow.name === "army") {
            return aWindow;
        } else {
            return null;
        }
    }

    function loadGoogleKey() {
       var aWindow = getActiveWindow();
        if (!aWindow) {
            if (global.console) {
            global.console.log('Unknown window');
        }
            return;
        }
        return _getValue('key', '');

    }

    function loadGoogleEntry() {
       var aWindow = getActiveWindow();
        if (!aWindow) {
            if (global.console) {
            global.console.log('Unknown window');
        }
            return;
        }
        return _getValue('entry', '');

    }

    function checkGoogleKey() {
        playerName = wofh.account.name;
        currentHost = location.host;
        if (playerName.length < 1 && currentHost.length < 1) {
            return;
        }
        var keyGoogle = loadGoogleKey();
        if (keyGoogle.length > 0) {
            $('.attack-key-export-input').val(keyGoogle);
        }
        var curAttack = getAttack();
        if (curAttack.length > 1) {
            $('.attack-export-btn').removeClass('-disabled')
        }

    }

    function checkGoogleID() {
        if (playerName.length < 1 && currentHost.length < 1) {
            return;
        }
        var idGoogle = loadGoogleEntry();
        var keyGoogle = loadGoogleKey();
        if (idGoogle.length > 0) {
            $('.attack-entry-export-input').val(idGoogle);
        }
        var curAttack = getAttack();
        if (curAttack.length > 2 && keyGoogle.length > 0) {
            $('.attack-export-btn').removeClass('-disabled')
        }

    }

    function getAttack()
    {
        var attack = JSON.stringify(wofh.events.list.filter(function(o){return 101==o.type}).map(function(o){return{id:o.id,speed:o.data.speed,start:o.start,arrival:o.time,atid:o.town1,dtid:o.town2,atname:wofh.world.towns[o.town1].name,aaname:wofh.world.towns[o.town1].account.name,aaid:wofh.world.towns[o.town1].account.id,acid:wofh.world.towns[o.town1].country,dtname:wofh.world.towns[o.town2].name,daname:wofh.world.towns[o.town2].account.name,daid:wofh.world.towns[o.town2].account.id,dcid:wofh.world.towns[o.town2].country}}));
        return attack;
    }

    function saveGoogleKey() {
        if ($('.attack-key-save-btn').hasClass('-disabled')) {
            return;
        }
        var aWindow = getActiveWindow();
        if (!aWindow) {
            alert("Unknown window");
            return;
        }

        _setValue('key', $('.attack-key-export-input').val());
        $('.attack-key-save-btn').addClass('-disabled');
    }

    function saveGoogleID() {
        if ($('.attack-entry-save-btn').hasClass('-disabled')) {
            return;
        }
        var aWindow = getActiveWindow();
        if (!aWindow) {
            alert("Unknown window");
            return;
        }

        _setValue('entry', $('.attack-entry-export-input').val());
        $('.attack-entry-save-btn').addClass('-disabled');
    }

    function toGoogle() {
        if ($('.attack-export-btn').hasClass('-disabled')) {
            return;
        }
        var aWindow = getActiveWindow();
        if (!aWindow) {
            alert("Unknown window");
            return;
        }
        var curAttack = getAttack();
        var key = loadGoogleKey();
        var entry = loadGoogleEntry();
        if (curAttack.length < 3 && key.length < 1 && entry.length < 1) {
            return;
        }

        $.ajax({
            url: 'https://docs.google.com/forms/d/e/' + key + '/formResponse',
            data: { [entry] : curAttack },
            type: 'POST',
            dataType: 'xml',
            success: function(data, textStatus, XMLHttpRequest) {
                console.log('success');
                console.log(data);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.log('error');
                console.log(textStatus);
            },
        });
        alert ("Выгружено!");

    }


    // div with stuff
    function addHTML() {
        var exportAttackContainer = $('.page_army').parent();
        if (exportAttackContainer.length > 0) {
            if (exportAttackContainer.find('.attack-export').length === 0) {
                exportAttackContainer.prepend($('<div class="page_army_u_cont attack-export"/>'));
                exportAttackContainer.find('.attack-export').append($('<table class="attack-export-table"/>'));
                exportAttackContainer.find('.attack-export-table').append($('<td class="attack-key-td-p"/>'));
                exportAttackContainer.find('.attack-export-table').append($('<td class="attack-key-td-input"/>'));
                exportAttackContainer.find('.attack-export-table').append($('<td class="attack-key-td-span"/>'));
                exportAttackContainer.find('.attack-export-table').append($('<td class="attack-key-td-save-btn"/>'));
                exportAttackContainer.find('.attack-export-table').append($('<td class="attack-entry-td-p"/>'));
                exportAttackContainer.find('.attack-export-table').append($('<td class="attack-entry-td-input"/>'));
                exportAttackContainer.find('.attack-export-table').append($('<td class="attack-entry-td-span"/>'));
                exportAttackContainer.find('.attack-export-table').append($('<td class="attack-entry-td-save-btn"/>'));
                exportAttackContainer.find('.attack-export-table').append($('<td class="attack-td-export-btn"/>'));
                exportAttackContainer.find('.attack-key-td-p').append($('<p class="attack-key-export-p"/>').text('Ключ'));
                exportAttackContainer.find('.attack-key-td-input').append($('<input type=text class="attack-key-export-input"/>'));
                exportAttackContainer.find('.attack-key-td-span').append($('<span class="helpIcon attack-key-export-span"/>').attr('data-title', "Вставьте ключ вашей формы атак Google"));
                exportAttackContainer.find('.attack-key-td-save-btn').append($('<button class="button1 attack-key-save-btn -disabled"/>').attr('style','bottom: 12px;').text('Сохранить').on('click', saveGoogleKey));
                exportAttackContainer.find('.attack-entry-td-p').append($('<p class="attack-entry-export-p"/>').text('ID'));
                exportAttackContainer.find('.attack-entry-td-input').append($('<input type=text class="attack-entry-export-input"/>'));
                exportAttackContainer.find('.attack-entry-td-span').append($('<span class="helpIcon attack-entry-export-span"/>').attr('data-title', "Вставьте ID вашей формы атак Google"));
                exportAttackContainer.find('.attack-entry-td-save-btn').append($('<button class="button1 attack-entry-save-btn -disabled"/>').attr('style','bottom: 12px;').text('Сохранить').on('click', saveGoogleID));
                exportAttackContainer.find('.attack-td-export-btn').append($('<button class="button1 attack-export-btn"/>').attr('style','bottom: 12px;').text('Выгрузить').on('click', toGoogle));
                exportAttackContainer.find('.attack-key-export-input').on('input', function() {$('.attack-key-save-btn').removeClass('-disabled');});
                exportAttackContainer.find('.attack-entry-export-input').on('input', function() {$('.attack-entry-save-btn').removeClass('-disabled');});

            }
        }
    }

    function insertButtons() {
        var orig = tblArmyOper.prototype.afterShow;

        tblArmyOper.prototype.afterShow = function () {
            orig.apply(this, arguments);
            addHTML();
            checkGoogleKey();
            checkGoogleID();
        };
        if (global.console) {
            global.console.log('Done!');
        }
    }
})();