Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Refactor: collapse 4× page-world injection boilerplate into a hook factory#239

Merged
twschiller merged 1 commit into
mainfrom
refactor/page-world-hook-factory
Jun 9, 2026
Merged

Refactor: collapse 4× page-world injection boilerplate into a hook factory#239
twschiller merged 1 commit into
mainfrom
refactor/page-world-hook-factory

Conversation

@twschiller

Copy link
Copy Markdown
Contributor

What

Four page-world (world: "MAIN") features — webdriver-probe,
checkout-checkbox-defense, shadow-root-probe, and dump-trace-bridge
each shipped the same four-part shape: a *-registration.ts with an ~80 LOC
register/isRegistered/unregister/sync/start lifecycle keyed on a
toggle, a *-source.ts page-world install fn, a thin entry file, and (for
three of them) a byte-identical executeScript({ world: "MAIN", func })
inject-* handler in background.ts.

This collapses the duplicated lifecycle and dispatch into:

  • lib/page-world-hook.tscreatePageWorldHook({ scriptId, scriptFile, logLabel, allFrames, shouldRegister, subscribe }) owns the register/sync
    reconcile once, plus injectPageWorldScript(sender, installFn, messageType)
    for the on-demand fallback.
  • lib/page-world-hooks.ts — the four hooks as four config objects in one
    table, startPageWorldHooks(), and a table-driven
    dispatchPageWorldInject().
  • background.ts — the three inject-* if blocks become one dispatch
    call; the four start*Registration() calls become startPageWorldHooks().

Why

Drift had already started: the dump-trace variant used a different
shouldRegister predicate and allFrames value, so the "same" pattern was
silently divergent. Adding a fifth page-world hook meant touching 4 files + the
manifest + the background dispatch by hand. The divergent allFrames /
eligibility are now explicit, asserted config — drift surfaces in the table
test instead of hiding.

Behavior

Unchanged. Same script ids, bundles, frame scopes, persistAcrossSessions,
log strings, and inject-* message types. Net ~255 fewer production LOC.

Testing

  • bun run check (biome + eslint) clean
  • bun run typecheck clean
  • Full suite: 107 suites / 2023 tests pass (the four per-feature registration
    tests are replaced by page-world-hook.test.ts for the generic lifecycle +
    page-world-hooks.test.ts pinning the per-hook config and dispatch routing)
  • bun run build succeeds, including the background.js purity canary check;
    all four page-world bundles still emit

🤖 Generated with Claude Code

…ctory

The webdriver-probe, checkout-checkbox-defense, shadow-root-probe, and
dump-trace-bridge features each shipped the same register/isRegistered/
unregister/sync/start lifecycle (~80 LOC ×4) plus, for three of them, a
byte-identical executeScript({world:"MAIN"}) inject handler in background.ts
(~27 LOC ×3). The pattern had already drifted — the dump-trace variant used a
different shouldRegister predicate and allFrames value — and adding a fifth
hook meant getting 4 files + the dispatch right by hand.

Introduce `createPageWorldHook({ scriptId, scriptFile, logLabel, allFrames,
shouldRegister, subscribe })` (lib/page-world-hook.ts) that owns the
registration lifecycle once, plus `injectPageWorldScript` for the on-demand
fallback. The four hooks become four config objects in a single table
(lib/page-world-hooks.ts); background.ts calls `startPageWorldHooks()` and a
table-driven `dispatchPageWorldInject()`. The divergent allFrames/eligibility
are now explicit, asserted config — drift surfaces in the table test instead
of hiding.

Net ~255 fewer production LOC. Behavior unchanged: same script ids, bundles,
frame scopes, log strings, and inject message types.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@vercel

vercel Bot commented Jun 9, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agent-browser-shield-demo-site Ready Ready Preview, Comment Jun 9, 2026 11:27pm

Request Review

@twschiller twschiller merged commit 96bcded into main Jun 9, 2026
7 checks passed
@twschiller twschiller deleted the refactor/page-world-hook-factory branch June 9, 2026 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant