aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTianren Dong <tianrendong.dtr@gmail.com>2026-06-01 21:49:15 -0400
committerTianren Dong <tianrendong.dtr@gmail.com>2026-06-01 21:49:15 -0400
commitfa33b0797755b146b175f4aa792c8d0c22ba781f (patch)
tree670888ebf16dc95ba286d3af67ef50237f98a2b9
parent1c7cb9934275731de8f2fe950a0aa461637e5450 (diff)
0.15.35: persist /chrome authorize grant across /reload (re-register chrome_* tools on session_start)
-rw-r--r--extensions/chrome-profile-bridge/browser-extension/manifest.json2
-rw-r--r--extensions/chrome-profile-bridge/index.ts25
-rw-r--r--package.json2
3 files changed, 27 insertions, 2 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");
diff --git a/package.json b/package.json
index 830208a..d2a2b18 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "pi-chrome",
- "version": "0.15.34",
+ "version": "0.15.35",
"scripts": {
"test": "node test-suite/unit/csp-eval.test.mjs",
"version": "node scripts/sync-manifest-version.js",