Greasy Fork

Greasy Fork is available in English.

岐黄天使刷课助手 - UI模块

岐黄天使刷课助手的用户界面模块,负责生成和管理控制面板及相关UI元素。

当前为 2025-05-24 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/537080/1594821/%E5%B2%90%E9%BB%84%E5%A4%A9%E4%BD%BF%E5%88%B7%E8%AF%BE%E5%8A%A9%E6%89%8B%20-%20UI%E6%A8%A1%E5%9D%97.js

// ==UserScript==
// @name         岐黄天使刷课助手 - UI模块
// @namespace    http://tampermonkey.net/qhtx-modules
// @version      1.3.0
// @description  岐黄天使刷课助手的用户界面模块,负责生成和管理控制面板及相关UI元素。
// @author       AI助手
// ==/UserScript==

// UI模块
(function() {
    'use strict';

    // 创建控制面板
    function createPanel() {
        // 检查是否在iframe中,如果是则不创建面板
        try {
            if (window.self !== window.top) {
                console.log('在iframe中,不创建控制面板');
                return;
            }
        } catch (e) {
            console.error('检查iframe状态出错:', e);
            return;
        }

        // 检查是否已经创建过面板,避免重复创建
        if (window.qh.panelCreated || document.querySelector('.qh-assistant-panel')) {
            // 如果面板已存在但不可见,则显示它
            const existingPanel = document.querySelector('.qh-assistant-panel');
            if (existingPanel && existingPanel.style.display === 'none') {
                existingPanel.style.display = 'block';
            }
            return;
        }

        const panel = document.createElement('div');
        panel.className = 'qh-assistant-panel';
        panel.id = 'qh-assistant-panel';
        panel.innerHTML = `
            <div class="qh-assistant-title">
                岐黄天使刷课助手 v1.3.0
                <span class="qh-assistant-minimize">_</span>
                <span class="qh-assistant-close">✖</span>
            </div>
            <div class="qh-assistant-content">
                <div><span>状态:</span> <span id="qh-status">初始化中...</span></div>
                <div><span>当前课程:</span> <span id="qh-current-course">无</span></div>
                <div><span>进度:</span> <span id="qh-progress">0%</span></div>
                <div class="qh-assistant-progress">
                    <div class="qh-assistant-progress-bar" id="qh-progress-bar"></div>
                </div>
            </div>
            <div class="qh-assistant-nav-btns">
                <button class="qh-assistant-nav-btn" id="qh-prev-btn" disabled>上一课</button>
                <button class="qh-assistant-nav-btn" id="qh-next-btn" disabled>下一课</button>
            </div>
            <button class="qh-assistant-btn" id="qh-toggle-btn">开始自动刷课</button>
            <button class="qh-assistant-btn" id="qh-get-questions-btn" style="background: linear-gradient(90deg, #FF9800, #F57C00);">获取题目和答案</button>
            <button class="qh-assistant-btn" id="qh-auto-answer-btn" style="background: linear-gradient(90deg, #E91E63, #C2185B);">自动答题</button>
            <button class="qh-assistant-btn" id="qh-chapter-exam-btn" style="background: linear-gradient(90deg, #9C27B0, #7B1FA2);">章节考试</button>
            <button class="qh-assistant-btn" id="qh-final-exam-btn" style="background: linear-gradient(90deg, #3F51B5, #303F9F);">结业考试</button>
            <button class="qh-assistant-btn" id="qh-question-manage-btn" style="background: linear-gradient(90deg, #009688, #00796B);">题库管理</button>
            <button class="qh-assistant-btn" id="qh-auto-flow-btn" style="background: linear-gradient(90deg, #FF5722, #E64A19);">自动化设置</button>
            <div id="qh-question-status" style="font-size: 12px; text-align: center; margin-top: 5px; color: rgba(255,255,255,0.7);">
                题库状态: ${window.qh.savedQuestionBank.length > 0 ? `已保存 ${window.qh.savedQuestionBank.length} 道题目` : '未保存题库'}
            </div>
        `;
        document.body.appendChild(panel);
        window.qh.panelCreated = true;

        // 绑定关闭按钮事件
        document.querySelector('.qh-assistant-close').addEventListener('click', function() {
            panel.style.display = 'none';
        });

        // 绑定最小化按钮事件
        document.querySelector('.qh-assistant-minimize').addEventListener('click', function() {
            panel.classList.toggle('minimized');
            if (panel.classList.contains('minimized')) {
                this.textContent = '+';
            } else {
                this.textContent = '_';
            }
        });

        // 绑定开始/暂停按钮事件
        document.getElementById('qh-toggle-btn').addEventListener('click', function() {
            // 调用toggleAutoLearn函数切换状态
            if (typeof toggleAutoLearn === 'function') {
                toggleAutoLearn();
            }
        });

        // 绑定上一课按钮事件
        document.getElementById('qh-prev-btn').addEventListener('click', function() {
            if (typeof navigateToPrevCourse === 'function') {
                navigateToPrevCourse();
            }
        });

        // 绑定下一课按钮事件
        document.getElementById('qh-next-btn').addEventListener('click', function() {
            if (typeof navigateToNextCourse === 'function') {
                navigateToNextCourse();
            }
        });

        // 绑定获取题目和答案按钮事件
        document.getElementById('qh-get-questions-btn').addEventListener('click', function() {
            if (typeof showQuestionPanel === 'function') {
                showQuestionPanel();
            }
        });

        // 绑定自动答题按钮事件
        document.getElementById('qh-auto-answer-btn').addEventListener('click', function() {
            if (typeof toggleAutoAnswer === 'function') {
                toggleAutoAnswer();
            }
        });

        // 绑定章节考试按钮事件
        document.getElementById('qh-chapter-exam-btn').addEventListener('click', function() {
            if (typeof showChapterExamPanel === 'function') {
                showChapterExamPanel();
            }
        });

        // 绑定结业考试按钮事件
        document.getElementById('qh-final-exam-btn').addEventListener('click', function() {
            if (typeof checkFinalExamStatus === 'function') {
                checkFinalExamStatus();
            }
        });

        // 绑定题库管理按钮事件
        document.getElementById('qh-question-manage-btn').addEventListener('click', function() {
            if (typeof showQuestionManagePanel === 'function') {
                showQuestionManagePanel();
            }
        });

        // 绑定自动化设置按钮事件
        document.getElementById('qh-auto-flow-btn').addEventListener('click', function() {
            if (typeof showAutoFlowSettingsPanel === 'function') {
                showAutoFlowSettingsPanel();
            }
        });
    };

    // 更新状态显示
    function updateStatus(status) {
        // 在控制台记录状态,确保在iframe中也能看到
        console.log('状态更新:', status);

        // 尝试更新UI状态
        const statusEl = document.getElementById('qh-status');
        if (statusEl) {
            statusEl.textContent = status;
        }

        // 如果在iframe中,尝试向父窗口发送消息
        try {
            if (window.self !== window.top) {
                window.parent.postMessage({
                    type: 'qh-status-update',
                    status: status
                }, '*');
            }
        } catch (e) {
            console.error('向父窗口发送状态更新失败:', e);
        }
    };

    // 更新当前课程显示
    function updateCurrentCourse(course) {
        const courseEl = document.getElementById('qh-current-course');
        if (courseEl) {
            if (course && course.trim()) {
                courseEl.textContent = course;
                console.log('更新当前课程:', course);
            } else {
                // 如果没有提供课程名称,尝试从页面中获取
                const detectedCourse = detectCurrentCourse();
                if (detectedCourse) {
                    courseEl.textContent = detectedCourse;
                    console.log('自动检测到当前课程:', detectedCourse);
                } else {
                    courseEl.textContent = '无';
                    console.log('未能检测到当前课程');
                }
            }
        }
    };

    // 从页面中检测当前课程 - 简化版
    function detectCurrentCourse() {
        try {
            // 1. 首先尝试从kcTitle元素获取(最优先)
            const kcTitleElement = document.getElementById('kcTitle');
            if (kcTitleElement && kcTitleElement.textContent.trim()) {
                const title = kcTitleElement.textContent.trim();
                console.log('从kcTitle元素获取课程:', title);
                return title;
            }

            // 2. 尝试从iframe中查找kcTitle元素
            const frames = document.querySelectorAll('iframe');
            for (const frame of frames) {
                try {
                    const frameDoc = frame.contentDocument || frame.contentWindow.document;
                    const frameTitleElement = frameDoc.getElementById('kcTitle');
                    if (frameTitleElement && frameTitleElement.textContent.trim()) {
                        const title = frameTitleElement.textContent.trim();
                        console.log('从iframe中的kcTitle元素获取课程:', title);
                        return title;
                    }
                } catch (e) {
                    console.error('无法访问iframe内容:', e);
                }
            }

            // 3. 如果从navigateToCourse函数中提取了视频ID和标题,使用该信息
            if (window.qh.lastExtractedVideoInfo) {
                console.log('使用上次提取的视频信息:', window.qh.lastExtractedVideoInfo);
                return window.qh.lastExtractedVideoInfo;
            }

            // 4. 尝试从当前活动的课程链接获取
            if (window.qh.courseList.length > 0 && window.qh.currentCourseIndex >= 0 && window.qh.currentCourseIndex < window.qh.courseList.length) {
                return window.qh.courseList[window.qh.currentCourseIndex].title;
            }

            return null;
        } catch (e) {
            console.error('检测当前课程出错:', e);
            return null;
        }
    }

    // 更新进度显示
    function updateProgress(progress) {
        const progressEl = document.getElementById('qh-progress');
        const progressBarEl = document.getElementById('qh-progress-bar');
        if (progressEl && progressBarEl) {
            progressEl.textContent = progress + '%';
            progressBarEl.style.width = progress + '%';
        }
    };

    // 更新按钮状态
    function updateButtonStatus() {
        // 获取当前状态,如果GM_getValue不可用,则使用localStorage
        let isRunning;
        if (typeof GM_getValue !== 'undefined') {
            isRunning = GM_getValue('qh-is-running', false);
        } else {
            isRunning = localStorage.getItem('qh-is-running') === 'true';
        }

        const btn = document.getElementById('qh-toggle-btn');
        if (btn) {
            btn.textContent = isRunning ? '暂停自动刷课' : '开始自动刷课';
            // 使用背景渐变而不是backgroundColor,以匹配CSS样式
            if (isRunning) {
                btn.style.background = 'linear-gradient(90deg, #f44336, #e53935)';
            } else {
                btn.style.background = 'linear-gradient(90deg, #4CAF50, #45a049)';
            }
            console.log('播放状态已更新:', isRunning ? '播放中' : '已暂停');
        }
    }

    // 导出模块函数
    window.ui = createPanel;
    window.createPanel = createPanel;
    window.updateStatus = updateStatus;
    window.updateCurrentCourse = updateCurrentCourse;
    window.updateProgress = updateProgress;
    window.updateButtonStatus = updateButtonStatus;

    // 通知主脚本模块已加载
    console.log('[模块加载] ui 模块已加载');
})();