Script para ies

Script que agrega funcionalidades al tid

Verze ze dne 19. 02. 2018. Zobrazit nejnovější verzi.

// ==UserScript==
// @name         Script para ies
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Script que agrega funcionalidades al tid
// @author       Falaz
// @match        http://tid.ies21.edu.ar/*
// @grant        none
// ==/UserScript==

(function() {
    var tiempoInicial = new Date();
    try{
        var navbar = document.getElementsByClassName("ui-grid-b");
        navbar[0].id = "Hola";
        var a = document.getElementById("Hola");
        a.classList.remove("ui-grid-b");
    }catch (Exception){
        var navbar = document.getElementsByClassName("ui-grid-c");
        navbar[0].id = "Hola";
        var a = document.getElementById("Hola");
        a.removeChild(a.childNodes[3]);

    }

    a.classList.add("ui-grid-c");
    var liBtn = document.createElement("li");
    liBtn.id = "botonchito";
    liBtn.classList.add("ui-block-d");

    var aChild = document.createElement("a");
    var aSpan1 = document.createElement("span");
    var aSpan2 = document.createElement("span");
    var aSpan2icon = document.createElement("span");
    aChild.classList.add("ui-btn");
    aChild.classList.add("ui-btn-inline");
    aChild.classList.add("ui-btn-up-a");

    aSpan1.classList.add("ui-btn-inner");
    aSpan2.classList.add("ui-btn-text");

    aSpan2icon.classList.add("ui-btn-text");
    aSpan2icon.innerText = "\n";

    aChild.id = "achild";
    aChild.href= "javascript:void(0)";
    aChild.onclick= function(){
        tiempoInicial = new Date();
        return false;
    };

    aSpan1.appendChild(aSpan2icon);
    aSpan1.appendChild(aSpan2);
    aChild.appendChild(aSpan1);
    liBtn.appendChild(aChild);
    a.appendChild(liBtn);

    // Calcular tiempo de lectura
    var todosLosP = document.getElementsByTagName("p");
    var texto = "";
    for(i = 0;i<todosLosP.length;i++){texto += (todosLosP[i].innerText);}
    primerBlanco = /^ /;
    ultimoBlanco = / $/;
    variosBlancos = /[ ]+/g;
    texto = texto.replace (variosBlancos," ");
    texto = texto.replace(primerBlanco,"");
    texto = texto.replace(ultimoBlanco,"");
    textoTroceado = texto.split(" ");

    numeroPalabras = textoTroceado.length;

    console.log("Palabras contadas: " + numeroPalabras);
    console.log("El tiempo estimado de lectura, se basa en unas 150 o 100 palabras por minuto");
    console.log("Tiempo inicial: " + tiempoInicial);
    aSpan2.innerText = Math.round(numeroPalabras / 170) + " - "+ Math.round(numeroPalabras/130) + " min aprox\n";
    var b = false;
    var time = setInterval(myTimer, 1000);

    function myTimer() {
        if(b){b=false;}else{b=true;}
        var c = new Date();
        var segundos = (c.getTime() - tiempoInicial.getTime()) /1000;
        var min = Math.floor(segundos / 60);
        segundos = Math.round(segundos % 60);
        if(b){
            aSpan2icon.innerText = min + ":" + segundos+ "\n";
        }else{
            aSpan2icon.innerText = min + " " + segundos + "\n";
        }
    }
        function replaceLinks(){
        var botones = document.getElementsByClassName('ui-btn');
        var linksPrincipal = document.getElementsByClassName('ui-link-inherit');
        for(i=0;i<linksPrincipal.length;i++){
            linksPrincipal[i].setAttribute("target","_parent");
        }
        for(i=0;i<botones.length;i++){
            botones[i].setAttribute("target","_parent");
        }
        console.log("links reemplazados");
    }

     function replaceMedia(){
        var medias = $('.textoc');
        var mediasLinks =  $('a');
        for (var i = 0; i < medias.length; i++) {
            if(medias[i].children){
                if(medias[i].children.length > 0 && medias[i].children[0] !== null){
                    var link;
                    var tipo = 'video';
                    link = medias[i].children[0].href;
                    var div = document.createElement('div');
                    replaceVideo(link, div, medias[i]);
                    div.id = 'media' + [i];
                    medias[i].appendChild(div);
                }
            }
            // $('#media' + [i]).load(link + '#containingBlock');
        }
        for(var i= 0;i<mediasLinks.length;i++){
            if(mediasLinks[i].href.match(/media/)){
                if(mediasLinks[i].attributes.getNamedItem('data-rel') == null){
                    //console.log(mediasLinks[i].href);
                    var link;
                    var tipo = 'object';
                    link = mediasLinks[i].href;
                    var div = document.createElement('div');
                    replaceVideo(link, div, mediasLinks[i]);
                    div.id = 'media' + [i];
                    mediasLinks[i].appendChild(div);
                }
               }
        }
    }

    function replaceVideo(link,div, medias){
        var xhr = new XMLHttpRequest();
        xhr.withCredentials = true;

        xhr.addEventListener("readystatechange", function () {
          if (this.readyState === 4) {
              //tomar el iframe del response. Para los videos de youtube.
              if(xhr.response.includes('iframe')){
                  //remove link and img from div

                  medias.children[0].remove();
                  var index = xhr.response.indexOf('<iframe');
                  var j = xhr.response.lastIndexOf('iframe>');
                  j+= 7;
                  var iframetext = xhr.response.substring(index,j);
                  var iframeSrc = iframetext.match(/src="([^"]+)"/);
                  var iframe = document.createElement('iframe');
                  iframe.src = iframeSrc[1];
                  iframe.height = 400;
                  iframe.width = 600;
                  div.appendChild(iframe);
                //   div.append("aquí estaba un video");
            }else if(xhr.response.includes('object')){
                console.log("Soy un object :D");
                medias.children[0].remove();
                var iObject = xhr.response.indexOf('<object');
                var jObject = xhr.response.indexOf('.swf"');
                //console.log(iObject, jObject);
                jObject+= 5;
                var objectText = xhr.response.substring(iObject,jObject);
                var objectData = objectText.match(/data="([^"]+)"/);
                var objectWidth = objectText.match(/width="([^"]+)"/);
                var objectHeight =objectText.match(/height="([^"]+)"/);
                var iframeObject = document.createElement('object');
                iframeObject.data = objectData[1];
                iframeObject.height = parseInt(objectHeight[1]) + 100;
                //if (objectWidth[1].match(/%/)){
                //    iframeObject.width = 600;
                //}else{
                    iframeObject.width = objectWidth[1];
                //}
                div.appendChild(iframeObject);
            }else{
                //console.log(xhr.response);
                //console.log("hay un elemento que no es un video, ni un sfw. Reporte esto asi lo implemento");
            }

          }
        });

        xhr.open("GET", link);
        xhr.send('cache-control');
    }
    setTimeout(replaceLinks(),1000);
    setTimeout(replaceMedia(),1000);
})();