Skip to content

Commit b9aadbf

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 77b29b3 commit b9aadbf

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
@@ -1069,6 +1069,16 @@ chrome.runtime.onStartup.addListener(() => {
10691069
// initialize() is idempotent, so lifecycle events remain harmless.
10701070
initialize();
10711071

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

0 commit comments

Comments
 (0)