User:Cloned/Interwiki Language Events

From Guild Wars 2 Wiki
Jump to navigationJump to search

This is my script to display all events.

How to use it[edit]

Copy-Paste this to a new textfile (not word ;) ) and change the end from .txt to .html, open the page and give it a few seconds to load. It fetches every Event-Name for every language from the API, this can take a few seconds (for me it took ~8 seconds). The results are stored in the browser so that the next time you open the page it skips the api-call and the list appears instantly. If new events are added, clear the local storage and then generate the EventList again (it can take some seconds to build it again). It's not very pretty and not fool-proof, so if you click the "Generate complete Event List" button too fast, too often ... I don't know what happens.

The underline for visited links is just temporary, if you close your browser and open it again, you won't see it. But you can see where you left off because the textfield still has the name of the last events inside (at least for me in opera)

One last thing: To change the output from the german to the english names, find the OutputList function and change dictionary["de"] to dictionary["en"].

If you want to use this for the other wikis (es, fr) feel free to modify this to show the appropriate textfields

<!DOCTYPE HTML>
 <html>
 <head>
     <meta charset="utf-8" />
     <meta http-equiv="X-UA-Compatible" content="IE=10" />
     <title>GW2 language events</title>
     <!--<link type="text/css" rel="stylesheet" href="Content/style.css" />-->
     <style>
        /*#test1 ul{width:40%;}*/
        #output{  width: 50%;
                    min-height: 400px;
                    border: 1px solid black;
                    position: fixed;
                    top: 1px;
                    right: 0;}

        #test1 li{padding:5px;cursor: pointer}
     </style>
 </head>
     <body>
 		<div>
 			<label for="slctLang">Language:</label>
 			<select id="slctLang">
 				<option value="de">Deutsch</option>
 				<option value="en">English</option>
 				<option value="es">Espanol</option>
 				<option value="fr">French</option>
 			</select> 

 			<button onclick="javascript:addEvents()"> Generate complete Event List</button>
      <button onclick="javascript:localStorage.clear();$('#test1').empty();"> Clear List</button>
 			
         </div>
 
 		<div id="test1"></div>
        <div id="output">
            <h2>Deutsch</h2>
            <a id="linkDE" href="http://" target="ger"> Deutsch </a> <br>
            Edit: <a id="editDE" href="http://" target="ger"> Deutsch </a> <br>
            <textarea id="interwikiDE" rows="5" cols="100"> </textarea>

            <h2>Englisch</h2>
            <a id="linkEN" href="http://" target="en"> Englisch </a> </br>
            Edit: <a id="editEN" href="http://" target="en"> Englisch </a> <br>
            <textarea id="interwikiEN" rows="5" cols="100"> </textarea>
            
        </div>
 
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
 <script type="text/javascript">


 const LANGUAGES = ["de", "en", "es", "fr"];
 var dictionary = {"de" : [], "en": [] , "es" :[] , "fr" :[]}, 
 EVENTS = {},
 eventNamesUri = "https://api.guildwars2.com/v1/event_details.json",
 i = LANGUAGES.length - 1; 

GW2 = window.GW2 || {} ;

GW2.EventNames = function(){
    i = LANGUAGES.length - 1;
    if (typeof LANGUAGES[i] != 'undefined'){
      callEvents( LANGUAGES[i])
    }
}

$(document).ajaxComplete(function() {
  console.log("ajax complete");
});

function callEvents(lang){
    $.getJSON(eventNamesUri+"?lang="+lang,
        function(data){
            $.each(data, function () {
                //console.log(lang);
                for (key in this){
                    //console.log(this[key].name);
                    dictionary[lang].push(this[key].name);
                 }
             });
            localStorage.setItem('lang-'+lang, JSON.stringify(dictionary[lang] ) );
        }).done(function() {
          console.log(" call complete ", i);
          i--;
          if (i == -1){
            OutputList();
          }
          else { callEvents(LANGUAGES[i]) }
        });
}


