Greasy Fork

Greasy Fork is available in English.

Douyin live stream optimization(优化网页)

自动检测特定元素并刷新页面,同时优化观看体验

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Douyin live stream optimization(优化网页)
// @namespace    http://tampermonkey.net/
// @version      0.91
// @description  自动检测特定元素并刷新页面,同时优化观看体验
// @match        https://*.douyin.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    /** ==================== 1. 检测目标元素,必要时刷新页面 ==================== */
    let reloadAttempts = sessionStorage.getItem('reloadAttempts') || 0;
    reloadAttempts = parseInt(reloadAttempts, 10);

    function checkAndRefresh() {
        setInterval(() => {
            const element = document.querySelector("div.RYFhabj2.K5lzL1ML");
            if (element) {
                console.log("检测到目标元素,刷新页面...");
                location.reload();
            } else {
                console.log("未检测到目标元素,继续检查...");
            }
        }, 1000);
    }

    checkAndRefresh();  // 立即开始检查

    /** ==================== 2. 其他优化功能 ==================== */
    function simulateBKeyPress() {
        var event = new KeyboardEvent('keydown', {
            key: 'b',
            code: 'KeyB',
            keyCode: 66,
            charCode: 0,
            which: 66,
            shiftKey: false,
            ctrlKey: false,
            metaKey: false,
            bubbles: true
        });
        document.dispatchEvent(event);
        console.log('模拟按下 B 键');
    }

    function clickButton() {
        var button = document.querySelector('.psKR9RS0 .WoNKVQmY.Z20k_Nsy');
        if (button) {
            button.click();
            console.log('按钮已点击');
        } else {
            console.log('按钮未找到');
        }
    }

    function removeElement(selector, message) {
        var element = document.querySelector(selector);
        if (element) {
            element.remove();
            console.log(message + ' 已删除');
        } else {
            console.log('未找到 ' + message);
        }
    }

    function removeSpecialContent() {
        document.querySelectorAll('div._192Y0el > div.ljM5iqdR').forEach(e => e.remove());
        console.log('特殊内容已删除');
        removeElement('.LE4P00KT', '新的指定元素 1');
        removeElement('.mDjtvQMS.GiftEffectPlugin', '新的指定元素 2');
    }

    function clickNewElement() {
        var newElement = document.querySelector('.D7UhJyco');
        if (newElement) {
            newElement.click();
            console.log('新的指定元素已点击');
        } else {
            console.log('未找到新的指定元素');
        }
    }

    /** ==================== 3. 延迟执行任务 ==================== */
    setTimeout(simulateBKeyPress, 5000);
    setTimeout(clickButton, 5000);
    setTimeout(removeSpecialContent, 5000);

    /** ==================== 4. 定时任务,避免无限循环 ==================== */
    let counter = 0;
    const maxTries = 10;
    const interval = setInterval(() => {
        removeElement('.LE4P00KT', '新的指定元素 1');
        clickNewElement();
        counter++;
        if (counter >= maxTries) clearInterval(interval);
    }, 1000);

})();