Browser

JavaScript Execution and Storage

Methods for executing custom JavaScript on the page and for reading, writing, or deleting browser storage (cookies, localStorage, sessionStorage, IndexedDB).

Methods for executing custom JavaScript on the page and for reading, writing, or deleting browser storage (cookies, localStorage, sessionStorage, IndexedDB).

ExecuteJavaScriptStr executes a JavaScript snippet and returns the result as a string. ExecuteJavaScriptObj returns the result as a dynamic object for accessing structured data. RunGet, RunSet, and RunDelete operate on browser storage types via the WebsiteStorageType enum. GetStorageData retrieves the result of a storage read operation. These methods are useful for manipulating page state that cannot be reached through the standard DOM interaction API.

WARNING

ExecuteJavaScriptStr and ExecuteJavaScriptObj only work when the browser session uses Selenium or Puppeteer. On an OttoMagic session, Manifest V3's content security policy blocks running arbitrary, dynamically-constructed JavaScript from the extension - both methods publish a GPALEventType.ERROR and return null, and GPAL.MagicHelper.ExecuteJavaScript has been removed entirely. If you need code to run on every page in an OttoMagic session, use InjectScript instead.

Examples

GPAL Fluent: High-level fluent C# API

//JavaScript results are stored internally after ExecuteJavaScriptStr or ExecuteJavaScriptObj - retrieve them with GetStorageData. For structured JS results, use ExecuteJavaScriptObj and access properties on the dynamic result.

// Execute JavaScript and get a string result string title; GPAL.Browser .GoTo("https://example.com") .ExecuteJavaScriptStr("return document.title;") .GetStorageData(out title); // Clear localStorage before navigating GPAL.Browser .RunDelete(WebsiteStorageType.localStorage) .GoTo("https://example.com"); // Set a cookie value GPAL.Browser .RunSet(WebsiteStorageType.cookie) .GoTo("https://example.com");