Skip to content

[Feature] Web view: mute a page's audio, clear the stored website data, and append to the platform's user agent #114

Closed

Description

@houko

Context

core.WebView can replace the user agent and nothing more. Its comment at core/webview.go:26 says "UserAgent replaces the platform's.", and each host passes the string through whole: Apple calls setCustomUserAgent: with it (internal/platform/apple/web.go:233-237), Linux calls webkit_settings_set_user_agent (internal/platform/linux/web.go:217-221), and Windows put_UserAgent (internal/platform/windows/web.go:411-417). Windows already reads the platform's default at creation (internal/platform/windows/web.go:225), but only to restore it when UserAgent is empty. An app cannot add a token to the platform's own string without hardcoding the whole string for each engine and version.

WebView has nothing for sound. None of its fields (core/webview.go:19-57) is about audio, and its Ref commands are Reload, GoBack, GoForward, StopLoading, PostMessage, InjectJavaScript and SendInput (internal/driver/webview.go:139-155, core/webview.go:216-238), plus Evaluate (core/webview.go:279). core.Video has Muted/MutedFunc (core/video.go:19-20), but there is no way to mute a web page's sound.

No API clears website data either. All of an app's web views share one store, and it persists. Linux keeps one network session per app ID, with cookies in cookies.sqlite, and its comment says "Cookies outlive the app, as other website data does." (internal/platform/linux/web.go:304-325). Apple builds a plain WKWebViewConfiguration (internal/platform/apple/web.go:251), so it uses the default persistent WKWebsiteDataStore. Scope has no method that removes that data.

The roadmap row "More of a web view's loads" (docs/roadmap.md:27) and #33 cover Incognito, which keeps a view's data in memory. That solves a different problem. The app below wants the login to persist and to be erased only when the user asks. Draft PR #76 (headers, downloads, Incognito, ServeScheme, Env.HasWebView, Env.CanSendInput) does not touch muting, clearing or appending to the user agent.

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 in it, and the app then downloads from that site with the same cookies and user agent. Kando does three things on its current framework that NSGo has no way to do:

  • Mute while hidden. The browser's web view stays mounted while another page of the app shows, so when the user comes back the page is where they left it. A video playing in it keeps playing, so the app mutes the page while it is hidden and unmutes it when the page shows again. With NSGo, nothing stops that video's sound from playing over the app's own player. A script run through Ref.Evaluate that sets muted on <video>/<audio> misses Web Audio, cross-origin iframes and elements the page creates later, and the page can see the change and undo it.
  • "Clear sign-in data". A menu command, after a confirmation, deletes the cookies, storage and caches of the shared store, for example when the user signs out or switches accounts, then reloads the page. A page script cannot reach HttpOnly session cookies, so the page itself offers no workaround.
  • Append to the user agent. A bare WKWebView user agent names no browser, and some sites send it an older page or refuse its sign-in. The app appends Version/26.0 Safari/605.1.15 on macOS only and keeps WebKit's own Mozilla/5.0 (Macintosh; ...) AppleWebKit/... prefix. WebKitGTK's and WebView2's defaults already name their browsers, so the app leaves them unchanged. Replacing the whole string means hardcoding each OS and engine version, which breaks sites that check them.

(The downloader also needs the user agent the page actually sends. That already works through Ref.Evaluate("navigator.userAgent", ...).)

Minimal repro of the user agent gap:

core.WebView{
	Source: core.WebSrc("https://example.com/"),
	// The only option: this becomes the whole string, losing
	// "Mozilla/5.0 (Macintosh; ...) AppleWebKit/605.1.15 (KHTML, like Gecko)".
	UserAgent: "Version/26.0 Safari/605.1.15",
}

And of the audio gap: mount a WebView that plays a <video autoplay> with sound, then hide it while keeping it mounted, for example with Style{Display: core.DisplayNone}. No field or Ref method silences the page.

Proposal

On WebView, following Video's Muted/MutedFunc and the existing SourceFunc/StyleFunc pattern:

// UserAgentSuffix is appended, after a space, to UserAgent, or to the
// platform's own user agent when UserAgent is empty.
UserAgentSuffix string

