// ==UserScript==
// @name           DSO letzte Hoffnung
// @namespace      DSO letzter Atemzug
// @description    Mit dem DSO-Client spielen
// @author         Roni
// @grant          none
// @version        1.3.2
// @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 li4 = document.createElement ('li');
    li4.className = "sub ";
    li4.innerHTML = '<span></span><a id="clbutton" href="https://hagenbusch.de/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(li4);

var li1 = document.createElement ('li');
    li1.className = "active main ";
    li1.innerHTML = '<span></span><a id="clbutton" href="https://www.hagenbusch.de/forum" alt="Zum Forum Lostsettler" title="Zum Forum Lostsettler" target="_blank"><img src="https://forum.thesettlersonline.pl/images/settlers/editor/createlink.png" width="16" height="16" align="center" valign="center"></img> Forum lostsettler <img src="https://forum.thesettlersonline.pl/images/settlers/statusicon/forum_link-16.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="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(li2);

var li3 = document.createElement ('li');
    li3.className = "main";
    li3.innerHTML = '<div style="padding-top: 5px;"><img src="https://tso-pl-ssl.cdn.ubi.com/sites/default/themes/siedler/images/community/news/forward.png" width="10" height="20" align="left" valign="left"><alt="Starten Sie die UbiCollect-Anwendung und aktivieren Sie das Kontrollkästchen" title="Starten Sie die UbiCollect-Anwendung und aktivieren Sie das Kontrollkästchen"><input type="checkbox" id="highlight"><label for="highlight"><b>  Sammeldingens &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></b></font></label></div>';

//    li3.innerHTML = '<div style="padding-top: 5px;" alt="Starten Sie die UbiCollect-Anwendung und aktivieren Sie das Kontrollkästchen" title="Starten Sie die UbiCollect-Anwendung und aktivieren Sie das Kontrollkästchen"><input type="checkbox" id="highlight"><label for="highlight">  Suchdingens</label></div>';
    document.querySelector("#menu ul").appendChild(li3);

    var url = new URL(window.location.href);
    document.getElementById("clplay").onmouseover = function() {document.getElementById("clplay").style.color = "#ffffff"};
    document.getElementById("clplay").onmouseout = function() {document.getElementById("clplay").style.color = "#000000"};
    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 "(.*)"/g.exec(body))[1];
    //var tso = (/thisProgram: "(.*)"/g.exec(body))[1];
    if (!tso) {
	    alert("something went wrong play page");
	    return;
    }
      if(document.getElementById('highlight').checked == true) {
      tso = decodeURIComponent(tso).replace(/&s=(.*?)\|(.*?)&/,"&s=http://127.0.0.1:9000/$1|http://127.0.0.1:9000/$2&");
    }
    tso = tso.replace("bb=https", "bb=http", tso);
    window.location.href = "tso://"+tso+"&baseUri="+encodeURIComponent(url.origin);
    return;
    tso = tso.replace("bb=https", "bb=http", tso);
    window.location.href = "tso://"+tso+"&baseUri="+url.origin;
    return;
  });
})();