You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome标签页失活时如何让Tampermonkey脚本的setInterval正常运行?

解决Tampermonkey脚本后台标签定时器失效问题

浏览器为了节省资源,会对后台标签(切换到其他标签/窗口最小化)的原生setTimeout/setInterval进行节流,导致定时器延迟甚至停止执行。针对你的问题,提供两个可靠的解决方案:

方案一:使用Tampermonkey内置定时器(最简便)

Tampermonkey提供了不受浏览器后台节流影响的GM_setTimeout和GM_setInterval函数,只需添加对应的权限即可使用。

修改你的脚本如下:

// ==UserScript==
// @name         User Script
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  自动检查并点击Twitch掉落按钮
// @author       None
// @match        https://www.twitch.tv/drops/inventory
// @icon         https://www.google.com/s2/favicons?domain=tampermonkey.net
// @require      https://code.jquery.com/jquery-1.11.0.min.js
// @grant        GM_setTimeout
// @grant        GM_setInterval
/* globals jQuery, $ */
// ==/UserScript==

$(window).load(function(){
    'use strict';

    function SearchController(){
        $('.dYkLvU').each(function(){
            const btnText = $(this).find('button .phMMp').text();
            if (btnText.trim() === 'Click'){
                $(this).find('button .phMMp').trigger('click');
            }
        });
    }

    // 初始延迟执行检查
    GM_setTimeout(function(){
        SearchController();
        // 每3分钟(180000毫秒)刷新页面
        GM_setInterval(function(){
            location.reload();
        }, 180000);
    }, 4000);
});

关键修改点:

  • 在@grant中添加GM_setTimeout和GM_setInterval权限
  • 替换原生定时器为Tampermonkey内置函数
  • 调整刷新间隔为你需求的3分钟(180000毫秒)

方案二:使用Web Workers(更底层,不受沙箱限制)

Web Workers运行在独立线程,不受浏览器后台标签的定时器节流影响。可以在Worker中定时发送消息,主线程收到消息后执行页面操作。

修改后的脚本示例:

// ==UserScript==
// @name         User Script
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  自动检查并点击Twitch掉落按钮
// @author       None
// @match        https://www.twitch.tv/drops/inventory
// @icon         https://www.google.com/s2/favicons?domain=tampermonkey.net
// @require      https://code.jquery.com/jquery-1.11.0.min.js
// @grant        none
/* globals jQuery, $ */
// ==/UserScript==

$(window).load(function(){
    'use strict';

    function SearchController(){
        $('.dYkLvU').each(function(){
            const btnText = $(this).find('button .phMMp').text();
            if (btnText.trim() === 'Click'){
                $(this).find('button .phMMp').trigger('click');
            }
        });
    }

    // 初始执行检查
    setTimeout(() => {
        SearchController();
        // 创建Web Worker处理定时
        const worker = new Worker(URL.createObjectURL(new Blob([`
            // 每3分钟发送一次刷新指令
            setInterval(() => {
                postMessage('reload');
            }, 180000);
        `], {type: 'application/javascript'})));

        // 监听Worker消息
        worker.onmessage = function(e){
            if (e.data === 'reload'){
                location.reload();
            }
        };
    }, 4000);
});

这个方案通过Worker线程维持定时,主线程只负责接收指令执行操作,完全避开浏览器的后台节流机制。

关于HackTimer失效的原因

HackTimer通过替换全局的setTimeout/setInterval实现无节流定时,但Tampermonkey的沙箱环境会隔离脚本与页面的全局对象,导致HackTimer的替换无法生效,因此不推荐在Tampermonkey中使用。

内容的提问来源于stack exchange,提问作者Lememlol

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 03:05:29