Skip to content

Commit e39c595

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 6557622 commit e39c595

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
@@ -1420,6 +1420,11 @@ chrome.runtime.onStartup.addListener(() => {
14201420
initialize();
14211421
});
14221422
initialize();
1423+
if (chrome.idle?.onStateChanged?.addListener) {
1424+
chrome.idle.onStateChanged.addListener((newState) => {
1425+
if (newState === "active") void connect();
1426+
});
1427+
}
14231428
chrome.alarms.onAlarm.addListener(async (alarm) => {
14241429
if (alarm.name === "keepalive") void connect();
14251430
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
@@ -1075,6 +1075,16 @@ chrome.runtime.onStartup.addListener(() => {
10751075
// initialize() is idempotent, so lifecycle events remain harmless.
10761076
initialize();
10771077

1078+
// Reconnect when the user becomes active after OS resume or screen unlock.
1079+
// The keepalive alarm fires every ~24 s, but Chrome may suspend the service
1080+
// worker across long sleeps, so an alarm tick can be skipped past resume.
1081+
// `chrome.idle.onStateChanged` wakes the worker on the first active signal.
1082+
if (chrome.idle?.onStateChanged?.addListener) {
1083+
chrome.idle.onStateChanged.addListener((newState) => {
1084+
if (newState === 'active') void connect();
1085+
});
1086+
}
1087+
10781088
chrome.alarms.onAlarm.addListener(async (alarm) => {
10791089
if (alarm.name === 'keepalive') void connect();
10801090
const leaseKey = leaseKeyFromAlarmName(alarm.name);

0 commit comments

Comments
 (0)