// Muted silences everything the page plays, as a browser mutes a tab:
// media elements, Web Audio and frames of other sites, without the page
// seeing its elements muted. It lasts across navigations.
Muted     bool
MutedFunc func() bool

For the stored data, a Scope method like ImageSize and NotificationPermission. It is asynchronous because every engine reports completion through a callback:

// ClearWebsiteData deletes what the app's web views stored in its shared,
// persistent store: cookies, local and session storage, IndexedDB,
// service workers and caches. Open pages keep what they hold in memory
// until they load again.
func (cx *Scope) ClearWebsiteData(done func(err error))

Platform mechanisms:

  • User agent suffix: Apple WKWebViewConfiguration.applicationNameForUserAgent, set on the configuration make() builds before the view exists, or customUserAgent = UserAgent + suffix when UserAgent is set. Windows: the default already read at internal/platform/windows/web.go:225 + suffix. WebKitGTK: the settings' default user agent + suffix. Android: WebSettings.getDefaultUserAgent + suffix.
  • Muted: WebKitGTK webkit_web_view_set_is_muted (2.30+). WebView2 ICoreWebView2_8::put_IsMuted. Apple has no public mute API. Safari mutes a tab through WebKit's -[WKWebView _setPageMuted:], which could be used behind respondsToSelector:. If private API is not acceptable, the public setAllMediaPlaybackSuspended:completionHandler: (macOS 12 / iOS 15) pauses media rather than muting it, and the field's doc should say so. Targets with no mechanism at all (Android, web) should be reported in Env or in the field's doc.
  • ClearWebsiteData: Apple -[WKWebsiteDataStore removeDataOfTypes:modifiedSince:completionHandler:] with allWebsiteDataTypes and distantPast. WebKitGTK webkit_website_data_manager_clear on the website data manager of the app's network session. WebView2 ICoreWebView2Profile2::ClearBrowsingDataAll. Android CookieManager.removeAllCookies + WebStorage.deleteAllData + WebView.clearCache.

Acceptance

  • With UserAgentSuffix: "Version/26.0 Safari/605.1.15" and no UserAgent, navigator.userAgent on macOS starts with WebKit's default and ends with the suffix. On Windows and Linux it is the engine's default plus the suffix. With UserAgent set, it is UserAgent plus the suffix.
  • The suffix also appears in the HTTP User-Agent request header, not only in navigator.userAgent.
  • A page playing <video> with sound and a Web Audio oscillator is silent on Windows and Linux while MutedFunc returns true. It plays again when MutedFunc returns false and stays muted across a navigation. HTMLMediaElement.muted still reads false in the page. macOS meets the same check, or its documented limitation is stated.
  • After ClearWebsiteData reports nil, a reload on all three desktops finds none of the cookies (HttpOnly included), localStorage entries or IndexedDB databases the page had set. A failure reaches done as an error.
  • docs/architecture/native-views.md lists, per target, which of the three are supported, and the gallery's WebView demo exercises them.

Activity

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

    @houko
    CollaboratorAuthor

    I audited #114 against the latest origin/main. The persistent-store portion is already implemented as Scope.ClearWebsiteData, with Apple, Linux, Windows, and Android backends and native tests covering cookies and website storage. Incognito storage is also present.

    The remaining request is UserAgentSuffix and page-level mute. A correct suffix must preserve each engine's default user agent in both navigator.userAgent and HTTP requests, including Android and Web, while page mute needs native tab/audio controls that are unavailable on some targets and a documented capability contract. I did not submit a partial field that would replace defaults or mute only media elements. The issue remains open for those cross-target controls and their platform matrix.

  3. houko commented on Nov 21, 2026

    @houko
    CollaboratorAuthor

    Rechecked main after #1792. #1013 added UserAgentSuffix, #1247 added page mute, and Scope.ClearWebsiteData clears the shared store. The gallery exercises all three and the native-view guide states which hosts support each operation.

    The native tests cover the request header and the page's user agent. Linux and Windows use their engines' page-wide mute controls, and Apple suspends media through WKWebView as documented. Linux audio capture covered the media element and Web Audio before and after toggling mute, and the Windows native tests checked mute state across navigation. The Windows audio output capture remains unverified.

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