Skip to content

Commit 0b48f61

Browse files
committed
fix(extension): reconnect via chrome.idle on OS resume / screen unlock
The 24-second keepalive alarm is the only reconnect trigger today, but Chrome may suspend the MV3 service worker across deep sleep so an alarm tick can be skipped past system resume. Add chrome.idle.onStateChanged to wake the worker on the first active signal after lock or sleep. Fixes #1735
1 parent 55d9190 commit 0b48f61

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

extension/dist/background.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,11 @@ chrome.runtime.onStartup.addListener(() => {
14031403
initialize();
14041404
});
14051405
initialize();
1406+
if (chrome.idle?.onStateChanged?.addListener) {
1407+
chrome.idle.onStateChanged.addListener((newState) => {
1408+
if (newState === "active") void connect();
1409+
});
1410+
}
14061411
chrome.alarms.onAlarm.addListener(async (alarm) => {
14071412
if (alarm.name === "keepalive") void connect();
14081413
const leaseKey = leaseKeyFromAlarmName(alarm.name);

extension/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"cookies",
1010
"activeTab",
1111
"alarms",
12+
"idle",
1213
"storage",
1314
"tabGroups",
1415
"downloads"

extension/src/background.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,16 @@ chrome.runtime.onStartup.addListener(() => {
10211021
// initialize() is idempotent, so lifecycle events remain harmless.
10221022
initialize();
10231023

1024+
// Reconnect when the user becomes active after OS resume or screen unlock.
1025+
// The keepalive alarm fires every ~24 s, but Chrome may suspend the service
1026+
// worker across long sleeps, so an alarm tick can be skipped past resume.
1027+
// `chrome.idle.onStateChanged` wakes the worker on the first active signal.
1028+
if (chrome.idle?.onStateChanged?.addListener) {
1029+
chrome.idle.onStateChanged.addListener((newState) => {
1030+
if (newState === 'active') void connect();
1031+
});
1032+
}
1033+
10241034
chrome.alarms.onAlarm.addListener(async (alarm) => {
10251035
if (alarm.name === 'keepalive') void connect();
10261036
const leaseKey = leaseKeyFromAlarmName(alarm.name);

0 commit comments

Comments
 (0)