User:Jens Ingels/common.js

function getHTML (oXHR, sTargetId) { //gets HTML via AJAX, but is more compatible var rOpen = new RegExp("<(?!\!)\\s*([^\\s>]+)[^>]*\\s+id\\=[\"\']" + sTargetId + "[\"\'][^>]*>" ,"i"), sSrc = oXHR.responseText, aExec = rOpen.exec(sSrc); return aExec ? (new RegExp("(?:(?:.(?!<\\s*" + aExec[1] + "[^>]*[>]))*.?<\\s*" + aExec[1] + "[^>]*[>](?:.(?!<\\s*\/\\s*" + aExec[1] + "\\s*>))*.?<\\s*\/\\s*" + aExec[1] + "\\s*>)*(?:.(?!<\\s*\/\\s*" + aExec[1] + "\\s*>))*.?", "i")).exec(sSrc.slice(sSrc.indexOf(aExec[0]) + aExec[0].length)) || "" : ""; }

var myPage = "Lucian"; var userpageurl = "http://leagueoflegends.wikia.com/wiki/" + myPage; var oReq = new XMLHttpRequest; oReq.open("GET", userpageurl, true); oReq.onload = function { var osLucian = getHTML(this, "champion_info-lower"); } oReq.send(null); alert(osLucian); /*

$.getJSON("/api.php?format=json&action=expandtemplates&text=%7B%7B:Lucian%7D%7D", function(data) {	var a = $(' ').html(data.expandtemplates["*"])	var loc = $(a).find("#champion_info-lower td span a[title='Health']");       var loc = loc.html;        alert(loc);        var loc = $(loc).parent.parent.next;	valueCode = val.toLowerCase.replace(/ /g,"_");        value = loc.html;        if (value == undefined) {        }        else        {           $( loc ).addClass( "data-" + valueCode );           defCode = value.replace(/\(.*?\)/, ' $& ');           $(a).find(".data-" + valueCode).html(defCode);           test = $(a).find(".data-" + valueCode).text;        }        alert(test); });



/**
 * Description:
 * Improves the functionality of the champion infobox.
 * @Author Jens Ingels
 * Last updated 12th April, 2014
 * @Version 0.1.3 (béta)
 * Last updated 12th April, 2014
 * @Version 0.1.3 (béta)

$( document ).ready(function {

if( $('#champion_info').length ) {

//Prepare Arrays var name = ["Health","Health regeneration","Mana","Mana regeneration","Range","Attack damage","Attack speed","Armor","Magic resistance","Movement speed", "Energy", "Energy regeneration"];

//Def Classes $.each(name, function( index, val ) {

loc = $( "#champion_info-lower td span a[title='"+ val + "']").parent.parent.next; valueCode = val.toLowerCase.replace(/ /g,"_"); value = loc.html; if (value == undefined) { }        else {           $( loc ).addClass( "data-" + valueCode ); defCode = value.replace(/\(.*?\)/, ' $& '); $(".data-" + valueCode).html(defCode); }

});

//Add buttons $( "#champion_info-lower td:nth-child(2) table th").append( " Lv default 1 2 3 4 5 6 7 8 9 10 11 12 13 <option value='14'>14 <option value='15'>15 <option value='16'>16 <option value='17'>17 <option value='18'>18 " ); //Prepare Functions callBase = function(value) { if( $( ".data-" + value ).length ) { mainVal = $( ".data-" + value ).contents.filter(function {           return this.nodeType == 3;            }).text; mainVal = parseFloat(mainVal); return mainVal; }        else {        }      };

callPlus = function(value) { plusVal = $( ".data-" +value+ " span" ).html; plusVal = plusVal.substring(1, plusVal.length-1) if(plusVal[plusVal.length - 1] === '%') { plusVal = parseFloat(plusVal); plusVal = plusVal/100; }        else {           plusVal = parseFloat(plusVal); }        return plusVal; };

//Collect Data health = { base : callBase("health"), plus : callPlus("health"), backup : $( ".data-health" ).html };     health_regeneration = { base : callBase("health_regeneration"), plus : callPlus("health_regeneration"), backup : $( ".data-health_regeneration" ).html };     if( $('.data-mana').length ) {

mana = { base : callBase("mana"), plus : callPlus("mana"), backup : $( ".data-mana" ).html };	        mana_regeneration = { base : callBase("mana_regeneration"), plus : callPlus("mana_regeneration"), backup : $( ".data-mana_regeneration" ).html };

}     if( $('.data-energy').length ) {

energy = { base : callBase("energy"), backup : $( ".data-energy" ).html };        energy_regeneration = { base : callBase("energy_regeneration"), backup : $( ".data-energy_regeneration" ).html };

}     range = { base : callBase("range"), plus : callPlus("range"), backup : $( ".data-range" ).html };     attack_damage = { base : callBase("attack_damage"), plus : callPlus("attack_damage"), backup : $( ".data-attack_damage" ).html };     attack_speed = { base : callBase("attack_speed"), plus : callPlus("attack_speed"), backup : $( ".data-attack_speed" ).html };     armor = { min : "10.75", max : "25", mid : "17.875", base : callBase("armor"), plus : callPlus("armor"), backup : $( ".data-armor" ).html };     magic_resistance = { base : callBase("magic_resistance"), plus : callPlus("magic_resistance"), backup : $( ".data-magic_resistance" ).html };     movement_speed = { base : callBase("movement_speed"), backup : $( ".data-movement_speed" ).html };

//On Change Calculation $( "#lv" ) .change(function {

getLevel = $("#lv").val; getLevel = parseInt(getLevel); if ( getLevel == 0 ) { sendA = health.backup; sendB = health_regeneration.backup; if( $('.data-mana').length ) { sendC = mana.backup; sendD = mana_regeneration.backup; }        sendF = attack_damage.backup; sendG = attack_speed.backup; sendH = armor.backup; sendI = magic_resistance.backup; }        else {        sendA = health.base + health.plus * getLevel; sendB = health_regeneration.base + health_regeneration.plus * getLevel; sendB = (sendB).toFixed(2); if( $('.data-mana').length ) { sendC = mana.base + mana.plus * getLevel; sendD = mana_regeneration.base + mana_regeneration.plus * getLevel; sendD = (sendD).toFixed(2); }        sendF = attack_damage.base + attack_damage.plus * getLevel; sendF = (sendF).toFixed(2); sendG = attack_speed.base + attack_speed.plus * (getLevel - 1); sendG = (sendG).toFixed(3); sendH = armor.base + armor.plus * getLevel; sendH = (sendH).toFixed(2); sendI = magic_resistance.base + magic_resistance.plus * getLevel; }        $( ".data-health" ).html(sendA); $( ".data-health_regeneration" ).html(sendB); if( $('.data-mana').length ) { $( ".data-mana" ).html(sendC); $( ".data-mana_regeneration" ).html(sendD); }        $( ".data-attack_damage" ).html(sendF); $( ".data-attack_speed" ).html(sendG); $( ".data-armor" ).html(sendH); $( ".data-magic_resistance" ).html(sendI);

});     //test      a = armor.base - parseInt(armor.min);      alert(a);      b = parseInt(armor.max) - armor.base;      alert(b);      if (armor.base >= parseInt(armor.mid)) {         c = armor.base - parseInt(armor.mid);      }      else      {         c = parseInt(armor.mid) - armor.base;      }      alert(c);      median = Math.max(Math.min(a,b), Math.min(Math.max(a,b),c));      alert(median);   } });