UseDesk Button

Usedesk QoL improvments

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         UseDesk Button
// @namespace    http://tampermonkey.net/
// @version      1.3.2
// @description  Usedesk QoL improvments
// @author       Jonty
// @match        https://www.tampermonkey.net/index.php?version=4.13.6138&ext=iikm&updated=true
// @icon         https://bcg-hub.herokuapp.com/static/extension%20logo.jpg
// @grant        none
// @include      https://secure.usedesk.ru/tickets/*
// ==/UserScript==

var sender;

(function() {
    'use strict';

    // Your code here...
	var counterBtn = 0;
	var counterTxt = 0;
    var payload;
    var attachment = '';

    function postDataToWebhook(data, author, theme, email, attachment){
        //url to webhook
        const webHookUrl="https://bcg-hub.herokuapp.com/usedeskresender";
        //https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
        var oReq = new XMLHttpRequest();
        var myJSONStr = payload ={
            "origin": "*",
            "text": data,
            "theme": theme,
            "author": author,
            "mail": email,
            "attach": attachment,
        };

        //register method called after data has been sent method is executed
        oReq.addEventListener("load", reqListener);
        oReq.open("POST", webHookUrl, true);
        oReq.setRequestHeader('Content-Type', 'application/json');
        oReq.send(JSON.stringify(myJSONStr));
    }

    function respResult(id, resp, name){
        //url to webhook
        const webHookUrl="https://bcg-hub.herokuapp.com/autoreplyhandler";
        //https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
        var oReq = new XMLHttpRequest();
        var myJSONStr = payload ={
            "origin": "*",
            "id": id,
            "result": resp,
            "name": name,
        };

        //register method called after data has been sent method is executed
        oReq.addEventListener("load", reqListener);
        oReq.open("POST", webHookUrl, true);
        oReq.setRequestHeader('Content-Type', 'application/json');
        oReq.send(JSON.stringify(myJSONStr));
    }

    //callback method after webhook is executed
    function reqListener () {
        console.log(this.responseText);
    }

    let txt = document.querySelectorAll('.horizontal-scroll');
	txt.forEach(function(userItem) {
		userItem.setAttribute('id', 'txt' + counterTxt);
		counterTxt += 1;
	});

	let div = document.querySelectorAll('.rating');
	div.forEach(function(userItem) {
		let btn = userItem.parentNode;
  		btn.innerHTML += '<div class="mail-sender" style="width: 130px; padding: 0px; padding-right: 10px; vertical-align: middle;"><button id=' + counterBtn + ' onclick="sender(this.id)" class="btn-helper" style="border-radius: 3px; border: 1px solid transparent; padding: 6px; width: 130px; background-color: #00a651; color: #ffffff">Отправить в HR чат</button></div>';
		counterBtn += 1;
    });

    /*Тест окончен
    let id = window.location.href.slice(34)
    let comCheckTextDiv = document.querySelectorAll('.mail-text-private');
    comCheckTextDiv.forEach(function(userItem) {
        let comCheckTestStr = 'Рекомендуемый ответ';
        if (userItem.textContent.indexOf(comCheckTestStr) > 0) {
            let comCheckBtnPlace = userItem.previousSibling.previousSibling.firstChild.nextSibling.insertAdjacentHTML("afterend", '<div class="mail-sender" style="width:600px; padding:0px; padding-right:10px; padding-bottom:10px; vertical-align:middle; display:inline-block;"><p id="respQ">Подходящий вариант имени?</p><button id="good '+id+'" class="btn-success btn" onclick="resp(this.id)">Хорошо</button><button id="bad '+id+'" class="btn-danger btn" onclick="resp(this.id)">Плохо</button></div>');
            console.log(comCheckBtnPlace);
        }
    });

    window.resp = function (btnID) {
        let preTicketID = btnID;
        let ticketID = preTicketID.slice(-8);
        let result = preTicketID.slice(0, -8);
        console.log(result);
        let sourceBlock = document.getElementById(btnID).parentNode.parentNode.nextSibling.nextSibling.textContent;
        let re = /Добрый день, (.*)!/g;
        let name = sourceBlock.match(re)[0];
        respResult(ticketID, result, name);
    };
    */


    window.sender = function (btnID) {
        let textID = 'txt' + btnID;
        let message = document.getElementById(textID).innerHTML;
        let parentOfBtn = document.getElementById(btnID).parentNode;
        let parentOfPOB = parentOfBtn.parentNode.innerHTML;
        let preMail = document.getElementsByClassName('change-contact');
        let mail = preMail[0].getAttribute('data-value');
        //let childOfCOPOPOB = childOfPOPOB.;

        let theme = document.getElementsByClassName('ticket-title')[0].textContent

        let parentOfMsg = document.getElementById(textID).parentNode;
        let potentialAttach = parentOfMsg.nextElementSibling;
        console.log(potentialAttach);
        if (potentialAttach != null) {
            if (potentialAttach.className === 'mail-attachments  ') {
                let attach = potentialAttach.querySelectorAll('.attach-image-link');
                attach.forEach(function(userItem) {
                    let attachmentLink = userItem.getAttribute('href');
                    attachment += attachmentLink + '\n';
                });
            }
        }

        console.log(message);
        console.log(mail);
        console.log(attachment);
        console.log(theme);
        postDataToWebhook(message, parentOfPOB, theme, mail, attachment);
        attachment = '';
    };

    function blockRemover () {
        let stringCheck = "D2D ID";
        let checker = 0
        let targetMessage = document.getElementsByClassName('mail-text');
        for (let text_block of targetMessage) {
            if (text_block.textContent.indexOf(stringCheck) > 0) {
                checker++;
            }
        }
        console.log(checker)
        if (checker == 0) {
            let block = document.querySelector('[data-block-id="9912"]');
            block.remove();
        }
    }

    window.onload = blockRemover;
})();