LiveWorksheets Auto-Magic Solver

Detecta, preenche e salva automaticamente assim que a página carrega

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         LiveWorksheets Auto-Magic Solver
// @namespace    http://tampermonkey.net/
// @version      7.0
// @description  Detecta, preenche e salva automaticamente assim que a página carrega
// @author       User & Gemini
// @match        https://*.liveworksheets.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Função que faz o trabalho sujo
    function solveEverything() {
        if (typeof jQuery === 'undefined' || !jQuery("#worksheet-preview").worksheetPreview) return;

        console.log("🚀 Iniciando preenchimento automático...");

        // 1. Ativa o motor de respostas que você descobriu
        jQuery("#worksheet-preview").worksheetPreview("validation", {
            clicked: true, 
            showAnswers: true, 
            showRightAnswers: true
        });

        // 2. Busca o JSON de respostas
        let answers = [];
        document.querySelectorAll('script[type="application/json"]').forEach(s => {
            try {
                const json = JSON.parse(s.textContent);
                const raw = json?.worksheet?.json || json?.props?.pageProps?.worksheet?.json;
                if (raw) answers = JSON.parse(raw);
            } catch(e) {}
        });

        if (answers.length === 0) return;

        // 3. Preenche e força o salvamento em cada campo
        const elements = document.querySelectorAll('#worksheet-preview-elements > *');
        elements.forEach((el, i) => {
            const data = answers[i];
            if (!data) return;
            const val = data[0];

            if (el.tagName === 'INPUT' || el.hasAttribute('contenteditable')) {
                el.focus();
                if (el.hasAttribute('contenteditable')) el.innerText = val;
                else el.value = val;

                // Dispara os eventos para o site entender que houve digitação real
                ['input', 'change', 'blur', 'keyup'].forEach(evt => {
                    el.dispatchEvent(new Event(evt, { bubbles: true }));
                });
            } 
            else if (el.classList.contains('worksheet-select-div') && val === 'select:yes') {
                if (!el.classList.contains('worksheet-select-div-selected')) {
                    el.click();
                }
            }
        });

        console.log("✅ Tudo preenchido e salvo automaticamente!");
    }

    // Tenta executar a cada 1 segundo até encontrar os elementos
    const autoRun = setInterval(() => {
        const elements = document.querySelectorAll('#worksheet-preview-elements > *');
        if (elements.length > 0 && typeof jQuery !== 'undefined') {
            solveEverything();
            clearInterval(autoRun); // Para de tentar depois que conseguir
        }
    }, 1000);

})();