LiveWorksheets Auto-Magic Solver

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

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         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);

})();