抖音直播间显示人数

显示抖音直播间人数 @陈泽开播了吗

// ==UserScript==
// @name         抖音直播间显示人数
// @namespace    http://your.namespace.com
// @version      0.4
// @description  显示抖音直播间人数 @陈泽开播了吗
// @author       Your Name
// @match        https://live.douyin.com/*
// @grant        none
// @license      MIT
// ==/UserScript==
(function() {
    'use strict';

    const updateInterval = 3000; // 每10秒更新一次
    let hasLiveStarted = false; // 直播是否已经开始

    const copySuccessMessage = document.createElement('div');
    copySuccessMessage.id = 'copySuccessMessage';
    copySuccessMessage.style.cssText = 'position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: rgba(255, 255, 255, 0.3); color: black; padding: 10px 20px; border-radius: 5px; display: none; z-index: 9999;';
    document.body.appendChild(copySuccessMessage);

    function updateStats() {
        const current_url = window.location.href;
        const regex =  /https:\/\/live\.douyin\.com\/([\w\-\.]+)/;
        const match = current_url.match(regex);
        if (match) {
            const room_number = match[1];
            const user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586";
            const additional_headers = {"User-Agent": user_agent};
            const webcast_url = `https://live.douyin.com/webcast/room/web/enter/?aid=6383&live_id=1&device_platform=web&browser_language=zh-CN&browser_platform=Win32&browser_name=Edge&browser_version=119.0.0.0&web_rid=${room_number}`;

            fetch(webcast_url, {headers: additional_headers})
            .then(response => response.json())
            .then(data => {
                const isLiveNow = data['data']['data'][0]['status'] !== 2; // 状态2表示直播进行中
                if (!hasLiveStarted && isLiveNow) {
                    setTimeout(() => {
                        window.location.reload();
                    }, 10000); // 延迟10秒钟后刷新页面
                }

                if (!hasLiveStarted && isLiveNow) {
                    setTimeout(simulateBKeyPress, 5000); // 延迟5秒钟后模拟按下B键
                }

                hasLiveStarted = isLiveNow; // 更新直播状态

                const audienceCount = data['data']['data'][0]['room_view_stats']['display_value'];
                updateAudienceCount(audienceCount, data);
                updateAudienceCount2(audienceCount, data);
            })
            .catch(error => {
                console.error('Error fetching live stats:', error);
            });
        }
    }

    function updateAudienceCount(count, data) {
        const audienceElement = document.querySelector('.UxWMHF9c[data-e2e="live-room-audience"]');
        if (audienceElement) {
            audienceElement.textContent = count;
            audienceElement.style.cursor = 'pointer';
            audienceElement.addEventListener('click', () => copyStreamURL(data));
        } else {
            console.error('Audience element not found');
        }
    }

    function updateAudienceCount2(count, data) {
        const audienceElement = document.querySelector('.pnW5bGAA[data-e2e="live-room-audience"]');
        if (audienceElement) {
            audienceElement.textContent = count;
            audienceElement.style.cursor = 'pointer';
            audienceElement.addEventListener('click', () => copyStreamURL(data));
        } else {
            console.error('Audience element not found');
        }
    }

    function copyStreamURL(data) {
        const streamURL = data['data']['data'][0]['stream_url']['flv_pull_url']['FULL_HD1'];
        navigator.clipboard.writeText(streamURL).then(() => {
            copySuccessMessage.innerText = 'Stream URL 已复制到剪贴板!';
            copySuccessMessage.style.display = 'block';
            setTimeout(() => {
                copySuccessMessage.style.display = 'none';
            }, 3000);
        });
    }

    function simulateBKeyPress() {
        const event = new KeyboardEvent('keydown', {
            key: 'b',
            code: 'KeyB',
            keyCode: 66, // B 键的键码
            charCode: 0,
            which: 66, // B 键的键码
            shiftKey: false,
            ctrlKey: false,
            metaKey: false,
            bubbles: true, // 事件是否应该冒泡
        });
        document.dispatchEvent(event);
        console.log("执行按B")
    }

    // 初次加载页面时立即更新一次数据
    updateStats();

    // 定时更新数据
    setInterval(updateStats, updateInterval);
})();