diff --git a/main/src/extension/browserApi.ts b/main/src/extension/browserApi.ts index c11b48e6..90f1ba2e 100644 --- a/main/src/extension/browserApi.ts +++ b/main/src/extension/browserApi.ts @@ -14,7 +14,6 @@ import Utils, { type Satisfies, type Thennable, } from "lib/utils"; -import { PLATFORM } from "consts"; /** * Type map for messages between extension processes @@ -111,8 +110,6 @@ export class BrowserApi { if (opts.handleConnection) { this.attachConnectionHandler(); } - - this.setupIosPeriodicReload(); } private attachRequestHandler() { @@ -508,31 +505,4 @@ export class BrowserApi { handleBrowserLoad(handler: () => void) { chrome.runtime.onStartup.addListener(handler); } - - // workaround to ios 17.5 bug where background script freezes after ~30s of non-stop activity - // https://github.com/alexkates/content-script-non-responsive-bug/issues/1 - private setupIosPeriodicReload() { - if (PLATFORM !== "ios" || this.context !== "background") return; - console.debug("Set up periodic ios reload"); - - let counter = 0; - let last = Date.now(); - - function checkReload() { - const curr = Date.now(); - if (curr - last > 4500) { - counter = 0; - } else { - counter += 1; - } - last = curr; - - if (counter > 25) { - console.debug("Reloading extension"); - chrome.runtime.reload(); - } - } - - setInterval(checkReload, 1000); - } } diff --git a/main/src/platform/ios/index.ts b/main/src/platform/ios/index.ts index 9942a7e9..a0084d85 100644 --- a/main/src/platform/ios/index.ts +++ b/main/src/platform/ios/index.ts @@ -21,6 +21,7 @@ import { migrateConfigObject, type StoredCompatConfiguration, } from "lib/compat"; +import { PLATFORM } from "consts"; export * from "../common"; @@ -32,11 +33,18 @@ export interface AppMessageMap { search: [SearchRequest, RawTokenizeResult]; ttsVoices: [null, TTSVoice[]]; tts: [TTSRequest, null]; + iosVersion: [null, IosVersion]; } export type AppRequest = AppMessageMap[K][0]; export type AppResponse = AppMessageMap[K][1]; +interface IosVersion { + major: number; + minor: number; + patch: number; +} + export class IosPlatform implements IPlatform { static IS_DESKTOP = false; static IS_IOS = true; @@ -58,6 +66,8 @@ export class IosPlatform implements IPlatform { browserApi.handleRequest("saveConfig", (config) => { return this.saveConfig(config); }); + + void this.setupIosPeriodicReload(); } } @@ -184,6 +194,36 @@ export class IosPlatform implements IPlatform { await this.saveConfig(migrated); return migrated; } + + // workaround to ios 17.5 bug where background script freezes after ~30s of non-stop activity + // https://github.com/alexkates/content-script-non-responsive-bug/issues/1 + private async setupIosPeriodicReload() { + if (PLATFORM !== "ios" || this.browserApi.context !== "background") return; + console.debug("Set up periodic ios reload"); + + let wakeup = Date.now(); + let last = Date.now(); + + function checkReload() { + const curr = Date.now(); + if (curr - last > 4500) { + wakeup = curr; + } + last = curr; + + if (curr - wakeup > 25 * 1000) { + console.debug("Reloading extension"); + chrome.runtime.reload(); + } + } + + const iv = setInterval(checkReload, 1000); + + const ver = await this.requestToApp("iosVersion", null); + if (!(ver.major > 18 || (ver.major === 17 && ver.minor >= 5))) { + clearInterval(iv); + } + } } IosPlatform satisfies IPlatformStatic; diff --git a/safari/iOS (Extension)/IOSWebExtensionHandler.swift b/safari/iOS (Extension)/IOSWebExtensionHandler.swift index 45ffbe5c..94c08dde 100644 --- a/safari/iOS (Extension)/IOSWebExtensionHandler.swift +++ b/safari/iOS (Extension)/IOSWebExtensionHandler.swift @@ -46,6 +46,10 @@ class IOSWebExtensionHandler: NSObject, NSExtensionRequestHandling { case "tts": let req: TTSRequest = try jsonDeserialize(json: request) try ttsSpeak(voice: req.voice, text: req.text) + case "iosVersion": + let ver = ProcessInfo.processInfo.operatingSystemVersion + let iosVersion = IosVersion(major: ver.majorVersion, minor: ver.minorVersion, patch: ver.patchVersion) + jsonResponse = try jsonSerialize(obj: iosVersion) default: return } @@ -74,3 +78,9 @@ private struct SearchRequest: Decodable { var term: String var charAt: UInt32? } + +private struct IosVersion: Codable { + var major: Int + var minor: Int + var patch: Int +}