From e913cf86b79e0e21b10aca81243f79cb7c240a7e Mon Sep 17 00:00:00 2001 From: cyfung1031 <44498510+cyfung1031@users.noreply.github.com> Date: Sat, 11 Apr 2026 10:12:53 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20WakeUpPing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/offscreen.ts | 2 ++ src/pkg/utils/wakeup-ping.ts | 65 ++++++++++++++++++++++++++++++++++++ src/service_worker.ts | 6 ++++ 3 files changed, 73 insertions(+) create mode 100644 src/pkg/utils/wakeup-ping.ts diff --git a/src/offscreen.ts b/src/offscreen.ts index 9cc1cb42c..f8c51f8a3 100644 --- a/src/offscreen.ts +++ b/src/offscreen.ts @@ -2,6 +2,7 @@ import LoggerCore from "./app/logger/core"; import MessageWriter from "./app/logger/message_writer"; import { OffscreenManager } from "./app/service/offscreen"; import { ServiceWorkerClientMessage } from "@Packages/message/window_message"; +import { startRepetitivePing } from "./pkg/utils/wakeup-ping"; function main() { // 通过postMessage与SW通信,支持结构化克隆(Blob等) @@ -15,6 +16,7 @@ function main() { // 初始化管理器 const manager = new OffscreenManager(swPostMessage); manager.initManager(); + startRepetitivePing(); } main(); diff --git a/src/pkg/utils/wakeup-ping.ts b/src/pkg/utils/wakeup-ping.ts new file mode 100644 index 000000000..c1f22dd8e --- /dev/null +++ b/src/pkg/utils/wakeup-ping.ts @@ -0,0 +1,65 @@ +const PING_INTERVAL_MS = 14_225; + +/** + * scheduler 用于后台排程:Chrome 94+, Firefox 142+ + * @link https://developer.mozilla.org/en-US/docs/Web/API/Scheduler/postTask + */ +const nativeScheduler = + //@ts-ignore + typeof scheduler !== "undefined" && typeof scheduler?.postTask === "function" && scheduler; + +// 高效的 BroadcastChannel 通讯:service worker 和 offscreen 共用同一通道 +const channel = new BroadcastChannel("custom-ping"); + +export const startRepetitivePing = () => { + if (typeof frameElement === "object" && typeof document === "object" && document) { + let counter = 0; + let isMutationPending = false; + + const customPingHandler = (e: Event) => { + chrome.storage.session.set({ persistentWakeup: `${e.timeStamp}` }); + }; + + const pingNode = document.createComment("0"); + + const incrementCounter = () => { + if (!isMutationPending) { + isMutationPending = true; + counter = counter > 8 ? 1 : counter + 1; + pingNode.data = `${counter}`; + } + }; + + pingNode.addEventListener("custom-ping", customPingHandler); + + const pingTask = async () => { + channel.postMessage({}); + incrementCounter(); + }; + + const mutationObserver = new MutationObserver(() => { + if (isMutationPending) { + isMutationPending = false; + if (nativeScheduler) { + nativeScheduler.postTask(pingTask, { priority: "background", delay: PING_INTERVAL_MS }); + } else { + setTimeout(pingTask, PING_INTERVAL_MS); + } + } + }); + mutationObserver.observe(pingNode, { characterData: true }); + incrementCounter(); + } +}; + +export const listenWakeupPing = (onWakeupPing: (...args: any) => any) => { + chrome.storage.session.onChanged.addListener((obj) => { + // consume persistentWakeup + if (typeof obj.persistentWakeup !== "undefined") { + onWakeupPing(); + } + }); + channel.onmessage = (e) => { + chrome.storage.session.set({ persistentWakeup: `${e.timeStamp}` }); + }; +}; diff --git a/src/service_worker.ts b/src/service_worker.ts index 407a27922..85d445516 100644 --- a/src/service_worker.ts +++ b/src/service_worker.ts @@ -8,6 +8,7 @@ import { MessageQueue } from "@Packages/message/message_queue"; import { ServiceWorkerMessageSend } from "@Packages/message/window_message"; import migrate, { migrateChromeStorage } from "./app/migrate"; import { cleanInvalidKeys } from "./app/repo/resource"; +import { listenWakeupPing } from "./pkg/utils/wakeup-ping"; migrate(); migrateChromeStorage(); @@ -59,6 +60,10 @@ async function setupOffscreenDocument() { } } +export const onWakeupPing = () => { + console.debug("onWakeupPing"); // 不用记录在系统日志 +}; + function main() { cleanInvalidKeys(); // 初始化管理器 @@ -77,6 +82,7 @@ function main() { manager.initManager(); // 初始化沙盒环境 setupOffscreenDocument(); + listenWakeupPing(onWakeupPing); } main(); From fb8d80c0021269c2d463fbb3fc6f63206103cade Mon Sep 17 00:00:00 2001 From: cyfung1031 <44498510+cyfung1031@users.noreply.github.com> Date: Sat, 11 Apr 2026 10:59:39 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=9C=AA=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pkg/utils/wakeup-ping.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/pkg/utils/wakeup-ping.ts b/src/pkg/utils/wakeup-ping.ts index c1f22dd8e..fe05194e8 100644 --- a/src/pkg/utils/wakeup-ping.ts +++ b/src/pkg/utils/wakeup-ping.ts @@ -16,10 +16,6 @@ export const startRepetitivePing = () => { let counter = 0; let isMutationPending = false; - const customPingHandler = (e: Event) => { - chrome.storage.session.set({ persistentWakeup: `${e.timeStamp}` }); - }; - const pingNode = document.createComment("0"); const incrementCounter = () => { @@ -30,8 +26,6 @@ export const startRepetitivePing = () => { } }; - pingNode.addEventListener("custom-ping", customPingHandler); - const pingTask = async () => { channel.postMessage({}); incrementCounter(); From efda557e771a068736695b81091d420411c97b8b Mon Sep 17 00:00:00 2001 From: cyfung1031 <44498510+cyfung1031@users.noreply.github.com> Date: Sat, 11 Apr 2026 11:01:51 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E4=B8=AD=E6=96=87=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pkg/utils/wakeup-ping.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pkg/utils/wakeup-ping.ts b/src/pkg/utils/wakeup-ping.ts index fe05194e8..47d40ff37 100644 --- a/src/pkg/utils/wakeup-ping.ts +++ b/src/pkg/utils/wakeup-ping.ts @@ -48,12 +48,14 @@ export const startRepetitivePing = () => { export const listenWakeupPing = (onWakeupPing: (...args: any) => any) => { chrome.storage.session.onChanged.addListener((obj) => { - // consume persistentWakeup + // 消耗 persistentWakeup if (typeof obj.persistentWakeup !== "undefined") { + // 执行任意 callback onWakeupPing(); } }); channel.onmessage = (e) => { + // 触发 chrome storage onChanged 使 service worker 保持活跃 chrome.storage.session.set({ persistentWakeup: `${e.timeStamp}` }); }; }; From 86491b98162631aa79cecae193e469bf95e7e345 Mon Sep 17 00:00:00 2001 From: cyfung1031 <44498510+cyfung1031@users.noreply.github.com> Date: Sat, 11 Apr 2026 11:09:46 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E9=87=87=E7=94=A8=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E9=97=B4=E9=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pkg/utils/wakeup-ping.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pkg/utils/wakeup-ping.ts b/src/pkg/utils/wakeup-ping.ts index 47d40ff37..016e95e28 100644 --- a/src/pkg/utils/wakeup-ping.ts +++ b/src/pkg/utils/wakeup-ping.ts @@ -1,4 +1,5 @@ -const PING_INTERVAL_MS = 14_225; +const PING_INTERVAL_MS_1 = 13_225; +const PING_INTERVAL_MS_2 = 17_765; /** * scheduler 用于后台排程:Chrome 94+, Firefox 142+ @@ -34,10 +35,11 @@ export const startRepetitivePing = () => { const mutationObserver = new MutationObserver(() => { if (isMutationPending) { isMutationPending = false; + const pingIntervalMs = Math.random() * (PING_INTERVAL_MS_2 - PING_INTERVAL_MS_1) + PING_INTERVAL_MS_1; if (nativeScheduler) { - nativeScheduler.postTask(pingTask, { priority: "background", delay: PING_INTERVAL_MS }); + nativeScheduler.postTask(pingTask, { priority: "background", delay: pingIntervalMs }); } else { - setTimeout(pingTask, PING_INTERVAL_MS); + setTimeout(pingTask, pingIntervalMs); } } });