seb92kl / ressInDeut_30112015_bak

// ==UserScript==
// @name           ressInDeut_30112015_bak
// @namespace      http://aa.com
// @version        0.1
// @author         S.K.
// @description    jquery-ui
// @include        http://uni4.xorbit.de/frames.php
//
// @grant          GM_getResourceText
// @grant          GM_getResourceURL
// @grant          GM_xmlhttpRequest
//
// @resource       jQuery               https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
//
// @resource       jQueryUICSS          http://strd6.com/stuff/jqui/theme/ui.all.css
//
// ==/UserScript==
// 30.11.2015   2_ressInDeut_30112015

// https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css


nav = frames[0] || window;
win = frames[1] || window;

function insert_jquery(pos, url) {
    var script = document.createElement('script');
    script.src = url;
    frames[0].document.head.appendChild(script);
}

insert_jquery(nav, 'http://localhost/jquery.js');
setTimeout(start(), 2500 );
function start() {

    // win.location.href = 'http://uni4.xorbit.de/fleet.php';
    $('frameset')[0].id = 'frameset';
    $('frameset')[0].cols = '350,*';
    $('frameset')[0].children[0].scrolling = 'yes';

    if ( win.location.href.match('buildings.php?') !== null && win.location.href.match('mode=fleet') !== null ) {
        page_raumschiffwerft();
    } else { console.log('...') }

    function page_raumschiffwerft() {
        met_vorhanden = win.$('#met')[0].innerText.replace(/\./g,'');
        kris_vorhanden = win.$('#cry')[0].innerText.replace(/\./g,'');
        deut_vorhanden = win.$('#deut_rechner')[0].innerText.replace(/\./g,'');
        ress_in_deut = parseInt(met_vorhanden/3)+parseInt(kris_vorhanden/2)+parseInt(deut_vorhanden);
        anzahl_benoetigt = parseInt(ress_in_deut/500000);

        schiffID = 209;
        matchString = win.$('a[href*=' + schiffID + ']')[1].nextSibling.textContent;
        if ( matchString.match('Anzahl') !== null ) { anzahl_vorhanden = matchString.match(/\d+\.*\d*/).toString().replace(/\.*/g,''); } else { anzahl_vorhanden = 0; }

        anzahl_noch_bauen = anzahl_benoetigt - anzahl_vorhanden;
        // anzahl_noch_bauen = 5000 - anzahl_benoetigt % 5000;
        if ( anzahl_noch_bauen < 0 ) {
            anzahl_noch_bauen = 0;
        } else {
            win.$('a[href*=' + schiffID + ']')[0].parentElement.parentElement.children[2].children[0].value = anzahl_noch_bauen;
            //win.$(':submit').click();
        }

        console.log('\n' + 'anzahl_vorhanden: ' + anzahl_vorhanden + '\n' + 'anzahl_benoetigt: ' + anzahl_benoetigt + '\n' + 'anzahl_noch_bauen: ' + anzahl_noch_bauen + '\n' +
                    'Met: ' + met_vorhanden/1000000 + '___||___' + parseInt(met_vorhanden/3000000) + '\n' +
                    'Kris: ' + kris_vorhanden/1000000 + '___||___' + parseInt(kris_vorhanden/2000000) + '\n' +
                    'Deut: ' + deut_vorhanden/1000000 + '___||___' + parseInt(deut_vorhanden/1000000) + '\n' 

         );
    }

    nav.$('a[id="nogif"]')[3].click();
    nav.$('a[id="nogif"]')[4].click();
    nav.$('a[id="nogif"]')[5].click();
    nav.$('a[id="nogif"]')[6].click();

    /*
    iframe1 = win.document.createElement('iframe');
    iframe1.id = 'iframe1';
    iframe1.src = 'http://uni4.xorbit.de/marchand.php';
    iframe1.width = '200px';
    iframe1.height = '150px';
    nav.document.body.appendChild(iframe1);
    */

    // ifr1 = nav.$('#iframe1')[0].contentDocument;
    // ifr1_win = nav.$('#iframe1')[0].contentWindow;

    url_marchand        = 'http://uni4.xorbit.de/marchand.php';
    url_schiffswerft    = 'http://uni4.xorbit.de/buildings.php?mode=fleet';
    url_fleet           = 'http://uni4.xorbit.de/fleet.php';
    url_flotte1         = 'http://uni4.xorbit.de/floten1.php';
    url_flotte2         = 'http://uni4.xorbit.de/floten2.php';
    url_flotte3         = 'http://uni4.xorbit.de/floten3.php';
    url_imperium        = 'http://uni4.xorbit.de/imperium.php';
    url_galaxy          = 'http://uni4.xorbit.de/galaxy.php';



    create_iframe(nav, 'iframe_metToDeut',      url_marchand,       '150px', '100px', nav, 'hidden');
    create_iframe(nav, 'iframe_krisToDeut',     url_marchand,       '150px', '100px', nav, 'hidden');
    create_iframe(nav, 'iframe_marchand',       url_marchand,       '150px', '100px', nav, 'hidden');
    create_iframe(nav, 'iframe_schiffswerft',   url_schiffswerft,   '150px', '100px', nav, 'show');
    create_iframe(nav, 'iframe_flotte',         url_fleet,          '250px', '200px', nav, 'show');
    create_iframe(nav, 'iframe_imperium',       url_imperium,       '150px', '100px', nav, 'hidden');
    create_iframe(nav, 'iframe_galaxy',         url_galaxy,         '150px', '100px', nav, 'hidden');
    create_iframe(win, 'iframe_marchand_win',   url_marchand,       '600px', '200px', win, 'show');
    create_iframe(win, 'iframe_gala_win',       url_galaxy,         '600px', '350px', win, 'hidden');
    create_iframe(win, 'iframe_schiffswerft_win',   url_schiffswerft,       '600px', '200px', win, 'show');



    delete_restimer(frames[0].frames, frames[0].frames.length);
    insert_styleSheet_iframe_5();

    window['win_metToDeut']     = frames[0].iframe_1.window;
    window['win_krisToDeut']    = frames[0].iframe_2.window;
    window['win_marchand']      = frames[0].iframe_3.window;
    window['win_schiffswerft']  = frames[0].iframe_4.window;
    window['win_flotte']        = frames[0].iframe_5.window;
    window['win_imperium']      = frames[0].iframe_6.window;
    window['win_galaxy']        = frames[0].iframe_7.window;
    window['win_marchand_win']  = frames[1].iframe_1.window;
    window['win_galaxy_win']    = frames[1].iframe_2.window;
    window['win_schiffswerft_win']  = frames[1].iframe_3.window;

    // win_galaxy_win.clearTimeout( win_galaxy_win['restimer'] );
    // win_galaxy_win.document.body.innerHTML = spio( 2, 255, 8, 1, 2, 267, 15, 1 );


    // frames[1].frames[0].window.clearTimeout(frames[1].frames[0].window['restimer']);



    create_button(nav, 'btn_metToDeut', 'btn_metToDeut ', nav, get_met );
    create_button(nav, 'btn_krisToDeut', 'btn_krisToDeut ', nav, get_kris );
    create_button(nav, 'btn_getSchiffe', 'btn_getSchiffe', nav, get_schiffe );
    create_button(nav, 'btn_insert_iframe_win', 'btn_insert' + '' + '_iframe_win', nav, insert_iframe_win );
    create_button(nav, 'insert_styleSheet_hauptframe', 'insert_styleSheet' + '' + '_hauptframe', nav, insert_styleSheet_hauptframe );
    create_button(nav, 'insert_styleSheetiframe_hauptframe_win', 'insert_styleSheet' + '' + 'iframe_hauptframe_win', nav, insert_styleSheet_iframe_marchand_win );

    insert_styleSheet_buttons()

    function create_button(location_, id, text, position, fn) {
        var button = nav.document.createElement('input');
        button.id = id;
        console.log(id + '\ttext: ' + text);
        button.innerHTML = text.toString();
        button.value = text.toString();
        button.style.backgroundColor = 'green';
        button.type = 'button';
        button.addEventListener('click', fn);
        nav.document.body.appendChild(button);
    }
    // insert_input(location_, id, text, position, fn)

    function insert_input_recs() {
        insert_input(nav, 'input_recs', 'recs: ', nav, 'fn')
        frames[0].$('#input_recs')[0].style.cssText += 'position: absolute; top: 300px; left: 0px; size: 5; text: "recs"'
    }

    function insert_input(location_, id, text, position, fn) {
        var input = nav.document.createElement('input');
        input.id = id;
        console.log(id + '\ttext: ' + text);
        input.innerHTML = text.toString();
        input.value = text.toString();
        input.style.backgroundColor = 'red';
        input.type = 'input';
        // input.addEventListener('click', fn);
        nav.document.body.appendChild(input);
    }

    function insert_iframe_win() {
        if ( frames[1].frames[0] == undefined ) {
            create_iframe(win, 'iframe_marchand_win',   url_marchand,       '600px', '200px', win, 'show');
            window['win_marchand_win']  = frames[1].iframe_1.window;
        }
        frames[1].frames[0].window.clearTimeout(frames[1].frames[0].window['restimer']);
    }

    function getHTML(pID, query) {
            if ( frames[1].frames[0] == undefined ) {
                create_iframe(win, 'iframe_marchand_win',   url_marchand,       '600px', '200px', win, 'show');
                window['win_marchand_win']  = frames[1].iframe_1.window;
            }
            frames[1].frames[0].window.clearTimeout(frames[1].frames[0].window['restimer']);
            url = 'http://uni4.xorbit.de/fleet.php';
            if (pID != undefined || typeof(pID) == 'number' ) {
                url += '?cp=' + pID
            }
            var response = frames[1].$.ajax( {
                url: url,
                type: 'GET',
                cache: false,
                async: false
            }).responseText;
            parser = new DOMParser();
            dom = parser.parseFromString(response, 'text/html');
            html = dom.querySelector(query);
            console.log( 'html: ===> ' + html.innerText );
            window['dom'] = dom;
            return html;
    }

    function get_schiffe(pID) {
        iframe_5.contentDocument.location.href = 'http://uni4.xorbit.de/fleet.php';
        setTimeout(function() {
            frames[0].frames[4].window.clearTimeout(frames[0].frames[4].window['restimer']);
            iframe_5.contentDocument.body.innerHTML = iframe_5.contentDocument.querySelectorAll('center')[4].innerHTML; 
            console.log('.........');
        }, 5000)
    }

    function get_met(pID) {
        met_vorhanden = parseInt( getHTML(pID, '#met').innerText.replace(/\./g, '') );
        setTimeout(met_to_deut(pID, met_vorhanden), 3000);
    }

    function get_kris(pID) {
        kris_vorhanden = parseInt( getHTML(pID, '#cry').innerText.replace(/\./g, '') );
        setTimeout(kris_to_deut(pID, kris_vorhanden), 3000);
    }



    function kris_to_deut(pID, kris_vorhanden) {
        kris_in_deut = parseInt( (kris_vorhanden-50000000)/2 );
        console.log('kris_vorhanden: ' + kris_vorhanden + '\t|\t' + 'kris_in_deut: ' + kris_in_deut + '\t|\t' + typeof(kris_in_deut));
        url = 'http://uni4.xorbit.de/marchand.php';
        if ( pID != undefined ) {
            url += '?cp=' + pID
        }
        if ( kris_in_deut > parseInt(0) ) {
            var request = win_krisToDeut.$.ajax( {
                url: url,
                type: 'POST',
                cache: false,
                async: true,
                data: { ress: 'cristal', metal: 0, deut: kris_in_deut }
            })
            request.done( function(data) {
                frames[1].frames[0].document.body.innerHTML = data;
                // console.debug('data: ====> ' + data);
                return true;
            });
        } else {
            return false;
        }   
    }

    function met_to_deut(pID, met_vorhanden) {
        // var met_vorhanden = parseInt( getHTML(pID, '#met').innerText.replace(/\./g, '') );
        var met_in_deut = parseInt( (met_vorhanden-50000000)/3 );
        console.log('met_vorhanden: ' + met_vorhanden + '\t|\t' + 'met_in_deut: ' + met_in_deut + '\t|\t' + typeof(met_in_deut));
        url = 'http://uni4.xorbit.de/marchand.php';
        if ( pID != undefined ) {
            url += '?cp=' + pID
        }
        if ( met_in_deut > parseInt(0) ) {
            var request = win_metToDeut.$.ajax({
                url: url,
                type: 'POST',
                cache: false,
                async: false,
                data: { ress: 'metal', cristal: 0, deut: met_in_deut }
            })
            request.done( function(data) {
                frames[1].frames[0].document.body.innerHTML = data;
                return true;
            });
        } else {
            return 0;
        }   
    }




    function spio(quelle_gala, quelle_sys, quelle_plani, quelle_planettyp, ziel_gala, ziel_sys, ziel_plani, ziel_planettyp) {
        return win_galaxy_win.$.ajax( {
            url: 'http://uni4.xorbit.de/flotenajax.php?action=send',
            type: 'POST',
            cache: false,
            async: false,
            data: { thisgalaxy: quelle_gala, thissystem: quelle_sys, thisplanet: quelle_plani, thisplanettype: quelle_planettyp, mission:6, galaxy: ziel_gala, system: ziel_sys, planet: ziel_plani, planettype: ziel_planettyp, ship210:50 }
        }).responseText
    }


    function delete_restimer(iframe, anzahl) {
        if ( anzahl != undefined && typeof(anzahl) == 'number' ) {
                setTimeout(function(){
                        for ( var i = 0; i<anzahl; i++ ) {
                            iframe[i].clearTimeout(iframe[i].window['restimer']);
                        }
                    }, 5000)
        }
    }

    function insert_styleSheet_iframe_5() {
        css_iframe_5 = frames[0].document.createElement('style');
        css_iframe_5.type = 'text/css';
        css_style_iframe_5 = '.iframe_5 { position: absolute; left:0px; top:450px; width:350px; height:200px }';
        css_iframe_5.appendChild(frames[0].document.createTextNode(css_style_iframe_5));
        frames[0].document.head.appendChild(css_iframe_5);
    }

    function insert_styleSheet_hauptframe() {
        css_hauptframe = frames[0].document.createElement('style');
        css_hauptframe.type = 'text/css';
        css_style_hauptframe = 'table[width="519"] { position: absolute; top:500px; width:350px; height:200px }';
        css_hauptframe.appendChild(frames[1].document.createTextNode(css_style_hauptframe));
        frames[1].document.head.appendChild(css_hauptframe);
    }

    function insert_styleSheet_iframe_marchand_win() {
        css_iframe_marchand_win = frames[1].document.createElement('style');
        css_iframe_marchand_win.type = 'text/css';
        css_style_iframe_marchand_win = '.iframe_1 { position: absolute; left:100px; top:75px; width:550px; height:150px }';
        css_iframe_marchand_win.appendChild(frames[1].document.createTextNode(css_style_iframe_marchand_win));
        frames[1].document.head.appendChild(css_iframe_marchand_win);
    }
    /*
        create_button(nav, 'btn_metToDeut', nav, get_met );
        create_button(nav, 'btn_krisToDeut', nav, get_kris );
        create_button(nav, 'btn_getSchiffe', nav, get_schiffe );
        create_button(nav, 'btn_insert_iframe_win', nav, insert_iframe_win );
        create_button(nav, 'insert_styleSheet_hauptframe', nav, insert_styleSheet_hauptframe );
        create_button(nav, 'insert_styleSheet_iframe_marchand_win', nav, insert_styleSheet_iframe_marchand_win );
    */


    function insert_styleSheet_buttons() {
     css_buttons = frames[0].document.createElement('style');
        css_buttons.type = 'text/css';
        css_style_buttons = `
                            #btn_metToDeut { position: absolute; left:0px; top:50px; width:100px; height:20px }
                            #btn_krisToDeut { position: absolute; left:0px; top:100px; width:100px; height:20px }
                            #btn_getSchiffe { position: absolute; left:0px; top:150px; width:100px; height:20px }
                            #btn_insert_iframe_win { position: absolute; left:0px; top:200px; width:150px; height:40px }
                            #insert_styleSheet_hauptframe { position: absolute; left:0px; top:250px; width:150px; height:40px }
                            #insert_styleSheet_iframe_marchand_win { position: absolute; left:0px; top:300px; width:150px; height:40px }
                            `;
        css_buttons.appendChild(frames[0].document.createTextNode(css_style_buttons));
        frames[0].document.head.appendChild(css_buttons);
    }

    function create_iframe(location_, id, src, width, height, position, hidden) {
        iframe_max = parseInt(location_.document.querySelectorAll('iframe').length);
        var iframe_new = parseInt(iframe_max + 1);
        window['iframe_' + iframe_new] = location_.document.createElement("iframe");
        eval('iframe_' + iframe_new + '.id = id');
        eval('iframe_' + iframe_new + '.name = "iframe_" + iframe_new');
        eval('iframe_' + iframe_new + '.src = src');
        eval('iframe_' + iframe_new + '.className = "iframe_" + iframe_new');

        if ( width != undefined ) {
            eval('iframe_' + iframe_new + '.width = width');
        } else {
            eval('iframe_' + iframe_new + '.width = "150px"');
        }

        if ( height != undefined ) {
            eval('iframe_' + iframe_new + '.height = height');
        } else {
            eval('iframe_' + iframe_new + '.height = "75px"');
        }

        if ( hidden == 'hidden' ) {
            eval('iframe_' + iframe_new + '.hidden = true');
        }

        position.document.body.appendChild(eval('iframe_' + iframe_new));
        console.info('\n' + 'id: ' + id + '\t|\t' + 'name: ' + 'iframe_' + iframe_new);
        return (eval('iframe_' + iframe_new));
    }


    function insert_div_outside_table() {
        sHTML = frames[1].$('center')[3].children[2].outerHTML;
        div = document.createElement('div');
        div.id = 'div1';
        div.innerHTML = sHTML;
        div.className = 'table-draggable-resizeable';
        frames[1].$('center')[3].children[2].innerHTML = div.innerHTML;


        wrapper = document.createElement('div');
        wrapper.id = 'div_mainTable';
        myDiv = frames[1].$('center')[3].children[2];
        wrapper.appendChild(myDiv.cloneNode(true));

        s_head = `
                  <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
                  <script src="http://localhost/jquery-2.1.4.js"></script>
                  <script src="http://localhost/jquery-ui.js"></script>
                  <style>
                  #container { width: 500px; height: 500px; }
                  #resizable { width: 450px; height: 450px; padding: 0.5em; }
                  #resizable h3 { text-align: center; margin: 0; }
                  </style>
                  <script>
                  $(function() {
                    $( "#resizable" ).resizable({
                      containment: "#container"
                    });
                    $( "#resizable" ).draggable();
                  });
                  </script>
              `;
        frames[1].document.head.innerHTML += s_head;

        s_body = `
                <div id="container" class="ui-widget-content">
                  <div id="resizable" class="ui-widget-content">
                    <!-- <h3 class="ui-widget-header">Resizable</h3> -->
                    <iframe src="http://uni4.xorbit.de/marchand.php"></iframe>
                  </div>
                </div>
               `;
       frames[1].document.body.innerHTML += s_body;

    }

}