Skip to content

WebView: handle window.open / target=_blank, and report same-document URL and title changes #113

Closed

Description

@houko

Context

core.WebView reports a page's state through OnLoadStart, OnLoad and OnNavigationStateChange (core/webview.go:40-44). Each one carries a WebNavigation{URL, Title, Loading, CanGoBack, CanGoForward} (internal/driver/webview.go:18-25). The architecture doc says it the same way: "Load start and load each come with a navigation state change" (docs/architecture/native-views.md:20). No desktop backend reports two things a page does.

New windows. No callback exists for a page that asks to open a window, and no backend handles the request:

  • Apple: the handler class registers only WKNavigationDelegate, WKScriptMessageHandler and WKDownloadDelegate (internal/platform/apple/web.go:75). No WKUIDelegate is set, so nothing answers webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures:, and a target=_blank link or window.open does nothing. The navigation policy code already knows such actions exist ("a new window's has no frame", internal/platform/apple/web.go:97-101), but it uses that only to refuse them in a restricted view.
  • Windows: the ICoreWebView2 vtable slots NSGo uses go from NavigationStarting to WebResourceRequested (internal/platform/windows/webview2.go:96-115). listen subscribes only to NavigationStarting, NavigationCompleted and WebMessageReceived (internal/platform/windows/web.go:249-296). Since NewWindowRequested is never subscribed, WebView2 handles the request itself and opens its own top-level popup window. The app does not own that window, and none of the view's handlers see what it loads. The only Navigable check on Windows is in the main view's NavigationStarting handler (internal/platform/windows/web.go:260), so a restricted view's host list does not apply in the popup either.
  • Linux: decide-policy is connected (internal/platform/linux/web.go:278). webPolicyCallback only refuses navigation and new-window actions that a restricted view's config does not allow, and otherwise returns 0 (internal/platform/linux/web.go:492-508, the check is at :497). No handler is connected to create (internal/platform/linux/web.go:276-280), so the request ends there and nothing opens.

Same-document navigations and title changes. State is read only when a full load starts or ends:

  • Apple: didStartProvisionalNavigation and didFinishNavigation (internal/platform/apple/web.go:82-83) call navigated, which reads URL, title, canGoBack and canGoForward (internal/platform/apple/web.go:315-328).
  • Windows: NavigationStarting and NavigationCompleted (internal/platform/windows/web.go:251,267).
  • Linux: load-changed for WEBKIT_LOAD_STARTED and WEBKIT_LOAD_FINISHED (internal/platform/linux/web.go:449-464). The connected signals are load-changed, load-failed, decide-policy, map and script-message-received (internal/platform/linux/web.go:276-280). Nothing observes notify::uri, notify::title or the back-forward list.

history.pushState, replaceState, fragment navigations and document.title = … start neither a provisional load nor a WebView2 NavigationStarting. A single-page app that changes its route or title therefore never fires OnNavigationStateChange, and CanGoBack stays stale after a pushState. A title set after the load finishes is missed as well. The Apple backend is shared with iOS, so both gaps exist there too.

Why it matters for real apps

Kando (github.com/yldm-tech/kando) is a desktop video player with a built-in browser. The user signs in to a video site, browses to a video and hands its URL to the downloader. On its current toolkit Kando does three things that NSGo cannot do today (all in apps/kando/state_browse.go in that repo):

  • When a link or script opens a window, Kando loads its http(s) URL in the same view if the user clicked it. If a script opened it on its own, Kando does nothing, so pop-up ads never get through (:194, :213-218). On NSGo:
    • On macOS and Linux, every "open in new tab" link on these sites does nothing, and so does every sign-in button that uses window.open.
    • On Windows those links open in a separate browser window that the app cannot see or restrict.
  • The toolbar's address field, title, Back and Forward update when the page changes its URL or title (:184-185). Most video sites are SPAs, so clicking a video is a pushState. On NSGo the address field keeps showing the previous page, and "download this" takes the wrong URL.
  • Only http and https are allowed (:203-210). A restricted view (Scope.ProvideAccess with WebHosts returning "*") already refuses other schemes at the top level through the driver's Navigable (internal/driver/webview.go:73-88). The new-window path should be held to the same check.

The only workaround today is a script in InjectedJavaScriptBeforeContent that does four things: wraps window.open, catches clicks on a[target=_blank], hooks history.pushState and replaceState, and watches <title>. It reports everything through window.nsgo.postMessage. It misses form targets and iframes, and the page can see it and defeat it. It also cannot tell a real user gesture from a script, and that bit is exactly what a pop-up policy needs.

Proposal

There are two changes, and neither breaks the current API.

  1. A decide-only new-window callback on WebView. The app chooses what to do: load the URL in this view through its SourceFunc, open it with Scope.OpenURL, or ignore it. The platform never opens a window of its own, whether or not the callback is set.
type WebView struct {
	// ...
	// OnOpenWindow receives what a page asks to open in a new window: a link with target=_blank, window.open, or a form that targets a new window.
	OnOpenWindow func(WebWindowRequest)
}

// WebWindowRequest is a page's request for a new window.
type WebWindowRequest struct {
	URL string
	// UserInitiated is whether a click or key of the user's made it, rather than a script by itself.
	UserInitiated bool
}

