LiveWorksheets Auto-Magic Solver

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

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

})();