seb92kl / marchand_ajax_18112015

// ==UserScript==
// @name         marchand_ajax_18112015
// @namespace    http://tampermonkey.net/
// @version      0.1.4
// @description  try to take over the world!
// @author       You
// @icon         https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTf0UnJ9mqyjuAJTOk8pCs9-mJG94aNrf1lmKxvViy9jduVnPIk
// @match        http://uni4.xorbit.de/framesAAA.php
// @match        http://uni4.xorbit.de/marchand.php
// @require      https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @match        file:///opt/local/apache2/htdocs/local/html/_XOrbit/Frames/frames.php.html
// @grant        none
// ==/UserScript==
// var pID = prompt();
var pID;
// alert('start');


// if ( frames[0].document.location.href.match('http://uni4.xorbit.de/leftmenu.php') ) {

    // alert('left');

// }


console.clear();

console.debug('#############################################\n');


(function() {
    insert_script('http://localhost/jquery.js', 'jquery');
})()


function insert_script(url_src, script_name) {
            scr = document.createElement('script');
            scr.src = url_src;
            scr.id = script_name;
            scr.type = 'text/javascript';

            try {
                document.getElementsByTagName('head')[0].appendChild(scr);
                window.setTimeout(function() {
                    script_inserted = document.getElementById('jquery');
                    if (script_inserted !== null) {
                        // jquery-js-file wurde geladen und kann jetzt verwendet werden
                        fn_startNow();
                    } else {
                        console.debug('\n@@@@@@@@@@@@@@@@@@@@@@|===>  wasnt defined')
                    }
                }, 250);
            } catch (e) {
                console.log('\t\t====> e: ->\t' + e);
            }
        }

function fn_startNow() {
    ajax_get('http://uni4.xorbit.de/marchand.php');
}

function insert_values(dom_html) {
    window['dom'] = dom_html;
    met = $(dom).find('#met')[0].innerText.replace(/\./g, '');
    kris = $(dom).find('#cry')[0].innerText.replace(/\./g, '');
    deut = $(dom).find('#deut_rechner')[0].innerText.replace(/\./g, '');
    met_in_deut = parseInt( (met/3 - 5000000) );
    kris_in_deut = parseInt( (kris/2 - 5000000) );
    ress_in_deut = parseInt(met_in_deut) + parseInt(kris_in_deut) + parseInt(deut);
    i_recycler_benoetigt = parseInt(ress_in_deut/500000);
    
    console.info('\n' +
                 '####################################################################################\n' +
                 '____________________________________________________________________________________\n' +
                 '###--' + 'ress_in_deut: ' + ress_in_deut + ' \n' +
                 '###__recycler_benoetigt: ' + i_recycler_benoetigt + '\t|\tmet:\t' + parseInt(met/1000000)  + '\t|\tkris:\t' + parseInt(kris/1000000)  + '\t|\tdeut:\t' + parseInt(deut/1000000) + '\n' +
                 '____________________________________________________________________________________\n' +
                 '####################################################################################'
                )
    
    console.info(pID + '\t__->  ' + 'davor: ' + '\t|||\t__kris:' + met/1000000 + '\t|\t' + met_in_deut/1000000 + '\t|||\t__kris:' + kris/1000000 + '\t|\t' + kris_in_deut/1000000 + '\t|||\t__kris:' + deut/1000000);
    console.info(pID + '\t__->  ' + 'davor: ' + '\t|||\t__met:' + met.replace(/\./g,'')/1000000 + '\t|||\t__kris:' + kris.replace(/\./g,'')/1000000 + '\t|||\t__deut:  ' + deut.replace(/\./g,'')/1000000);

    // plani = jQuery('#pselector>option[selected]').html().replace(/ /g, '_|_').match(/.*\]/);
    console.debug('davor: ' + met + '|||' + met_in_deut + '\t|\t' + deut)
}


function fn_selector(rootElement, selector, findString) {
       var ergebnis = $(rootElement).find(selector).find(findString).text();

       return ergebnis;
}

function ajax_get(url) {
            $ = window['jQuery'];
            var parser = new DOMParser();
            jqxhr = $.get(url, function(data) {})
                .done(function(data) {
                    html = parser.parseFromString(data, 'text/html');
                    insert_values(html);
                    tauscheRessis(dom);
                })
                .fail(function(data) {
                    alert("error:\n" + data);
                })
        }

