超星学习通智能阅读助手-增强版

智能滚动、自适应翻页、状态监控的阅读时长脚本,支持异常处理和用户配置

目前為 2025-03-07 提交的版本,檢視 最新版本

// ==UserScript==
// @name         超星学习通智能阅读助手-增强版
// @namespace    https://mooc1.chaoxing.com/
// @version      2.0
// @description  智能滚动、自适应翻页、状态监控的阅读时长脚本,支持异常处理和用户配置
// @author       AI Assistant Pro
// @license      MIT
// @match        https://mooc1.chaoxing.com/*
// @grant        GM_addStyle
// @grant        GM_notification
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js
// ==/UserScript==

(function() {
'use strict';

// 用户配置区
const CONFIG = {
    scrollSpeed: 3500,    // 滚动间隔(毫秒)
    pageCheckInterval: 15, // 翻页检测间隔(秒)
    scrollStep: 0.85,     // 每次滚动页面高度的百分比(0-1)
    randomFactor: 0.3,    // 随机化系数(0-1)
    maxRetries: 3,        // 最大重试次数
    debugMode: false      // 调试模式
};

// 运行时状态
let state = {
    active: true,
    currentPage: 1,
    retryCount: 0,
    startTime: Date.now(),
    observers: [],
    timers: []
};

// 样式注入
GM_addStyle(`
    #reading-helper-panel {
        position: fixed;
        top: 20px;
        right: 20px;
        z-index: 9999;
        background: rgba(40, 40, 40, 0.9);
        color: #fff;
        padding: 15px;
        border-radius: 8px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.2);
        font-family: Arial, sans-serif;
        min-width: 220px;
    }
    .helper-btn {
        background: #4CAF50;
        border: none;
        color: white;
        padding: 8px 16px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 14px;
        margin: 4px 2px;
        cursor: pointer;
        border-radius: 4px;
        transition: all 0.3s;
    }
    .helper-btn:hover {
        background: #45a049;
    }
`);

// 创建控制面板
function createControlPanel() {
    const panel = document.createElement('div');
    panel.id = 'reading-helper-panel';
    panel.innerHTML = `
        <h3 style="margin:0 0 10px 0; font-size:16px;">📖 阅读助手 v2.0</h3>
        <div>状态:<span id="helper-status">运行中</span></div>
        <div>已阅读:<span id="helper-pages">0</span> 页</div>
        <div>运行时间:<span id="helper-duration">00:00:00</span></div>
        <button class="helper-btn" id="helper-toggle">暂停</button>
        <button class="helper-btn" id="helper-reset">重置统计</button>
    `;
    document.body.appendChild(panel);

    // 事件绑定
    $('#helper-toggle').click(toggleScript);
    $('#helper-reset').click(resetStats);
}

// 智能滚动函数
function smartScroll() {
    const currentPosition = window.scrollY;
    const maxScroll = document.documentElement.scrollHeight - window.innerHeight;
    const step = window.innerHeight * CONFIG.scrollStep * (1 + (Math.random() * CONFIG.randomFactor - CONFIG.randomFactor/2));

    if (currentPosition + step >= maxScroll) {
        window.scrollTo(0, maxScroll);
        log('到达页面底部,准备翻页');
        return true;
    }

    window.scrollBy({
        top: step,
        behavior: 'smooth'
    });
    return false;
}

// 增强版翻页检测
async function checkNextPage() {
    try {
        const pageButtons = [
            '.nextBtn',         // 标准下一页按钮
            '.ml10[href^="javascript"]', // 分页按钮
            '.btn[onclick*="nextPage"]', // 事件触发按钮
            'a:contains("下一页")'       // 文本匹配
        ];

        for (const selector of pageButtons) {
            const btn = document.querySelector(selector);
            if (btn && btn.offsetParent !== null) {
                log(`检测到有效翻页按钮:${selector}`);
                btn.click();
                
                // 等待页面加载
                await new Promise(resolve => {
                    const observer = new MutationObserver(() => {
                        if (document.readyState === 'complete') {
                            observer.disconnect();
                            resolve();
                        }
                    });
                    observer.observe(document, {
                        childList: true,
                        subtree: true
                    });
                    setTimeout(resolve, 3000);
                });

                state.currentPage++;
                updateUI();
                state.retryCount = 0;
                return true;
            }
        }

        // 完成检测
        const completionElements = [
            '.chapterItem.current .icon-complete',
            '.statusdone:contains("已完成")',
            '.allDone:contains("全部完成")'
        ];
        
        if (completionElements.some(sel => document.querySelector(sel))) {
            log('检测到课程已完成');
            stopScript();
            GM_notification({
                title: '阅读完成',
                text: '所有章节已学习完成!',
                timeout: 5000
            });
            return true;
        }

        return false;
    } catch (error) {
        handleError(error);
        return false;
    }
}

// 核心控制逻辑
function initCoreLogic() {
    // 智能滚动定时器
    state.timers.push(setInterval(async () => {
        if (!state.active) return;
        
        const reachBottom = smartScroll();
        if (reachBottom) {
            const success = await checkNextPage();
            if (!success) state.retryCount++;
            
            if (state.retryCount >= CONFIG.maxRetries) {
                log(`连续尝试失败 ${CONFIG.maxRetries} 次,暂停脚本`);
                stopScript();
            }
        }
    }, CONFIG.scrollSpeed * (1 + Math.random() * 0.2)));

    // 独立翻页检测
    state.timers.push(setInterval(async () => {
        if (state.active) await checkNextPage();
    }, CONFIG.pageCheckInterval * 1000));
}

// 辅助功能函数
function log(message) {
    if (CONFIG.debugMode) console.log(`[阅读助手] ${message}`);
}

function updateUI() {
    $('#helper-pages').text(state.currentPage);
    const duration = Math.floor((Date.now() - state.startTime) / 1000);
    $('#helper-duration').text(new Date(duration * 1000).toISOString().substr(11, 8));
}

function toggleScript() {
    state.active = !state.active;
    $('#helper-status').text(state.active ? '运行中' : '已暂停');
    $('#helper-toggle').text(state.active ? '暂停' : '继续');
}

function resetStats() {
    state.currentPage = 1;
    state.startTime = Date.now();
    updateUI();
}

function stopScript() {
    state.active = false;
    state.timers.forEach(clearInterval);
    state.observers.forEach(obs => obs.disconnect());
    $('#helper-status').text('已完成');
    $('#helper-toggle').remove();
}

function handleError(error) {
    console.error('[阅读助手] 发生错误:', error);
    GM_notification({
        title: '脚本异常',
        text: error.message,
        timeout: 3000
    });
}

// 初始化
$(document).ready(function() {
    createControlPanel();
    initCoreLogic();
    log('脚本已启动');
});

})();