Refactor: collapse 4× page-world injection boilerplate into a hook factory#239
Merged
Conversation
…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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Four page-world (
world: "MAIN") features —webdriver-probe,checkout-checkbox-defense,shadow-root-probe, anddump-trace-bridge—each shipped the same four-part shape: a
*-registration.tswith an ~80 LOCregister/isRegistered/unregister/sync/startlifecycle keyed on atoggle, a
*-source.tspage-world install fn, a thin entry file, and (forthree of them) a byte-identical
executeScript({ world: "MAIN", func })inject-*handler inbackground.ts.This collapses the duplicated lifecycle and dispatch into:
lib/page-world-hook.ts—createPageWorldHook({ scriptId, scriptFile, logLabel, allFrames, shouldRegister, subscribe })owns the register/syncreconcile once, plus
injectPageWorldScript(sender, installFn, messageType)for the on-demand fallback.
lib/page-world-hooks.ts— the four hooks as four config objects in onetable,
startPageWorldHooks(), and a table-drivendispatchPageWorldInject().background.ts— the threeinject-*ifblocks become one dispatchcall; the four
start*Registration()calls becomestartPageWorldHooks().Why
Drift had already started: the dump-trace variant used a different
shouldRegisterpredicate andallFramesvalue, so the "same" pattern wassilently 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) cleanbun run typecheckcleantests are replaced by
page-world-hook.test.tsfor the generic lifecycle +page-world-hooks.test.tspinning the per-hook config and dispatch routing)bun run buildsucceeds, including the background.js purity canary check;all four page-world bundles still emit
🤖 Generated with Claude Code