function ajax_post_metInDeut(url, ressName, met_in_deut) {
    $.post( url, { ress: 'metal', cristal: 0, deut: met_in_deut })
    .done( function(response) {
        parser = new DOMParser();
        dom = parser.parseFromString(response, 'text/html');
        met_danach = dom.getElementById('met').innerText;
        kris_danach = dom.getElementById('cry').innerText;
        deut_danach = dom.getElementById('deut_rechner').innerText;
        console.debug('met_in_deut: danach: ' + met_danach + '\t|\t' + deut_danach);
        console.info(pID + '\t__->  ' + 'danach: ' + '\t|||\t__met:__' + met_danach.replace(/\./g,'')/1000000 + '\t|||\t__kris:__' + kris_danach.replace(/\./g,'')/1000000 + '\t|||\t__deut:__' + deut_danach.replace(/\./g,'')/1000000);
    })
}



function ajax_post_krisInDeut(url, ressName, kris_in_deut) {
    $.post( url, { ress: 'cristal', metal: 0, deut: kris_in_deut })
    .done( function(response) {
        parser = new DOMParser();
        dom = parser.parseFromString(response, 'text/html');
        met_danach = dom.getElementById('met').innerText;
        kris_danach = dom.getElementById('cry').innerText;
        deut_danach = dom.getElementById('deut_rechner').innerText;
        console.debug('kris_in_deut: danach: ' + kris_danach + '\t|\t' + deut_danach);
        console.info(pID + '\t__->  ' + 'danach: ' + '\t|||\t__kris:' + kris_danach.replace(/\./g,'')/1000000 + '\t|||\t__deut:  ' + deut_danach.replace(/\./g,'')/1000000);
    })
}

// ajax_post_DeutInRest('marchand.php', 'deut', deut_to_met, deut_to_kris);
// function ajax_post_krisInDeut(url, ressName, kris_in_deut) {
function ajax_post_DeutInRest(url, data_send) {

    data_send = 'ress=deuterium&metal=2000000000&cristal=2000000000';
    url = 'marchand.php';
    console.info(url + '\n' + data_send);
    console.info('\n################################\n########################');
//    exit;
    $.post( url, data_send )
    $.ajax({
        url: "marchand.php",
        type: "post",
        contentType: 'application/x-www-form-urlencoded',
        data: data_send,
    }).done( function(response) {
        parser = new DOMParser();
        dom = parser.parseFromString(response, 'text/html');
        if ( dom.title.match('Anticheat') ) {
            alert('fehler! \n  Anticheat');
        } else {
            met_danach = dom.getElementById('met').innerText;
            kris_danach = dom.getElementById('cry').innerText;
            deut_danach = dom.getElementById('deut_rechner').innerText;
            console.debug('kris_in_deut: danach: ' + kris_danach + '\t|\t' + deut_danach);
            console.info(pID + '\t__->  ' + 'danach: ' + '\t|||\t__kris:' + kris_danach.replace(/\./g,'')/1000000 + '\t|||\t__deut:  ' + deut_danach.replace(/\./g,'')/1000000);
        }
    })
}

function send_post() {
    $.ajax({
        method:	"POST",
        contentType:   "application/x-www-form-urlencoded",
        url:	"marchand.php",
        data:	{ ress: 'metal', cristal: '0', deut: '400000000' },
    })
        .done(function( returnVal ) {
        console.info( 'returned_value: ' + returnVal );
        window['html_response'] = returnVal;
    });
}

function marchand_deut(url_pl) {

postData = 'ress=deut&metal=200000000&cristal=200000000';
$.ajaxSetup({
  url: "marchand.php"
});

$.ajaxSetup({
  url: "/xmlhttp/",
  global: false,
  async: false,
  type: "POST",
  method: "POST"
});
$.ajaxSend(postData);
// $.ajax({ ress: 'deut', metal: '500000000', cristal: '200000000' });    

// $.ajaxComplete( function(data) { console.debug( '########@@@@@@@@@@@@@______________@@@@@@@@@@@@@@#######' + '\n' + data) } ); 

    
    
}

