【油猴/JS脚本】模拟键盘/鼠标活动避免网页版抖音中断直播

// ==UserScript==
// @name         抖音直播防暂停脚本
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  通过模拟极微小操作防止抖音网页直播暂停
// @match        https://live.douyin.com/*
// @match        https://www.douyin.com/follow/live/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 配置参数
    const config = {
        moveStep: 1, // 每次移动的像素(用户不可见幅度)
        interval: 30000, // 操作间隔30秒(低于常见检测阈值)
        debug: false // 调试模式
    };

    // 创建初始位置记录器
    let mousePos = { x: 0, y: 0 };

    // 随机偏移函数
    const randomOffset = () => Math.random() > 0.5 ? config.moveStep : -config.moveStep;

    // 模拟鼠标移动(极微小幅度)
    function simulateMouseMove() {
        mousePos.x += randomOffset();
        mousePos.y += randomOffset();

        document.dispatchEvent(
            new MouseEvent('mousemove', {
                view: window,
                bubbles: true,
                cancelable: true,
                clientX: mousePos.x,
                clientY: mousePos.y
            })
        );

        if (config.debug) console.log('模拟鼠标移动:', mousePos);
    }

    // 模拟键盘活动(无实际输入)
    function simulateKeyboardActivity() {
        window.dispatchEvent(
            new KeyboardEvent('keydown', {
                key: ' ',
                keyCode: 32,
                bubbles: true,
                cancelable: true
            })
        );

        if (config.debug) console.log('模拟键盘活动');
    }

    // 随机选择操作模式
    function randomAction() {
        Math.random() > 0.5 ? simulateMouseMove() : simulateKeyboardActivity();
    }

    // 启动保活机制
    const keepAlive = setInterval(randomAction, config.interval);

    // 清除定时器(可选)
    // window.addEventListener('beforeunload', () => clearInterval(keepAlive));

    if (config.debug) console.log('防暂停脚本已激活');
})();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值