// ==UserScript==
// @name           TSOlastBreath
// @namespace      TSOlastBreath
// @description    Allow run TSO in old client
// @author         Sin aka SirriS & Brutus
// @grant          none
// @version        1.6
// @include        https://www.thesettlersonline.com.br/*
// @include        https://www.thesettlersonline.com/*
// @include        https://www.thesettlersonline.net/*
// @include        https://www.thesettlersonline.fr/*
// @include        https://www.thesettlersonline.cz/*
// @include        https://www.diesiedleronline.de/*
// @include        https://www.thesettlersonline.gr/*
// @include        https://www.thesettlersonline.it/*
// @include        https://www.juego-thesettlersonline.com/*
// @include        https://www.thesettlersonline.nl/*
// @include        https://www.thesettlersonline.pl/*
// @include        https://www.thesettlersonline.ro/*
// @include        https://www.thesettlersonline.ru/*
// @include        https://www.thesettlersonline.es/*
// ==/UserScript==

(function() {
  var li = document.createElement ('li');
      li.className = "promotion first ";
       li.innerHTML = '<span></span><a id="clplay" href="#" alt="Spielen Sie mit dem DSO-Client" title="Spielen Sie mit dem DSO-Client"><img src="https://tso-pl-ssl.cdn.ubi.com/sites/default/themes/siedler/images/community/news/forward.png" width="10" height="20" align="center" valign="center"></img><b>&nbsp Spiele mit Client &nbsp<img src="https://tso-pl-ssl.cdn.ubi.com/sites/default/themes/siedler/images/community/news/backward.png" width="10" height="20" align="center" valign="center"></img></font></b></a>';
  if(document.querySelector(".jsInstance-logout") == null)
    return;
  document.querySelector("#menu ul").appendChild(li);
//
  var li1 = document.createElement ('li');
    li1.className = "sub ";
    li1.innerHTML = '<span></span><a id="clplay" href="https://sammlerworld.de/files/client.swf" alt="Download Datei" title="Download Datei"><img src="https://forum.thesettlersonline.pl/images/settlers/statusicon/thread_lock-16.png" width="20" height="16" align="center" valign="center"></img>Neue client.swf <img src="https://forum.thesettlersonline.pl/images/settlers/misc/paperclip.png" width="16" height="16" align="center" valign="center"></img></font></b></a>';
    document.querySelector("#menu ul").appendChild(li1);
//
  var li2 = document.createElement ('li');
    li2.className = "promotion first ";
    li2.innerHTML = '<span></span><a id="clbutton" href="https://dso-karten.com" alt="Taktikkarten" title="Taktikkarten"><img src="https://forum.thesettlersonline.pl/images/settlers/statusicon/thread_lock-16.png" width="20" height="16" align="center" valign="center"></img>Taktikkarten <img src="https://forum.thesettlersonline.pl/images/settlers/misc/paperclip.png" width="16" height="16" align="center" valign="center"></img></font></b></a>';
    document.querySelector("#menu ul").appendChild(li2);
//
  var li4 = document.createElement ('li');
    li4.className = "promotion first ";
    li4.innerHTML = '<span></span><a id="clbutton" href="https://sammlerworld.de" alt="Zur Clientseite" title="Client-Seite"><img src="https://forum.thesettlersonline.pl/images/settlers/statusicon/thread_lock-16.png" width="20" height="16" align="center" valign="center"></img>Zur Client-Seite <img src="https://forum.thesettlersonline.pl/images/settlers/misc/paperclip.png" width="16" height="16" align="center" valign="center"></img></font></b></a>';
    document.querySelector("#menu ul").appendChild(li4);



  var url = new URL(window.location.href);
  document.getElementById("clplay").addEventListener("click", async () => {
    var playNowUrl = (/playNowforwardUrl = "(.+)";/g.exec(document.body.innerHTML))[1];
    if (!playNowUrl) {
        alert("something went wrong playNowforwardUrl");
        return;
    }
    var request = await fetch(url.origin + playNowUrl, {
            method: 'GET',
            credentials: 'include',
            redirect: 'manual'
        });
    var body = await request.text();
    var tso = (/return "(lang.*)"/g.exec(body))[1];
    if (!tso) {
	    alert("something went wrong play page");
	    return;
    }
    window.location.href = "tso://"+tso+"&baseUri="+encodeURIComponent(url.origin);
    return;
  });
})();