Corona forms

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

当前为 2020-10-09 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Corona forms
// @namespace    http://tampermonkey.net/
// @version      0.3.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";
            })
        } else {
            document.querySelectorAll('.choosed_optionbox').forEach(e => {
               e.style.display = "none";
            })
        }

    }

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

        const qcount = document.querySelectorAll('.office-form-body .office-form-question').length;
        let OtazkyAOdpovedi = [];
        let data = [];
        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 one_data = {
                "formID": formID,
                "question": otazka
           };
           data.push(one_data);
        }
            fetch(ServerHost + "getanswers", {
                "method": "POST",
                "headers": {
                    "content-type": "application/json"
                },
                "body": JSON.stringify({data:data})
            }).then(v=>v.json())
                .then(response => {
                console.log(response)
                for(let one_response of response){
                  ZobrazOdpovediOstatnich_for_one_question(one_response.question, one_response.answers)
                  console.log(one_response,JSON.stringify(one_response));
                }
            }).catch(err => {
                console.error(err);
            });
    }
    setInterval(v =>{
        let questionAnswers = ZiskejOdpovedinaOtazky()
        odeslatOdpovedi(questionAnswers);
        StahniOdpovediOstatnich();
    },1000)
    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";
        })

      }
    }
  };

      if(localStorage.getItem('displayAnswers') === "true"){
       showbar.style.display = "block"
      } else {
       showbar.style.display = "none"
      }

})();