From f5c09b9d1461ddd5c2bc8aaae49089af99e9aab1 Mon Sep 17 00:00:00 2001 From: Beksultan Date: Mon, 15 Sep 2025 14:42:49 +0600 Subject: [PATCH 1/5] Add functions to set user agent based on OS --- libs/webview/include/webview.h | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/libs/webview/include/webview.h b/libs/webview/include/webview.h index 62ac14b..d4a313c 100644 --- a/libs/webview/include/webview.h +++ b/libs/webview/include/webview.h @@ -265,6 +265,13 @@ WEBVIEW_API void webview_set_title(webview_t w, const char *title); WEBVIEW_API void webview_set_size(webview_t w, int width, int height, webview_hint_t hints); +/** + * @param w The webview instance. + * @param user_agent The new user agent. +*/ + +WEBVIEW_API void webview_set_user_agent(webview_t w, const char *user_agent); + /** * Navigates webview to the given URL. URL may be a properly encoded data URI. * @@ -1010,6 +1017,8 @@ if (status === 0) {\ void init(const std::string &js) { init_impl(js); } void eval(const std::string &js) { eval_impl(js); } + void set_user_agent(const std::string &ua) { set_user_agent_impl(ua); } + protected: virtual void navigate_impl(const std::string &url) = 0; virtual void *window_impl() = 0; @@ -1023,6 +1032,7 @@ if (status === 0) {\ virtual void set_html_impl(const std::string &html) = 0; virtual void init_impl(const std::string &js) = 0; virtual void eval_impl(const std::string &js) = 0; + virtual void set_user_agent_impl(const std::string &ua) = 0; virtual void on_message(const std::string &msg) { auto seq = json_parse(msg, "id", 0); @@ -1352,6 +1362,16 @@ class gtk_webkit_engine : public engine_base { } } + void set_user_agent_impl(const std::string &ua) override { + if (!m_webview) return; + + WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(m_webview)); + if (!settings) return; + + webkit_settings_set_user_agent(settings, ua.c_str()); + webkit_web_view_set_settings(WEBKIT_WEB_VIEW(m_webview), settings); +} + void navigate_impl(const std::string &url) override { webkit_web_view_load_uri(WEBKIT_WEB_VIEW(m_webview), url.c_str()); } @@ -1718,6 +1738,23 @@ class cocoa_wkwebview_engine : public engine_base { nullptr); } + void set_user_agent_impl(const std::string &ua) override { + objc::autoreleasepool pool; + + if (!m_webview) return; + + id configuration = objc::msg_send(m_webview, "configuration"_sel); + if (!configuration) return; + + id uaString = objc::msg_send("NSString"_cls, + "stringWithUTF8String:"_sel, + ua.c_str()); + + objc::msg_send(configuration, + "setApplicationNameForUserAgent:"_sel, + uaString); +} + private: id create_app_delegate() { objc::autoreleasepool arp; @@ -3552,6 +3589,11 @@ WEBVIEW_API void webview_set_size(webview_t w, int width, int height, static_cast(w)->set_size(width, height, hints); } +WEBVIEW_API void webview_set_user_agent(webview_t w, const char *user_agent) { + if (!w || !user_agent) return; + static_cast(w)->set_user_agent(user_agent); +} + WEBVIEW_API void webview_navigate(webview_t w, const char *url) { static_cast(w)->navigate(url); } From 36351aefc647d99828cb382c0e1579cc66c19784 Mon Sep 17 00:00:00 2001 From: Beksultan Date: Mon, 15 Sep 2025 14:43:18 +0600 Subject: [PATCH 2/5] Add function to connect code from C and go --- webview.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/webview.go b/webview.go index 7cf0d9f..fd7e210 100644 --- a/webview.go +++ b/webview.go @@ -25,16 +25,17 @@ void CgoWebViewUnbind(webview_t w, const char *name); */ import "C" import ( - _ "github.com/webview/webview_go/libs/mswebview2" - _ "github.com/webview/webview_go/libs/mswebview2/include" - _ "github.com/webview/webview_go/libs/webview" - _ "github.com/webview/webview_go/libs/webview/include" "encoding/json" "errors" "reflect" "runtime" "sync" "unsafe" + + _ "github.com/webview/webview_go/libs/mswebview2" + _ "github.com/webview/webview_go/libs/mswebview2/include" + _ "github.com/webview/webview_go/libs/webview" + _ "github.com/webview/webview_go/libs/webview/include" ) func init() { @@ -122,6 +123,9 @@ type WebView interface { // Removes a callback that was previously set by Bind. Unbind(name string) error + + // Sets a user agent based on OS + SetUserAgent(ua string) error } type webview struct { @@ -329,3 +333,10 @@ func (w *webview) Unbind(name string) error { C.CgoWebViewUnbind(w.w, cname) return nil } + +func (w *webview) SetUserAgent(ua string) error { + cua := C.CString(ua) + defer C.free(unsafe.Pointer(cua)) + C.webview_set_user_agent(w.w, cua) + return nil +} From 5addd0f7cb26a44d09c166accd29696079c48fd9 Mon Sep 17 00:00:00 2001 From: Beksultan Date: Mon, 15 Sep 2025 14:43:42 +0600 Subject: [PATCH 3/5] Update main.go file to show work of user agent --- examples/bind/main.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/bind/main.go b/examples/bind/main.go index 3374f84..000be83 100644 --- a/examples/bind/main.go +++ b/examples/bind/main.go @@ -2,12 +2,17 @@ package main import webview "github.com/webview/webview_go" -const html = ` +const html = `

UserAgent:

+
+
You tapped 0 time(s).