Corona forms

kolaborace při vyplňování microsoft forms.

Versione datata 05/10/2020. Vedi la nuova versione l'ultima versione.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo 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         Corona forms
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  kolaborace při vyplňování microsoft forms.
// @author       sirluky
// @match        https://forms.office.com/*
// @match        http://forms.office.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const ServerHost = "https://coronaforms.lkovar.tk/";

    const fID = new URLSearchParams(window.location.search).get('id')
function odeslatOdpovedi(questionAnswers){
	const data = JSON.stringify({data: questionAnswers})

	fetch(ServerHost + "answers", {
	  "method": "POST",
	  "headers": {
	    "content-type": "application/json"
	  },
	  "body":data
	}).then(v=>v.json())
	.then(response => {
	  console.log(response, "odpovedi odeslany");
	})
	.catch(err => {
	  console.error(err);
	});
}

function ZiskejOdpovedinaOtazky(){
	const formID = new URLSearchParams(window.location.search).get('id')

    window.username = window.OfficeFormServerInfo.userInfo.DisplayName || localStorage.getItem('coronaforms_username') || ('Anonym ' + Math.floor(Math.random()*100000))
    localStorage.setItem('coronaforms_username', window.username)

    const username = window.username;
	const qcount = document.querySelectorAll('.office-form-body .office-form-question').length;
	let OtazkyAOdpovedi = [];
	for(let i = 1; i <= qcount; i++){
		const qnumber = i // cislo otazky 0..?
		// selector otazky
		let q = document.querySelector(`.office-form-body .office-form-question:nth-child(${qnumber})`)

		let answers = [];
		// ziskej vsechny zvolene odpovedi pro zvolenou otazku do answers
		q.querySelectorAll('.office-form-question-textbox, [aria-checked=true]').forEach(e => {
			answers.push(e.value)
		})
		let otazka = q.querySelector('.office-form-question-title :not([class])').textContent
		answers = answers.join(', ')
		OtazkyAOdpovedi.push({
	        "formID": formID,
	        "question": otazka,
	        "username": username,
	        "answer": answers
	    })
	}
	return OtazkyAOdpovedi;
}

    function cisloOtazkyProOdpoved(hledana){
        const qcount = document.querySelectorAll('.office-form-body .office-form-question').length;
        let OtazkyAOdpovedi = [];
        for(let i = 1; i <= qcount; i++){
            const qnumber = i // cislo otazky 0..?
            // selector otazky
            let q = document.querySelector(`.office-form-body .office-form-question:nth-child(${qnumber})`)

            let otazka = q.querySelector('.office-form-question-title :not([class])').textContent;
            if(otazka === hledana){
                return i
            }
        }
    }
    function smazproOtazku(qnumber){
        const e = document.querySelector(`.office-form-body .office-form-question:nth-child(${qnumber})`).querySelector('.choosed_optionbox')
        if(e){
           e.textContent = "";
           e.remove();
        }
    }

    function ZobrazOdpovediOstatnich_for_one_question(question,answers){
        const qnumber = cisloOtazkyProOdpoved(question)
        smazproOtazku(qnumber);

        let othersChoices = Object.entries(answers).map(([name,answer]) => ({name, choices:answer}))
        console.log(othersChoices)
        let othersChoicesDOM = document.createElement('ul')
        // rerender
        othersChoicesDOM.classList.add('choosed_optionbox') // class push .choosed_optionbox
        // othersChoicesDOM.remove()
        var h3 = document.createElement('h5')
        h3.textContent = "Odpovědi ostatních: "
        othersChoicesDOM.appendChild(h3);

        for(let choice of othersChoices){
                let o = document.createElement('li')
                o.style.paddingLeft = "30px"
                o.textContent = `${choice.name} - ${choice.choices}`
                othersChoicesDOM.appendChild(o);
        }


          document.querySelector(`.office-form-body .office-form-question:nth-child(${qnumber})`).appendChild(othersChoicesDOM);
        if(localStorage.getItem('displayAnswers') == 'true'){
            document.querySelectorAll('.choosed_optionbox').forEach(e => {
               e.style.display = "block";
            })
        }

    }

    function StahniOdpovediOstatnich(){
    	const formID = new URLSearchParams(window.location.search).get('id')

        const qcount = document.querySelectorAll('.office-form-body .office-form-question').length;
        let OtazkyAOdpovedi = [];
        for(let i = 1; i <= qcount; i++){
           const qnumber = i // cislo otazky 0..?
           // selector otazky
           let q = document.querySelector(`.office-form-body .office-form-question:nth-child(${qnumber})`)

           let otazka = q.querySelector('.office-form-question-title :not([class])').textContent;

           const data = JSON.stringify({
                "formID": formID,
                "question": otazka
           });

            fetch(ServerHost + "getanswers", {
                "method": "POST",
                "headers": {
                    "content-type": "application/json"
                },
                "body": data
            }).then(v=>v.json())
                .then(response => {

                ZobrazOdpovediOstatnich_for_one_question(response.question, response.answers)
                console.log(response,JSON.stringify(response));
            })
                .catch(err => {
                console.error(err);
            });
        }


    }
    setInterval(v =>{
        let questionAnswers = ZiskejOdpovedinaOtazky()
        odeslatOdpovedi(questionAnswers);
        StahniOdpovediOstatnich();
    },3000)
    let questionAnswers = ZiskejOdpovedinaOtazky()
    odeslatOdpovedi(questionAnswers);
    StahniOdpovediOstatnich();


const showbar = document.createElement("p");
showbar.style.position = "fixed"
showbar.style.top = "10px";
showbar.style.right = "20px"
showbar.style.zIndex = "100000000"
showbar.style.color="black"
showbar.style.fontSize="30px"
showbar.style.background="white"
showbar.innerText = "0"

function secondsToHms(d) {
    d = Number(d);
    var h = Math.floor(d / 3600);
    var m = Math.floor(d % 3600 / 60);
    var s = Math.floor(d % 3600 % 60);

    var hDisplay = h > 0 ? h.toString().padStart(2,'0') + (h == 1 ? ":" : ":") : "";
    var mDisplay = m > 0 ? m.toString().padStart(2,'0') + (m == 1 ? ":" : ":") : "00:";
    var sDisplay = s > 0 ? s.toString().padStart(2,'0') + (s == 1 ? "" : "") : "00";
    return hDisplay + mDisplay + sDisplay;
}

setInterval(()=>{
    localStorage.setItem('casvsec-' +fID, parseInt(localStorage.getItem('casvsec-' +fID)||0)+1);
    showbar.innerText = `Váš čas: ${secondsToHms(localStorage.getItem('casvsec-' +fID))}`;
},1000)
// TODO - auto reset


  document.body.appendChild(showbar);
  document.onkeyup = function(e) {
    if (e.ctrlKey && e.which == 66) {
      localStorage.setItem('displayAnswers',localStorage.getItem('displayAnswers') === "true" ? "false" : "true")
      if(localStorage.getItem('displayAnswers') === "true"){
        showbar.style.display = "block"
       document.querySelectorAll('.choosed_optionbox').forEach(e => {
            e.style.display = "block";
        })

      } else {
        showbar.style.display = "none"
       document.querySelectorAll('.choosed_optionbox').forEach(e => {
            e.style.display = "none";
        })

      }
    }
  };
})();