function ajax_post(pID, tauschArt, ressValue, ressValue2) {
       url = 'marchand.php' + '?cp=' + pID;
       var ressName_von;
       a_tmp = tauschArt.split('_');
       ressName_von_input = a_tmp[0];
       if ( ressName_von_input == 'met' ) { ressName_von = 'metal' };
       if ( ressName_von_input == 'kris' ) { ressName_von = 'cristal' };
       if ( ressName_von_input == 'deut' ) { ressName_von = 'deuterium' };
    
       ressName_get = a_tmp[2];
    
       
       var ressname_von;
       var ressName1;
       var ressName2;
       var value1;
       var value2;
              
       if ( ressName_von_input == 'met' && ressName_get == 'deut') {
            ressName1 = 'cristal';
            ressName2 = 'deuterium';
            value1 = 0;
            value2 = ressValue;
       } else
           if ( ressName_von_input == 'met' && ressName_get == 'kris') {
                ressName1 = 'cristal';
                ressName2 = 'deuterium';
                value1 = ressValue;
                value2 = 0;
           } else
               if ( ressName_von_input == 'kris' && ressName_get == 'met' ) {
                    ressName1 = 'metal';
                    ressName2 = 'deuterium';
                    value1 = ressValue;
                    value2 = 0;
               } else
                   if ( ressName_von_input == 'kris' && ressName_get == 'deut' ) {
                        ressName1 = 'metal';
                        ressName2 = 'deuterium';
                        value1 = 0;
                        value2 = ressValue;
                   } else
                       if ( ressName_von_input == 'deut' && ressName_get == 'met' ) {
                            ressName1 = 'metal';
                            ressName2 = 'cristal';
                            value1 = ressValue;
                            value2 = 0;
                       } else
                           if ( ressName_von_input == 'deut' && ressName_get == 'kris' ) {
                                ressName1 = 'metal';
                                ressName2 = 'cristal';
                                value1 = 0;
                                value2 = ressValue;
                           } else
                               if ( ressName_von_input == 'deut' && ressName_get == 'kris' ) {
                                    ressName1 = 'metal';
                                    ressName2 = 'cristal';
                                    value1 = 0;
                                    value2 = ressValue;
                               } else {
                                   // falsche Eingabe
                               };
                           
    

//        formData.append( 'ress', ressName_get );
//        formData.append( ressName1, value1 );
//        formData.append( ressName2, value2 );
        formData = new FormData();
        formData.append( 'ress', 'metal' );
        formData.append( 'cristal', 0 );
        formData.append( 'deuterium', 100000000 );
//        formData = 'ress=' + ressName_von + '&' + ressName1 + '=' + value1 + '&' + ressName2 + '=' + value2;
        console.info(url + '\n' + formData);
     
        $.post(url, formData )
    /*
        formData = new FormData();
        formData.append( 'ress', ressName_get );
        formData.append( ressName1, value1 );
        formData.append( ressName2, value2 );
        $.ajax({
            url: url,
            type: "post",
            contentType: 'application/x-www-form-urlencoded',
            data: formData,
        })
     */
        .done( function(response) {
            parser = new DOMParser;
            dom = parser.parseFromString(response, 'text/html');
            if ( dom.title.match('Anticheat') ) {
                alert('fehler! \n  Anticheat');
            } else {
                met_danach = dom.getElementById('met').innerText;
                kris_danach = dom.getElementById('cry').innerText;
                deut_danach = dom.getElementById('deut_rechner').innerText;
                // console.debug('kris_in_deut: danach: ' + kris_danach + '\t|\t' + deut_danach);
                console.info(pID + '\t__->  ' + 'danach: ' + '\t|||\t__met:' + met_danach.replace(/\./g,'')/1000000 + '\t|||\t__kris:' + kris_danach.replace(/\./g,'')/1000000 + '\t|||\t__deut:  ' + deut_danach.replace(/\./g,'')/1000000);
            };
        })
};

    
    


function tauscheRessis() {

    eingabe = prompt();
    if ( eingabe == null ) { eingabe = 1; };

    if ( eingabe == 1 ) {
        ajax_post_metInDeut('marchand.php', 'metal', met_in_deut);
        ajax_post_krisInDeut('marchand.php', 'cristal', kris_in_deut);
    } else if ( eingabe == 2 ) {
        formData = new FormData()
        formData.append( 'ress', 'deut' );
        formData.append( 'metal', '200000000' );
        formData.append( 'cristal', '200000000' );
        ajax_post_DeutInRest('marchand.php', formData);
    } else if ( eingabe == 'test' ) {
        ajax_post(19, 'met_in_deut', 300000000, 0);   
    }

}