diff --git a/src/web.ts b/src/web.ts index ebc2ee8..a0af422 100644 --- a/src/web.ts +++ b/src/web.ts @@ -10,6 +10,10 @@ export class KeepAwakeWeb extends WebPlugin implements KeepAwakePlugin { private wakeLock: WakeLockSentinel | null = null; private readonly _isSupported = 'wakeLock' in navigator; + private handleVisibilityChange = () => { + if (document.visibilityState === 'visible') this.keepAwake(); + }; + public async keepAwake(): Promise { if (!this._isSupported) { this.throwUnsupportedError(); @@ -18,6 +22,8 @@ export class KeepAwakeWeb extends WebPlugin implements KeepAwakePlugin { await this.allowSleep(); } this.wakeLock = await navigator.wakeLock.request('screen'); + document.addEventListener('visibilitychange', this.handleVisibilityChange); + document.addEventListener('fullscreenchange', this.handleVisibilityChange); } public async allowSleep(): Promise { @@ -26,6 +32,14 @@ export class KeepAwakeWeb extends WebPlugin implements KeepAwakePlugin { } this.wakeLock?.release(); this.wakeLock = null; + document.removeEventListener( + 'visibilitychange', + this.handleVisibilityChange, + ); + document.removeEventListener( + 'fullscreenchange', + this.handleVisibilityChange, + ); } public async isSupported(): Promise {