Winning the race against page script
A hook is only useful if it is in place before the function is called. Page scripts often fingerprint within milliseconds of load, so injecting after the fact is too late. addScriptToEvaluateOnNewDocument solves this by registering your source with the browser; on every new document the browser evaluates it first, in the page main world, before handing control to the page. This is how automation frameworks and instrumentation tools install their shims.
How you call it
You speak CDP over a WebSocket (or via a binding like Playwright/Puppeteer's addInitScript, which wraps this exact command). You send the command once with your script source; the browser replays it on every navigation until you remove the identifier it returns. Because it runs in the main world, your code can reassign HTMLCanvasElement.prototype.toDataURL and the page will call your version -- solving the isolated-world problem entirely.
What it does not solve
Pre-load injection wins the timing race but not the detection race. Two problems remain. First, the wrapper you install is still a JavaScript function that reveals itself via toString and property descriptors. Second, your script runs in the top frame, but sandboxed widgets such as Cloudflare Turnstile live in out-of-process iframes your injection never reaches. Observing a script without perturbing it needs engine-level hooks.
