LiveWorksheets Auto-Magic Solver

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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

})();