//local storage -> if something is in localstorage, display it!
if (localStorage.getItem('lang-de') != null){
    dictionary["de"] = JSON.parse(localStorage.getItem('lang-de'));
    dictionary["en"] = JSON.parse(localStorage.getItem('lang-en'));
    dictionary["es"] = JSON.parse(localStorage.getItem('lang-es'));
    dictionary["fr"] = JSON.parse(localStorage.getItem('lang-fr'));
    OutputList();
}
else {
    //console.log("start");
    GW2Events = new GW2.EventNames();
}


function OutputList(){
    var j = dictionary["de"].length;
    $("#test1").empty();
    var ul = document.createElement("ul");
    while (j--){
        var name = formatName(dictionary["de"][j]);
        var li = "<li data-index='"+j+"'>" + name  +"</li>"
        $(ul).append(li);
    }
    $("#test1").append(ul);
}

function fillOutput(){

    $(this).css("text-decoration", "underline")

    //console.log(this); this = <li> .. </li>
    //generate DE 
    var index = $(this).data("index"), eventName = formatName(dictionary["de"][index]);
    $("#linkDE").attr("href", "http://wiki-de.guildwars2.com/wiki/"+eventName).html(eventName);
    $("#editDE").attr("href", "http://wiki-de.guildwars2.com/index.php?title="+eventName+"&action=edit").html( "Edit "+eventName);

    var txt  = "[[en:"+formatName(dictionary["en"][index])+"]]\n";
        txt += "[[es:"+formatName(dictionary["es"][index])+"]]\n";
        txt += "[[fr:"+formatName(dictionary["fr"][index])+"]]";
    $("#interwikiDE").val(txt);

    //generate EN
    eventName = formatName(dictionary["en"][index]);
    $("#linkEN").attr("href", "http://wiki.guildwars2.com/wiki/"+eventName).html(eventName);
    $("#editEN").attr("href", "http://wiki.guildwars2.com/index.php?title="+eventName+"&action=edit").html( "Edit "+eventName);

    var txt  = "[[de:"+formatName(dictionary["de"][index])+"]]\n";
        txt += "[[es:"+formatName(dictionary["es"][index])+"]]\n";
        txt += "[[fr:"+formatName(dictionary["fr"][index])+"]]";
    $("#interwikiEN").val(txt);        

}

function formatName(name){
    //console.log(name);
    if (name.indexOf("Défi de héros") != -1 ) {
        name = name.replace("Défi de héros : ","");
        name = name.charAt(0).toUpperCase() + name.slice(1);
        //tmp.substring(0,5) + tmp.charAt(5).toUpperCase() + tmp.substring(6,tmp.length) <- capitialize french
    } 

    if (name.indexOf("Hero Challenge: ") != -1 ) {
        name = name.replace("Hero Challenge: ","");
        name = name.charAt(0).toUpperCase() + name.slice(1);
    }

    if (name.substr(name.length - 1) == ".") {
        return name.substring(0, name.length - 1);
    }
    else {return name}
}


/*add events*/
//select in textarea
$("#output").on("click", "textarea", function() {
    this.select();
});

$("#test1").on("click", "li", fillOutput);

function outputList(){	
 	$("#test1").empty();
 	i = eventsEN.Pairs.length;
 	while (i--) {
 		var pTag = "<p>";
 		pTag += '<a target="_blank" href="http://wiki.guildwars2.com/wiki/' + eventsEN.Pairs[i].name.substring(0, eventsEN.Pairs[i].name.length - 1) + '">'+ eventsEN.Pairs[i].name.substring(0, eventsEN.Pairs[i].name.length - 1) +'</a> <br />';
 		pTag += '<a target="_blank" href="http://wiki-'+ $('#slctLang').val()+'.guildwars2.com/wiki/' + events.Pairs[i].name.substring(0, events.Pairs[i].name.length - 1) + '">'+ events.Pairs[i].name.substring(0, events.Pairs[i].name.length - 1) +'</a> <br /> </p>';
 		$("#test1").append(pTag);
 	}
 }

function addEvents(){
  localStorage.clear();
  console.log("start");
  GW2Events = new GW2.EventNames();
}


 </script>	
     </body>
 </html>