|
| 1 | +const PING_INTERVAL_MS = 14_225; |
| 2 | + |
| 3 | +/** |
| 4 | + * scheduler 用于后台排程:Chrome 94+, Firefox 142+ |
| 5 | + * @link https://developer.mozilla.org/en-US/docs/Web/API/Scheduler/postTask |
| 6 | + */ |
| 7 | +const nativeScheduler = |
| 8 | + //@ts-ignore |
| 9 | + typeof scheduler !== "undefined" && typeof scheduler?.postTask === "function" && scheduler; |
| 10 | + |
| 11 | +// 高效的 BroadcastChannel 通讯:service worker 和 offscreen 共用同一通道 |
| 12 | +const channel = new BroadcastChannel("custom-ping"); |
| 13 | + |
| 14 | +export const startRepetitivePing = () => { |
| 15 | + if (typeof frameElement === "object" && typeof document === "object" && document) { |
| 16 | + let counter = 0; |
| 17 | + let isMutationPending = false; |
| 18 | + |
| 19 | + const customPingHandler = (e: Event) => { |
| 20 | + chrome.storage.session.set({ persistentWakeup: `${e.timeStamp}` }); |
| 21 | + }; |
| 22 | + |
| 23 | + const pingNode = document.createComment("0"); |
| 24 | + |
| 25 | + const incrementCounter = () => { |
| 26 | + if (!isMutationPending) { |
| 27 | + isMutationPending = true; |
| 28 | + counter = counter > 8 ? 1 : counter + 1; |
| 29 | + pingNode.data = `${counter}`; |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + pingNode.addEventListener("custom-ping", customPingHandler); |
| 34 | + |
| 35 | + const pingTask = async () => { |
| 36 | + channel.postMessage({}); |
| 37 | + incrementCounter(); |
| 38 | + }; |
| 39 | + |
| 40 | + const mutationObserver = new MutationObserver(() => { |
| 41 | + if (isMutationPending) { |
| 42 | + isMutationPending = false; |
| 43 | + if (nativeScheduler) { |
| 44 | + nativeScheduler.postTask(pingTask, { priority: "background", delay: PING_INTERVAL_MS }); |
| 45 | + } else { |
| 46 | + setTimeout(pingTask, PING_INTERVAL_MS); |
| 47 | + } |
| 48 | + } |
| 49 | + }); |
| 50 | + mutationObserver.observe(pingNode, { characterData: true }); |
| 51 | + incrementCounter(); |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +export const listenWakeupPing = (onWakeupPing: (...args: any) => any) => { |
| 56 | + chrome.storage.session.onChanged.addListener((obj) => { |
| 57 | + // consume persistentWakeup |
| 58 | + if (typeof obj.persistentWakeup !== "undefined") { |
| 59 | + onWakeupPing(); |
| 60 | + } |
| 61 | + }); |
| 62 | + channel.onmessage = (e) => { |
| 63 | + chrome.storage.session.set({ persistentWakeup: `${e.timeStamp}` }); |
| 64 | + }; |
| 65 | +}; |
0 commit comments