GM_wakeup
GM_wakeup.js
GM_wakeup.js is a lightweight, public-domain developer library for userscripts (Tampermonkey, Violentmonkey, Greasemonkey). It acts as a background heartbeat and synchronization utility, designed to manage cross-tab timing or serve as a companion tool for libraries like GM_lock.GM_wakeup.js 是一个轻量级、公共领域的油猴脚本(Tampermonkey、Violentmonkey、Greasemonkey)开发者工具库。它作为一个后台心跳和同步工具,旨在管理跨标签页的定时任务,或作为 GM_lock 等库的辅助工具。
By checking and updating a shared storage key (__GM_WAKEUP__) every 20 seconds, it ensures that your background scripts stay synchronized and can reliably wake up specific operations without resource-heavy polling loops.
通过每 20 秒检查并更新一次共享存储键值(__GM_WAKEUP__),它能确保您的后台脚本保持同步,并能可靠地唤醒特定操作,而无需编写高能耗的轮询循环。
Features / 功能特点
- Cross-Tab Synchronization / 跨标签页同步: Uses standard extension storage APIs to coordinate timing and state across multiple open browser tabs.
使用标准的扩展存储 API 来协调多个打开的浏览器标签页之间的定时和状态。 - Interval Heartbeat / 定时心跳: Synchronizes actions on a precise, global 20-second interval grid.
在精确的、全局通用的 20 秒间隔网格上同步操作。 - API Compatibility / API 兼容性: Supports both standard synchronous functions (
GM_setValue/GM_getValue) and asynchronous promises (GM.setValue/GM.getValue).
同时支持标准的同步函数(GM_setValue/GM_getValue)和异步 Promise(GM.setValue/GM.getValue)。 - Performance Optimized / 性能优化: Utilizes background task scheduling to minimize CPU usage and prevent the browser from throttling the script in inactive tabs.
利用后台任务调度机制,将 CPU 占用率降至最低,并防止浏览器限制未激活标签页中的脚本运行。 - The Unlicense / 公共领域条款: Free, unencumbered software released entirely into the public domain.
完全释放到公共领域的免费、无拘束软件。
Requirements & Setup / 需求与配置
To use this library, your userscript must grant access to the extension's storage APIs. Ensure your userscript metadata block includes:
要使用此库,您的用户脚本必须授予对扩展存储 API 的访问权限。请确保您的脚本元数据块(Metadata Block)中包含以下内容:
// @grant GM_setValue
// @grant GM_getValue
// OR / 或
// @grant GM.setValue
// @grant GM.getValue
How It Works / 工作原理
GM_wakeup.start(): Activates the heartbeat loop.
激活心跳循环。- Time-Blocking / 时间分块: It divides time into consistent 20-second windows.
它将时间划分为固定且连续的的 20 秒时间窗口。 - The Check / 检查机制: The script checks if another tab has already updated the shared storage key for the current 20-second window. If not, it updates it.
脚本会检查是否有其他标签页已经更新了当前 20 秒窗口的共享存储键值。如果没有,则由当前标签页进行更新。 GM_wakeup.stop(): Safely halts the background loop.
安全地停止后台循环。