diff options
| author | Tianren Dong <tianrendong.dtr@gmail.com> | 2026-06-01 21:49:15 -0400 |
|---|---|---|
| committer | Tianren Dong <tianrendong.dtr@gmail.com> | 2026-06-01 21:49:15 -0400 |
| commit | fa33b0797755b146b175f4aa792c8d0c22ba781f (patch) | |
| tree | 670888ebf16dc95ba286d3af67ef50237f98a2b9 /extensions | |
| parent | 1c7cb9934275731de8f2fe950a0aa461637e5450 (diff) | |
0.15.35: persist /chrome authorize grant across /reload (re-register chrome_* tools on session_start)
Diffstat (limited to 'extensions')
| -rw-r--r-- | extensions/chrome-profile-bridge/browser-extension/manifest.json | 2 | ||||
| -rw-r--r-- | extensions/chrome-profile-bridge/index.ts | 25 |
2 files changed, 26 insertions, 1 deletions
diff --git a/extensions/chrome-profile-bridge/browser-extension/manifest.json b/extensions/chrome-profile-bridge/browser-extension/manifest.json index 580b25f..877bed5 100644 --- a/extensions/chrome-profile-bridge/browser-extension/manifest.json +++ b/extensions/chrome-profile-bridge/browser-extension/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "Pi Chrome Connector", - "version": "0.15.34", + "version": "0.15.35", "description": "Lets Pi control tabs in Chrome via a local connector at 127.0.0.1.", "permissions": [ "tabs", diff --git a/extensions/chrome-profile-bridge/index.ts b/extensions/chrome-profile-bridge/index.ts index b29a8e4..bed1302 100644 --- a/extensions/chrome-profile-bridge/index.ts +++ b/extensions/chrome-profile-bridge/index.ts @@ -55,6 +55,10 @@ function readPiChromeVersion(): string { } const PI_CHROME_VERSION = readPiChromeVersion(); const PI_CHROME_GLOBAL_KEY = "__piChromeProfileBridgeLoaded__"; +// Authorization is kept on globalThis (separate from the singleton flag, which is cleared on +// reload) so a /reload — which tears down and re-evaluates the module — does not silently drop +// an active /chrome authorize grant. +const PI_CHROME_AUTH_KEY = "__piChromeProfileBridgeAuth__"; const DEFAULT_HOST = process.env.PI_CHROME_BRIDGE_HOST ?? "127.0.0.1"; const DEFAULT_PORT = Number(process.env.PI_CHROME_BRIDGE_PORT ?? "17318"); const DEFAULT_TIMEOUT_MS = 30_000; @@ -535,6 +539,7 @@ export default function (pi: ExtensionAPI): void { const currentRoot = extensionRoot(); const globalState = globalThis as typeof globalThis & { [PI_CHROME_GLOBAL_KEY]?: { version: string; root: string; token?: symbol }; + [PI_CHROME_AUTH_KEY]?: { until: number | "indefinite" }; }; const alreadyLoaded = globalState[PI_CHROME_GLOBAL_KEY]; if (alreadyLoaded?.token || (alreadyLoaded && alreadyLoaded.root !== currentRoot)) { @@ -551,6 +556,19 @@ export default function (pi: ExtensionAPI): void { const bridge = new ChromeProfileBridge(DEFAULT_HOST, DEFAULT_PORT); let backgroundDefault = true; let chromeAuthorizedUntil: number | "indefinite" | undefined; + // Restore an authorization that survived a /reload. Drop it if it already expired. + const persistedAuth = globalState[PI_CHROME_AUTH_KEY]; + if (persistedAuth) { + if (persistedAuth.until === "indefinite" || persistedAuth.until > Date.now()) { + chromeAuthorizedUntil = persistedAuth.until; + } else { + delete globalState[PI_CHROME_AUTH_KEY]; + } + } + const persistAuth = (): void => { + if (chromeAuthorizedUntil === undefined) delete globalState[PI_CHROME_AUTH_KEY]; + else globalState[PI_CHROME_AUTH_KEY] = { until: chromeAuthorizedUntil }; + }; let chromeToolsRegistered = false; let authExpiryTimer: NodeJS.Timeout | undefined; // Remembered so bridge sends can tag tabs with this session's group even when ctx isn't handy. @@ -576,6 +594,7 @@ export default function (pi: ExtensionAPI): void { const lockChromeControl = (): void => { clearAuthExpiryTimer(); chromeAuthorizedUntil = undefined; + persistAuth(); deactivateChromeTools(); }; @@ -662,6 +681,11 @@ export default function (pi: ExtensionAPI): void { pi.on("session_start", async (_event, ctx) => { sessionCtx = ctx; await bridge.start(); + // Reestablish in-memory state after a /reload restored chromeAuthorizedUntil from globalThis. + if (chromeControlAuthorized()) { + activateChromeTools(); + if (typeof chromeAuthorizedUntil === "number") scheduleAuthExpiry(ctx, chromeAuthorizedUntil); + } updateChromeStatus(ctx); }); @@ -801,6 +825,7 @@ Usage rules: return; } chromeAuthorizedUntil = until; + persistAuth(); activateChromeTools(); scheduleAuthExpiry(ctx, until); ctx.ui.notify(`Chrome control authorized for ${label}.`, "info"); |
