Naurok Auto Copy & Notify (Optimized Version)

Автокопирование вопросов, ответов и изображений без лагов

目前為 2025-02-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Naurok Auto Copy & Notify (Optimized Version)
// @namespace    http://tampermonkey.net/
// @version      2.3
// @description  Автокопирование вопросов, ответов и изображений без лагов
// @author       ENDERVANO
// @license MIT
// @match        *://naurok.com.ua/test/testing/*
// @grant        GM_setClipboard
// ==/UserScript==

(function () {
    'use strict';

    let lastQuestionText = ""; // Храним последний вопрос, чтобы не копировать дублирующийся

    function addCopyButton() {
        let questionContainer = document.querySelector('.test-content-text');
        if (!questionContainer) return;

        let questionElem = document.querySelector('.test-content-text-inner');
        let question = questionElem ? questionElem.innerText.trim() : "Вопрос не найден";

        if (question === lastQuestionText) return; // Если вопрос не изменился — не перерисовываем кнопку
        lastQuestionText = question;

        let oldButton = document.querySelector("#copyQuestionButton");
        if (oldButton) oldButton.remove();

        let imageElem = document.querySelector('.test-content-image img');
        let imageUrl = imageElem ? imageElem.src : null;
        let imageText = imageUrl ? `\n[Изображение вопроса: ${imageUrl}]` : "";

        let answerElems = document.querySelectorAll('.question-option-inner');
        let answers = [];
        let correctAnswers = [];

        answerElems.forEach(el => {
            let textElem = el.querySelector('.question-option-inner-content');
            let imageElem = el.querySelector('.question-option-image');

            let text = textElem ? textElem.innerText.trim() : "";
            let answerImage = imageElem ? imageElem.style.backgroundImage.replace(/url\(["']?(.*?)["']?\)/, '$1') : null;

            if (text || answerImage) {
                let answerText = text ? text : `[Изображение ответа: ${answerImage}]`;
                answers.push(answerText);
                if (el.classList.contains('correct')) {
                    correctAnswers.push(answerText);
                }
            }
        });

        if (!questionElem || answers.length === 0) return;

        let copyText = `❓ Вопрос: ${question}${imageText}\n\n🔹 Варианты ответов:\n${answers.join("\n")}`;
        if (correctAnswers.length > 0) {
            copyText += `\n\n✅ Правильные ответы:\n${correctAnswers.join("\n")}`;
        }

        let copyButton = document.createElement("button");
        copyButton.id = "copyQuestionButton";
        copyButton.innerText = "📋 Скопировать вопрос";
        copyButton.style.cssText = `
            padding: 12px 20px;
            margin-top: 15px;
            width: 100%;
            background: #007BFF;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            text-align: center;
            transition: all 0.3s ease-in-out;
            box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
        `;

        copyButton.onmouseover = () => copyButton.style.background = "#0056b3";
        copyButton.onmouseout = () => copyButton.style.background = "#007BFF";

        copyButton.onclick = function () {
            GM_setClipboard(copyText, "text");
            showNotification("✅ Вопрос скопирован!", imageUrl);
        };

        questionContainer.appendChild(copyButton);
    }

    function showNotification(message, imageUrl) {
        let existingNotification = document.querySelector("#copyNotification");
        if (existingNotification) existingNotification.remove();

        let notification = document.createElement("div");
        notification.id = "copyNotification";
        notification.style.cssText = `
            position: fixed;
            top: 20px;
            right: 20px;
            background: white;
            color: black;
            padding: 15px 20px;
            border-radius: 10px;
            box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2);
            display: flex;
            align-items: center;
            font-size: 16px;
            font-weight: bold;
            z-index: 9999;
            opacity: 0;
            transform: translateX(50px);
            transition: opacity 0.3s ease, transform 0.3s ease;
        `;

        let icon = document.createElement("img");
        icon.src = "https://cdn-icons-png.flaticon.com/512/190/190411.png";
        icon.style.cssText = "width: 30px; height: 30px; margin-right: 10px;";

        let text = document.createElement("span");
        text.innerText = message;

        notification.appendChild(icon);
        notification.appendChild(text);

        if (imageUrl) {
            let image = document.createElement("img");
            image.src = imageUrl;
            image.style.cssText = "width: 40px; height: 40px; margin-left: 10px; border-radius: 5px;";
            notification.appendChild(image);
        }

        document.body.appendChild(notification);

        setTimeout(() => {
            notification.style.opacity = "1";
            notification.style.transform = "translateX(0)";
        }, 50);

        setTimeout(() => {
            notification.style.opacity = "0";
            notification.style.transform = "translateX(50px)";
            setTimeout(() => notification.remove(), 300);
        }, 3000);
    }

    function observeChanges() {
        const observer = new MutationObserver(() => {
            addCopyButton();
        });

        observer.observe(document.querySelector('.question-container'), { childList: true, subtree: true });
    }

    function main() {
        setTimeout(() => {
            addCopyButton();
            observeChanges();
        }, 3000);
    }

    main();
})();