Haladar / Auto conceito

// ==UserScript==
// @name         Auto conceito
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Automatiza o conceito do aluno no sistema SigEduca.
// @author       Denis Wilton de Paula Azevedo github.com/DennisWilton
// @match        *://sigeduca.seduc.mt.gov.br/ged/hwtgedlancaravaliacaoconcdesc*
// @grant        none
// @license      MIT
// ==/UserScript==

const button = document.createElement('button');
button.innerText = 'Teste';
button.style.position = `fixed`;
button.style.top = 0;
button.style.left = 0;
button.onclick = runScript;

const criteriosTbox = document.createElement('input');
criteriosTbox.placeholder = "Insira os critérios aqui, separados por vírgula";
criteriosTbox.style.position = `fixed`;
criteriosTbox.style.top = 20;
criteriosTbox.style.left = 0;

const btn2 = document.createElement('button');
btn2.innerText = 'Preencher B';
btn2.style.position = `fixed`;
btn2.style.top = 40;
btn2.style.left = 0;
btn2.onclick = preencherB;

const btnP = document.createElement('button');
btnP.innerText = 'Preencher P';
btnP.style.position = `fixed`;
btnP.style.top = 60;
btnP.style.left = 0;
btnP.onclick = preencherP;

const btnA = document.createElement('button');
btnA.innerText = 'Preencher A';
btnA.style.position = `fixed`;
btnA.style.top = 80;
btnA.style.left = 0;
btnA.onclick = preencherA;

function getElementByIdRegex(regex, element = document) {
    let output = [];
    for (let i of element.querySelectorAll('*')) {
        if (regex.test(i.id)) { // or whatever attribute you want to search
            output.push(i);
        }
    }
    return output;
}

var simulateMouseEvent = function(element, eventName, coordX, coordY) {
    element.dispatchEvent(new MouseEvent(eventName, {
        view: window,
        bubbles: true,
        cancelable: true,
        clientX: coordX,
        clientY: coordY,
        button: 0
    }));
};

function getElementByNameRegex(regex, element = document, filter = '') {
    let output = [];
    for (let i of element.querySelectorAll('*')) {
        if (regex.test(i.name)) { // or whatever attribute you want to search
            output.push(i);
        }
    }
    return output;
}

function wait(ml = 1000){
    return new Promise(res => {
        setTimeout( () => { res() }, ml);
    })
}

function checkCriterioValido(criterio, inverse = false){
    const listaCriterios = criteriosTbox.value.replace(/[^\d\,]/g, "").split(",");
    if (inverse) return !(listaCriterios.includes(criterio));
    return (listaCriterios.includes(criterio))
}

function preencherB(){
    limpaTudo();

    const LISTA_CHECKBOX_B = getElementByNameRegex(/vGRIDGEDAGRAVAPRMAVAB_\d+/);
    //console.log(LISTA_CHECKBOX_B);

    LISTA_CHECKBOX_B.forEach( async (checkbox, i) => {
        const criterio = checkbox.parentNode.parentNode.parentNode.children[0].innerText.trim();

        if(checkCriterioValido(criterio)) {
            checkbox.checked = true
            checkbox.value = 1
        }
    })

    preencherONT();
}


function preencherA(){
    limpaTudo();

    const LISTA_CHECKBOX_A = getElementByNameRegex(/vGRIDGEDAGRAVAPRMAVAA_\d+/);
    //console.log(LISTA_CHECKBOX_A);

    LISTA_CHECKBOX_A.forEach( async (checkbox, i) => {
        const criterio = checkbox.parentNode.parentNode.parentNode.children[0].innerText.trim();

        if(checkCriterioValido(criterio)) {
            checkbox.checked = true
            checkbox.value = 1
        }
    })

    preencherONT();
}


function preencherP(){
    limpaTudo();

    const LISTA_CHECKBOX_P = getElementByNameRegex(/vGRIDGEDAGRAVAPRMAVAP_\d+/);
    //console.log(LISTA_CHECKBOX_P);

    LISTA_CHECKBOX_P.forEach( async (checkbox, i) => {
        const criterio = checkbox.parentNode.parentNode.parentNode.children[0].innerText.trim();

        if(checkCriterioValido(criterio)) {
            checkbox.checked = true
            checkbox.value = 1
        }
    })

    preencherONT();
}

function limpaTudo(){
    const LISTA_CHECKBOX = getElementByNameRegex(/vGRIDGEDAGRAVAPRMAVA.*_\d+/);

    LISTA_CHECKBOX.forEach( async (checkbox, i) => {

        checkbox.checked = false
        checkbox.value = 0

    })

}

function preencherONT(){
    const LISTA_CHECKBOX_ONT = getElementByNameRegex(/vGRIDGEDAGRAVAPRMAVAONT_\d+/);
    //console.log(LISTA_CHECKBOX_B);

    LISTA_CHECKBOX_ONT.forEach( async (checkbox, i) => {
        const criterio = checkbox.parentNode.parentNode.parentNode.children[0].innerText.trim();
        const select = checkbox.parentNode.parentNode.parentNode.children[8].querySelector('select');

        select.value = 2;

        if(checkCriterioValido(criterio, true)) {
            checkbox.checked = true
            checkbox.value = 1
        }
    })
}

function runScript(){
    const GRID_CRITERIOS = getElementByIdRegex(/GridobjaprendizagemContainer_.*/)[0];
    const LISTA_CRITERIOS = getElementByIdRegex(/GridobjaprendizagemContainer_/, GRID_CRITERIOS);

    button.innerText = `${LISTA_CRITERIOS.length} critérios.`

    window.LISTA_CRITERIOS = LISTA_CRITERIOS;
}



//document.body.appendChild(button);
document.body.appendChild(btn2);
document.body.appendChild(btnP);
document.body.appendChild(btnA);
document.body.appendChild(criteriosTbox);