revoss7 / Task expandNEW

// ==UserScript==
// @name        Task expandNEW
// @namespace   revos
// @description Expand task in list
// @version     0.01
// @author      revos
// @include     http://wmmail.ru/index.php?cf=uzd-viewtask*
// @include     http://www.wmmail.ru/index.php?cf=uzd-viewtask*
// @license     MIT
// @run-at      document-end
// ==/UserScript==



function expand(id) {
    var comurl = "a[href='index.php?cf=uzd-commentzd&zdid="+id+"']";
    var el = document.getElementById("uzd"+id);
    if (el.nextSibling.id === "") {el.nextSibling.remove(); el.querySelector(comurl).nextSibling.innerHTML = "Развернуть";} else {
        function makeRequest (uzd) {
            return new Promise(function (resolve, reject) {
                var xhr = new XMLHttpRequest();
                xhr.open("GET", "/index.php?cf=uzd-readtask&zdid="+uzd);
                xhr.onload = function () {
                    if (this.status >= 200 && this.status < 300) {
                        resolve(xhr.response);
                    } else {
                        reject({
                            status: this.status,
                            statusText: xhr.statusText
                        });
                    }
                };
                xhr.onerror = function () {
                    reject({
                        status: this.status,
                        statusText: xhr.statusText
                    });
                };
                xhr.send();
            });
        }

        var tr = document.createElement("tr");

        var td = document.createElement("td");

        tr.appendChild(td);

        td.colSpan = 4

        makeRequest(id).then(res => {var div = document.createElement("div");
                                     function form() {
                                         var el = document.getElementById("uzd"+id).nextSibling;
                                         var form = el.querySelectorAll("form")[1];
                                         setTimeout(function() {function sendform(number) {
                                             return new Promise(function (resolve, reject) {var form = el.querySelectorAll("form")[1];
                                                                                            form.setAttribute("target", "_blank"); form.submit();
                                                                                            var data = new FormData(form);
                                                                                            var req = new XMLHttpRequest(); req.open("POST", "/index.php", true);
                                                                                            req.onload = resolve;
                                                                                            req.onerror = reject;
                                                                                            req.send(data);
                                                                                           });
                                         };sendform(2).then(e => {var lit = document.createElement("div"); lit.innerHTML = e.target.response; el.innerHTML = lit.querySelector("div.top_fon > div > table > tbody > tr > td > table:nth-child(4)").lastChild.firstChild.innerHTML; el.children[0].remove();el.children[1].remove();el.children[0].colSpan = 4;});}, 2000)

                                     }
                                     var code = "eval((" + form.toString().replace(/id/g, id)+")())";

                                     div.innerHTML = res;var task = div.querySelector("div > div.top_fon > div > table > tbody > tr > td > table:nth-child(4) > tbody > tr:nth-child(1) > td:nth-child(2) > table:nth-child(4) > tbody"); var ttask = task.querySelector('input[onclick="setInterval(function fresh() {location.reload();} , 1000);"]'); if(ttask) {ttask.setAttribute( "onClick", code );}td.appendChild(task); var line = document.createElement("tr");

                                     line.setAttribute("class", "line");

                                     td.firstChild.appendChild(line)

                                     var dots = document.createElement("td");

                                     dots.setAttribute("class", "nobdr");

                                     dots.colSpan = 4;

                                     line.appendChild(dots);});

        var comurl = "a[href='index.php?cf=uzd-commentzd&zdid="+id+"']";

        el.parentNode.insertBefore(tr, el.nextSibling);el.querySelector(comurl).nextSibling.innerHTML = "Свернуть";}}





function addLink(id) {var link = document.createElement('a');
                      var el = document.getElementById("uzd"+id);
                      var linkText = document.createTextNode("Развернуть");
                      link.appendChild(linkText);
                      link.title = "Развернуть";
                      link.href = "javascript:void(0);";
                      link.setAttribute("onclick", "expand("+id+")");

                      var comurl = "a[href='index.php?cf=uzd-commentzd&zdid="+id+"']";
                      el.querySelector(comurl).parentNode.insertBefore(link, el.querySelector(comurl).nextSibling);
                      link.setAttribute("style", "margin-left: 20px");}




var nodes = document.getElementsByClassName("col2");
var snodes = [...nodes].map(item => item.nextElementSibling);
if(nodes[0].previousElementSibling.id === "") {} else {snodes.unshift(nodes[0].previousElementSibling)}
var arr = [...nodes];
arr = [...arr].map((e, i) => i < arr.length ? [e, snodes[i]] : [e]).reduce((a, b) => a.concat(b));
if (arr[arr.length-1].id == "") {arr.pop()}

for (var i = 0; i < arr.length; i++) {
    if (arr[i].id !== "") {
        addLink(arr[i].id.substring(3)); }
}

function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad) {
        scriptNode.addEventListener ("load", runOnLoad, false);
    }
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
};

addJS_Node(expand);