Skip to content

Commit dc20ed3

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 c730a02 commit dc20ed3

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
@@ -1327,6 +1327,11 @@ chrome.runtime.onStartup.addListener(() => {
13271327
initialize();
13281328
});
13291329
initialize();
1330+
if (chrome.idle?.onStateChanged?.addListener) {
1331+
chrome.idle.onStateChanged.addListener((newState) => {
1332+
if (newState === "active") void connect();
1333+
});
1334+
}
13301335
chrome.alarms.onAlarm.addListener(async (alarm) => {
13311336
if (alarm.name === "keepalive") void connect();
13321337
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
@@ -913,6 +913,16 @@ chrome.runtime.onStartup.addListener(() => {
913913
// initialize() is idempotent, so lifecycle events remain harmless.
914914
initialize();
915915

916+
// Reconnect when the user becomes active after OS resume or screen unlock.
917+
// The keepalive alarm fires every ~24 s, but Chrome may suspend the service
918+
// worker across long sleeps, so an alarm tick can be skipped past resume.
919+
// `chrome.idle.onStateChanged` wakes the worker on the first active signal.
920+
if (chrome.idle?.onStateChanged?.addListener) {
921+
chrome.idle.onStateChanged.addListener((newState) => {
922+
if (newState === 'active') void connect();
923+
});
924+
}
925+
916926
chrome.alarms.onAlarm.addListener(async (alarm) => {
917927
if (alarm.name === 'keepalive') void connect();
918928
const leaseKey = leaseKeyFromAlarmName(alarm.name);

0 commit comments

Comments
 (0)