Backends:

  • Apple: set a WKUIDelegate, dispatch the request, and answer createWebViewWithConfiguration:… with nil. UserInitiated comes from public API only: the action's navigationType (WKNavigationTypeLinkActivated, WKNavigationTypeFormSubmitted), with WKPreferences.javaScriptCanOpenWindowsAutomatically left at its default so that WebKit's own pop-up blocking still applies.
  • Windows: subscribe to NewWindowRequested, read Uri and IsUserInitiated, then call put_Handled(TRUE).
  • Linux: connect create, read the request and webkit_navigation_action_is_user_gesture from its WebKitNavigationAction, and return NULL.

In a restricted view, a URL that Navigable refuses is reported as OnError, the way top-level navigations already are, and it never reaches OnOpenWindow.

Hosting a real child web view, so that popups relying on window.opener keep working, can come later as a return value. The decide-only form already covers in-app browsers.

  1. Report same-document changes through the existing OnNavigationStateChange. Its doc already says it fires "as it navigates" (core/webview.go:40-41), so no new field is needed:
  • Apple: KVO on the WKWebView's URL, title, canGoBack and canGoForward.
  • Windows: SourceChanged, HistoryChanged and DocumentTitleChanged.
  • Linux: notify::uri, notify::title, and the WebKitBackForwardList's changed.

Each change dispatches EventNavigationStateChange with a fresh WebNavigation. The events are coalesced so that a full load still gives one event at its start and one at its finish.

Minimal repro against today's API:

package main

import (
	"log"

	"github.com/nsgo-dev/nsgo"
	"github.com/nsgo-dev/nsgo/core"
)

const page = `<a href="https://example.com/" target="_blank">target=_blank</a>
<button onclick="window.open('https://example.com/')">window.open</button>
<button onclick="history.pushState({}, '', '/step-2'); document.title = 'Step 2'">pushState</button>`

func main() {
	nsgo.Run(func(cx *core.Scope) core.Node {
		return core.WebView{
			Source: core.WebHTML(page, "https://example.com/"),
			OnNavigationStateChange: func(n core.WebNavigation) {
				log.Printf("state url=%q title=%q back=%v", n.URL, n.Title, n.CanGoBack)
			},
		}
	})
}

What happens:

  • The link and the first button do nothing on macOS and Linux. On Windows they open a WebView2 window that the app does not own.
  • The third button logs nothing on any desktop, even though the view's real URL is now https://example.com/step-2, its title is Step 2 and canGoBack is true.

Acceptance

  • On macOS, Windows and Linux, a target=_blank link, window.open from a click handler, and a form with target="_blank" each reach OnOpenWindow with the URL and UserInitiated == true.
  • window.open from a timer with no user gesture either reaches OnOpenWindow with UserInitiated == false or is dropped by the platform's pop-up blocker. It never arrives as UserInitiated == true.
  • No platform opens a window of its own, whether or not OnOpenWindow is set. On Windows no WebView2 popup window appears.
  • In a view under ProvideAccess, a new-window request to a URL that Navigable refuses arrives as OnError and not as OnOpenWindow, on all three desktops.
  • On all three desktops, history.pushState, history.replaceState, a #fragment link and history.back() within the page each fire OnNavigationStateChange with the new URL and correct CanGoBack and CanGoForward.
  • Setting document.title fires OnNavigationStateChange with the new Title, including a title set after the load finished.
  • A full page load still fires one OnNavigationStateChange at its start and one at its finish, with no duplicate events from the new observers.
  • The gallery's WebView demo exercises both changes, and docs/architecture/native-views.md records the per-target behaviour.

Note

This is separate from #33, which covers script results, downloads, headers, Incognito, custom schemes and trusted input. It is also separate from the roadmap's "More of a web view's loads" row (docs/roadmap.md:27). Neither one mentions new windows or same-document navigations. #31 is about the app's own windows, not windows a page asks for.

Activity

  1. self-assigned this
    on Nov 20, 2026
  2. houko commented on Nov 20, 2026

    @houko
    CollaboratorAuthor

    Reviewed against current origin/main. The three desktop backends still lack a decide-only new-window callback and same-document/title observers. A complete implementation requires WKUIDelegate/KVO on Apple, WebView2 NewWindowRequested/SourceChanged/HistoryChanged/DocumentTitleChanged on Windows, and WebKit create/notify/list observers on Linux, plus gesture classification, access-policy routing, event coalescing, tests and gallery/docs coverage. This is a coordinated multi-backend webview change, so I am leaving the issue open rather than opening a partial backend implementation.

  3. houko commented on Nov 21, 2026

    @houko
    CollaboratorAuthor

    The requested WebView behavior is on main. #1663 and #1730 cover popup routing, user-gesture classification, access refusal, and suppression of native windows. #1654, #1657, and #1661 cover the navigation callback on Apple, Linux, and Windows.

    The native tests exercise link, script, form, and timer window requests, along with History API changes, fragments, title changes, and one start and finish per full load. The gallery's Media page demonstrates both callbacks and the native views architecture document describes the platform behavior.

    The issue's popup and navigation checks passed in the platform PRs. PR #1730 records a separate Windows two-view reload run that exited with 0x8000ffff after both views completed their navigation steps. No further implementation of the requested callbacks is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions