Skip to content

Add ExecuteScript APIs and PopupRequested event - #276

Open
CypherPotato wants to merge 2 commits into
tryphotino:masterfrom
CypherPotato:feature/execute-script
Open

Add ExecuteScript APIs and PopupRequested event#276
CypherPotato wants to merge 2 commits into
tryphotino:masterfrom
CypherPotato:feature/execute-script

Conversation

@CypherPotato

@CypherPotato CypherPotato commented Jun 6, 2026

Copy link
Copy Markdown

Summary

Adds two browser-control APIs to PhotinoWindow:

  • ExecuteScript(string script)
  • ExecuteScriptAsync(string script, CancellationToken cancellationToken = default)
  • PopupRequested event / RegisterPopupRequestedHandler(...)

The async script API wraps the user script in an internal callback envelope, executes it in the page, and completes a TaskCompletionSource from an internal window.external.sendMessage response. Results are JSON-serialized in JavaScript and converted back to .NET primitives, lists, dictionaries, or null.

PopupRequested exposes browser new-window requests such as window.open(...) and target=_blank. PopupRequestedEventArgs includes the requesting PhotinoWindow, URL, target name, optional X/Y/Width/Height, and a Handled flag. Setting Handled=true asks the native layer to suppress the browser engine's default popup handling.

Native dependency

Depends on tryphotino/photino.Native#179, which adds the Photino_ExecuteScript native export and popup-request callback used by this wrapper.

Before this PR is marked ready, the Photino.Native package dependency should be coordinated with the native PR release so the managed package does not depend on a native package missing the new export/callback contract.

Behavior notes

  • ExecuteScript blocks on ExecuteScriptAsync and rejects synchronous calls from the Photino UI thread to avoid deadlock.
  • Cancellation cancels waiting for the callback; it does not interrupt JavaScript already running in the browser.
  • Non-JSON-serializable or undefined JavaScript results currently return null.
  • If the browser control is not ready yet, the native layer returns false and the managed API throws an ApplicationException instead of crashing or hanging.
  • PopupRequested bounds are nullable on the managed side; platforms that do not provide a value pass null.

Validation

  • dotnet build Photino.NET.sln
  • dotnet build BrowserMcp with local Photino.NET project reference
  • Windows smoke test for ExecuteScriptAsync("return document.title;")
  • Windows smoke test for ExecuteScript("return document.title;")
  • BrowserMcp smoke test using local Photino.NET + local Photino.Native: TITLE=BrowserWindow ExecuteScript Test
  • Windows smoke test for PopupRequested with window.open(...), URL/name/bounds, requesting Window, and Handled=true

@CypherPotato CypherPotato changed the title Add ExecuteScript APIs Add ExecuteScript APIs and PopupRequested event Jun 6, 2026
@CypherPotato
CypherPotato marked this pull request as draft June 6, 2026 22:21
@CypherPotato
CypherPotato marked this pull request as ready for review June 7, 2026 15:24
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