Greasy Fork 支持简体中文。

DTUTOOL

Xem điểm học phần Duy Tân & Giải Captcha & Tự động đánh giá & Giải Captcha Đăng Ký

// ==UserScript==
// @name         DTUTOOL
// @namespace    https://mydtu.duytan.edu.vn/
// @version      0.5
// @description  Xem điểm học phần Duy Tân & Giải Captcha & Tự động đánh giá & Giải Captcha Đăng Ký
// @author       David Hua
// @match        *://mydtu.duytan.edu.vn/sites/index.aspx?p=home_grading_public_grade*
// @match        *://mydtu.duytan.edu.vn/Signin.aspx
// @match        *://mydtu.duytan.edu.vn/sites/index.aspx?p=home_ratingform*
// @match        *://mydtu.duytan.edu.vn/sites/index.aspx?p=home_registeredall*
// @grant        GM_addStyle
// @icon         https://mydtu.duytan.edu.vn/images/DTU.ICO
// ==/UserScript==

(function() {
    'use strict';

    // Thêm CSS cho hiệu ứng solving
    const css = `
        #txtCaptcha.solving,
        #ctl00_PlaceHolderContentArea_ctl00_ctl01_txtCaptcha.solving,
        input.txt.txt-vn.solving{
            background-image: url("https://mydtu.duytan.edu.vn/images/ajax-loader1.gif") !important;
            background-position: right 5px center !important;
            background-repeat: no-repeat !important;
            background-size: 16px 16px !important;
        }
    `;

    // Thêm style vào document
    const style = document.createElement('style');
    style.textContent = css;
    document.head.appendChild(style);

    let lastCaptchaUrl = '';

    function getCaptchaUrl() {
        // Try to get captcha URL from the registration page
        let captchaImage = document.querySelector('#imgCapt');
        if (captchaImage) {
            console.log("Registration Captcha URL:", captchaImage.src);
            return captchaImage.src;
        }

        // Fallback to the old captcha URL if not on the registration page
        captchaImage = document.querySelector('.floatbox img');
        if (captchaImage) {
            console.log("Old Captcha URL:", captchaImage.src);
            return captchaImage.src;
        } else {
            console.error("Không tìm thấy ảnh CAPTCHA.");
            return null;
        }
    }

    async function getCaptchaImage() {
        const captchaUrl = getCaptchaUrl();
        if (!captchaUrl) return null;

        if (captchaUrl !== lastCaptchaUrl) {
            lastCaptchaUrl = captchaUrl;
            const response = await fetch(captchaUrl);
            if (!response.ok) {
                console.error("Không thể tải ảnh CAPTCHA:", response.statusText);
                return null;
            }

            const blob = await response.blob();
            console.log("CAPTCHA Blob Size:", blob.size);

            return blob;
        }

        console.log("URL CAPTCHA không thay đổi.");
        return null;
    }

    async function uploadCaptchaImage(blob, inputElement) {
        // Thêm class solving vào input
        if (inputElement) {
            inputElement.classList.add('solving');
            inputElement.value = '';
        }

        try {
            const formData = new FormData();
            formData.append('file', blob, 'captcha.jpg');

            const response = await fetch('https://tpminer107.pythonanywhere.com/', {
                method: 'POST',
                body: formData
            });

            if (!response.ok) {
                console.error("Upload response error:", response.statusText);
                throw new Error("Upload CAPTCHA không thành công.");
            }

            const result = await response.json();
            console.log("Upload response:", result);
            return result.result || result.error;
        } finally {
            // Xóa class solving sau khi hoàn thành
            if (inputElement) {
                inputElement.classList.remove('solving');
            }
        }
    }

    async function handleCaptcha(inputId) {
        try {
            const captchaInput = document.getElementById(inputId);
            const captchaImageBlob = await getCaptchaImage();
            if (!captchaImageBlob) {
                console.error('Không thể lấy ảnh CAPTCHA.');
                return;
            }

            const captchaResult = await uploadCaptchaImage(captchaImageBlob, captchaInput);
            if (!captchaResult) {
                console.error('Giải CAPTCHA không thành công.');
                return;
            }

            console.log('Kết quả CAPTCHA:', captchaResult);

            if (captchaInput) {
                captchaInput.value = captchaResult;
            }
        } catch (error) {
            console.error('Có lỗi xảy ra:', error);
        }
    }

    // Override the original LoadCaptcha function
    if (typeof LoadCaptcha === 'function') {
        const originalLoadCaptcha = LoadCaptcha;
        LoadCaptcha = function() {
            originalLoadCaptcha.apply(this, arguments); // Call the original function

            const captchaInputId = 'ctl00_PlaceHolderContentArea_ctl00_ctl01_txtCaptchar';
            const captchaImageSelector = '#imgCapt';
            const loadCheckInterval = setInterval(() => {
                const captchaImage = document.querySelector(captchaImageSelector);
                if (captchaImage && captchaImage.src) {
                    clearInterval(loadCheckInterval);
                    console.log("Captcha đã được tải (LoadCaptcha), tiến hành giải...");
                    handleCaptcha(captchaInputId);
                }
            }, 500);
        };
    }

    function checkOption(radioId) {
        const radio = document.getElementById(radioId);
        if (radio) {
            radio.checked = true;
        }
    }

    function autoFillRatingForm() {
        // Điền "Không có ý kiến" cho các câu hỏi từ 48-51
        for (let i = 48; i <= 51; i++) {
            const element = document.getElementById("R" + i);
            if (element) {
                element.value += "Không có ý kiến";
            }
        }

        // Check các radio button từ 0-47
        for (let i = 0; i <= 47; i++) {
            checkOption("R" + i + "A");
        }

        // Cuộn xuống cuối trang
        window.scrollTo(0, document.body.scrollHeight);

        // Xử lý CAPTCHA cho form đánh giá
        handleCaptcha('ctl00_PlaceHolderContentArea_ctl00_ctl01_txtCaptcha');
    }

    // Xử lý theo từng trang
    if (window.location.href === 'https://mydtu.duytan.edu.vn/Signin.aspx') {
        handleCaptcha('txtCaptcha');

        const loginButton = document.getElementById('btnLogin1');
        if (loginButton) {
            loginButton.addEventListener('click', function() {
                const loginCheckInterval = setInterval(() => {
                    const captchaImage = document.querySelector('.floatbox img');
                    if (captchaImage && captchaImage.src) {
                        clearInterval(loginCheckInterval);
                        handleCaptcha('txtCaptcha');
                    }
                }, 500);
            });
        }

        document.addEventListener('keypress', function(event) {
            if (event.key === 'Enter') {
                const enterCheckInterval = setInterval(() => {
                    const captchaImage = document.querySelector('.floatbox img');
                    if (captchaImage && captchaImage.src) {
                        clearInterval(enterCheckInterval);
                        handleCaptcha('txtCaptcha');
                    }
                }, 500);
            }
        });
    } else if (window.location.href.includes('mydtu.duytan.edu.vn/sites/index.aspx?p=home_ratingform')) {
        // Tự động điền form đánh giá
        autoFillRatingForm();
    } else if (window.location.href.includes('mydtu.duytan.edu.vn/sites/index.aspx?p=home_grading_public_grade')) {
        const csvUrl = 'https://docs.google.com/spreadsheets/d/1RL30B0GkoiJcSYeABZCtUvIoWduxegF0/export?format=csv';

        async function fetchCsvData(url) {
            const response = await fetch(url);
            const text = await response.text();
            return text.split('\n').map(row => row.split(','));
        }

        async function updateTable() {
            const csvData = await fetchCsvData(csvUrl);
            const dataMap = {};

            csvData.forEach(row => {
                const [key, value] = row;
                dataMap[key.trim()] = value.trim();
            });

            const table = document.getElementById('frmNhapDiem');
            if (!table) return;

            const rows = table.getElementsByTagName('tbody')[0].getElementsByTagName('tr');

            for (let row of rows) {
                const cells = row.getElementsByTagName('td');
                if (cells.length > 2) {
                    const id = cells[1].innerText.trim();
                    if (dataMap[id]) {
                        cells[2].innerText = dataMap[id];
                    }
                }
            }
        }

        updateTable();
    } else if (window.location.href.includes('mydtu.duytan.edu.vn/sites/index.aspx?p=home_registeredall')) {
        // Initial handling for registration page load with setInterval check
        const captchaInputId = 'ctl00_PlaceHolderContentArea_ctl00_ctl01_txtCaptchar';
        const captchaImageSelector = '#imgCapt';
        const initialCheckInterval = setInterval(() => {
            const captchaImage = document.querySelector(captchaImageSelector);
            if (captchaImage && captchaImage.src) { // Kiểm tra ảnh tồn tại và có src
                clearInterval(initialCheckInterval);
                console.log("Captcha đã có sẵn khi tải trang (Initial), tiến hành giải...");
                handleCaptcha(captchaInputId);
            }
        }, 500);
    }
})();