抖音巨量百应直播中控台自动讲解(循环计时版)

自动控制直播中控台的讲解按钮,显示当前循环进度(如5/25秒)

当前为 2025-07-09 提交的版本,查看 最新版本

// ==UserScript==
// @name         抖音巨量百应直播中控台自动讲解(循环计时版)
// @namespace    https://github.com/hongxiaokun
// @version      1.4
// @description  自动控制直播中控台的讲解按钮,显示当前循环进度(如5/25秒)
// @author       鸿小坤
// @match        https://buyin.jinritemai.com/dashboard/live/control*
// @license      MIT
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // 添加样式
    GM_addStyle(`
        #auto-explain-panel {
            position: fixed;
            bottom: 20px;
            right: 20px;
            z-index: 9999;
            background: white;
            padding: 15px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            width: 280px;
        }
        #auto-explain-panel h3 {
            margin-top: 0;
            margin-bottom: 15px;
            font-size: 16px;
            color: #333;
            text-align: center;
        }
        .control-group {
            margin-bottom: 15px;
        }
        .control-group label {
            display: block;
            margin-bottom: 5px;
            font-size: 14px;
            color: #666;
        }
        .control-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .control-row {
            display: flex;
            gap: 10px;
        }
        .control-row .control-group {
            flex: 1;
            margin-bottom: 0;
        }
        #auto-explain-btn {
            width: 100%;
            padding: 10px;
            background-color: #FE2C55;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-weight: bold;
            transition: background-color 0.3s;
            margin-top: 10px;
        }
        #auto-explain-btn:hover {
            background-color: #E51E4A;
        }
        #auto-explain-btn.stop {
            background-color: #6c757d;
        }
        #auto-explain-btn.stop:hover {
            background-color: #5a6268;
        }
        #interval-display {
            margin: 15px 0;
            padding: 8px;
            background-color: #f8f9fa;
            border-radius: 4px;
            text-align: center;
            font-size: 13px;
        }
        #interval-display div {
            margin: 3px 0;
        }
        #interval-display span {
            font-weight: bold;
            color: #FE2C55;
        }
        .status-indicator {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            display: inline-block;
            margin-right: 5px;
        }
        .status-running {
            background-color: #28a745;
        }
        .status-stopped {
            background-color: #dc3545;
        }
        .progress-display {
            color: #333;
        }
        .progress-bar-container {
            width: 100%;
            height: 5px;
            background-color: #e9ecef;
            border-radius: 2.5px;
            margin-top: 5px;
            overflow: hidden;
        }
        .progress-bar {
            height: 100%;
            background-color: #FE2C55;
            width: 0%;
            transition: width 1s linear;
        }
    `);

    // 创建控制面板
    const panel = document.createElement('div');
    panel.id = 'auto-explain-panel';
    panel.innerHTML = `
        <h3>自动讲解控制</h3>
        <div class="control-row">
            <div class="control-group">
                <label for="explain-interval">基础间隔(秒)</label>
                <input type="number" id="explain-interval" value="20" min="5">
            </div>
            <div class="control-group">
                <label for="random-interval">随机+(秒)</label>
                <input type="number" id="random-interval" value="5" min="0">
            </div>
        </div>
        <div id="interval-display">
            <div><span class="status-indicator status-stopped"></span>状态: <span id="status-text">已停止</span></div>
            <div class="progress-display">循环进度: <span id="current-progress">0/0</span>秒</div>
            <div class="progress-bar-container"><div class="progress-bar"></div></div>
            <div>下次执行间隔: <span id="current-interval">0</span>秒</div>
        </div>
        <button id="auto-explain-btn">开始自动讲解</button>
    `;
    document.body.appendChild(panel);

    // 获取元素
    const intervalInput = document.getElementById('explain-interval');
    const randomInput = document.getElementById('random-interval');
    const currentIntervalDisplay = document.getElementById('current-interval');
    const currentProgressDisplay = document.getElementById('current-progress');
    const progressBar = document.querySelector('.progress-bar');
    const startBtn = document.getElementById('auto-explain-btn');
    const statusText = document.getElementById('status-text');
    const statusIndicator = document.querySelector('.status-indicator');

    let timer = null;
    let progressTimer = null;
    let isRunning = false;
    let nextInterval = 0;
    let currentCycleTime = 0;
    let totalCycleTime = 0;

    // 更新进度显示
    function updateProgressDisplay() {
        currentProgressDisplay.textContent = `${currentCycleTime}/${totalCycleTime}`;
        const progressPercent = totalCycleTime > 0 ? (currentCycleTime / totalCycleTime) * 100 : 0;
        progressBar.style.width = `${progressPercent}%`;
    }

    // 启动进度计时器
    function startProgressTimer(totalTime) {
        totalCycleTime = totalTime;
        currentCycleTime = 0;
        updateProgressDisplay();
        
        progressTimer = setInterval(() => {
            currentCycleTime++;
            updateProgressDisplay();
            
            if (currentCycleTime >= totalCycleTime) {
                clearInterval(progressTimer);
            }
        }, 1000);
    }

    // 停止进度计时器
    function stopProgressTimer() {
        if (progressTimer) {
            clearInterval(progressTimer);
            progressTimer = null;
        }
        currentCycleTime = 0;
        totalCycleTime = 0;
        updateProgressDisplay();
    }

    // 更新状态显示
    function updateStatusDisplay() {
        if (isRunning) {
            statusText.textContent = '运行中';
            statusIndicator.classList.remove('status-stopped');
            statusIndicator.classList.add('status-running');
        } else {
            statusText.textContent = '已停止';
            statusIndicator.classList.remove('status-running');
            statusIndicator.classList.add('status-stopped');
        }
    }

    // 查找讲解按钮
    function findExplainButton() {
        return document.evaluate(
            '//button[contains(., "讲解") or contains(., "取消讲解")]',
            document,
            null,
            XPathResult.FIRST_ORDERED_NODE_TYPE,
            null
        ).singleNodeValue;
    }

    // 生成随机间隔时间
    function generateRandomInterval() {
        const base = parseInt(intervalInput.value) || 20;
        const randomMax = parseInt(randomInput.value) || 5;
        const randomAdd = randomMax > 0 ? Math.floor(Math.random() * randomMax) + 1 : 0;
        nextInterval = base + randomAdd;
        currentIntervalDisplay.textContent = nextInterval;
        return nextInterval * 1000; // 转换为毫秒
    }

    // 执行讲解操作
    async function performExplain() {
        try {
            const explainBtn = findExplainButton();
            if (!explainBtn) {
                console.warn('未找到讲解按钮');
                return false;
            }

            // 检查当前按钮状态
            const isActive = explainBtn.textContent.includes('取消讲解');
            
            if (isActive) {
                // 当前是"取消讲解"状态,先点击取消
                explainBtn.click();
                await new Promise(resolve => setTimeout(resolve, 1000));
            }
            
            // 点击讲解
            explainBtn.click();
            return true;
        } catch (error) {
            console.error('执行讲解出错:', error);
            return false;
        }
    }

    // 开始/停止自动讲解
    async function toggleAutoExplain() {
        if (isRunning) {
            clearTimeout(timer);
            stopProgressTimer();
            isRunning = false;
            startBtn.textContent = '开始自动讲解';
            startBtn.classList.remove('stop');
            updateStatusDisplay();
            currentIntervalDisplay.textContent = '0';
        } else {
            const base = parseInt(intervalInput.value);
            const randomMax = parseInt(randomInput.value);
            
            if (base < 5) {
                alert('基础间隔时间不能小于5秒');
                return;
            }
            
            if (randomMax < 0) {
                alert('随机秒数不能为负数');
                return;
            }
            
            // 尝试执行一次讲解,确保能找到按钮
            const success = await performExplain();
            if (!success) {
                alert('未找到讲解按钮,请确认页面已加载完成');
                return;
            }
            
            // 生成初始间隔时间
            const intervalMs = generateRandomInterval();
            startProgressTimer(nextInterval);
            
            isRunning = true;
            startBtn.textContent = '停止自动讲解';
            startBtn.classList.add('stop');
            updateStatusDisplay();
            
            // 设置定时器
            scheduleNext();
        }
    }

    // 安排下一次执行
    function scheduleNext() {
        if (!isRunning) return;
        
        const intervalMs = generateRandomInterval();
        startProgressTimer(nextInterval);
        
        timer = setTimeout(async () => {
            await performExplain();
            scheduleNext();
        }, intervalMs);
    }

    // 绑定事件
    startBtn.addEventListener('click', toggleAutoExplain);
    
    // 输入变化时更新显示
    intervalInput.addEventListener('input', () => {
        if (isRunning) {
            generateRandomInterval();
            startProgressTimer(nextInterval);
        }
    });
    
    randomInput.addEventListener('input', () => {
        if (isRunning) {
            generateRandomInterval();
            startProgressTimer(nextInterval);
        }
    });

    // 初始化状态显示
    updateStatusDisplay();

    // 页面卸载时清理定时器
    window.addEventListener('beforeunload', () => {
        clearTimeout(timer);
        stopProgressTimer();
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址