Scripting

Inject Script

Register a script to run automatically on every new document load, until cleared.

Register a script to run automatically on every new document load, until cleared.

Registers the given JavaScript (via CDP Page.addScriptToEvaluateOnNewDocument) so it runs at the start of every subsequent document load - not just once. Can be called before the first .GoTo()/.Get() (so it is active from the very first page load) or at any later point in the workflow (applies to subsequent navigations). The script persists across navigations until ClearInjectedScripts is called. A GPALFile may be supplied instead of a raw string; if it resolves to multiple filenames (e.g. via a wildcard), each file's contents is loaded and injected as a separate script.

TIP

This is a general-purpose extensibility primitive (not a 'stealth' feature) - useful for things like polyfills, instrumentation hooks, or page-environment patches that need to exist before a site's own scripts run. See also Clear Injected Scripts.

WARNING

InjectScript via OttoMagic depends on the browser extension userScripts API (chrome.userScripts / browser.userScripts). In Chrome and Edge, this API is hidden until the user manually enables it for the extension: 1. Open chrome://extensions (or edge://extensions). 2. Enable 'Developer mode' using the toggle in the top-right corner. 3. Find the OttoMagic extension and click 'Details'. 4. Enable the 'Allow User Scripts' toggle. Without this one-time step, calls to InjectScript via OttoMagic will fail because chrome.userScripts is undefined. Firefox does not require this manual step - browser.userScripts is available to the extension by default. This setup is not required for InjectScript via Selenium or Puppeteer (CDP-based injection, Chrome/Edge only).

Examples

GPAL Fluent: High-level fluent C# API

//Either overload returns the fluent browser interface, so it can be called immediately after GPAL.Browser(...) (before the first GoTo/Get) or inserted anywhere later in the workflow. If a GPALFile resolves to multiple filenames, each file is read and registered as its own injected script.

// raw script string browser.InjectScript("window.__hooked = true;"); // or from one or more files (GPALFile.WithFileName supports wildcards) browser.InjectScript(GPAL.File.WithFileName("hooks/*.js"));

MagicHelper: MagicHelper utility library

//On the OttoMagic side, InjectScript registers the script via the browser extension's BROWSER.userScripts.register() API with world: 'MAIN' and runAt: 'document_start', so it runs on every new document across Chrome, Edge, and Firefox. The generated script id is tracked by the extension (Globals.injectedScriptIds) so ClearInjectedScripts can remove it later. See the Manual Setup warning below - Chrome and Edge require a one-time developer setting before chrome.userScripts is available.

GPAL.MagicHelper.InjectScript("window.__hooked = true;");

Puppeteer Client: Puppeteer-style C# client

browser.PuppeteerClient.InjectScript("window.__hooked = true;").Execute();

Puppeteer Communicator: Low-level communicator

//InjectScriptTracked wraps EnableFetchScriptInjection (Page.addScriptToEvaluateOnNewDocument for Puppeteer, the equivalent Selenium CDP command otherwise) and records the returned CDP script identifier so it can later be removed by ClearInjectedScripts.

string identifier = await browser.PuppeteerCommunicator.InjectScriptTracked("window.__hooked = true;", sessionId);

REST Client: Direct REST/HTTP client

GPAL.OttoMagicClient.InjectScript("window.__hooked = true;").Execute();