-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] Custom user-agent #4284
Comments
Since this most likely won't make it into v1, here's how you can do it on the latest tauri version: window.with_webview(|webview| {
#[cfg(windows)]
unsafe {
use webview2_com::Microsoft::Web::WebView2::Win32::ICoreWebView2Settings2;
use windows::core::Interface;
let settings: ICoreWebView2Settings2 = webview
.controller()
.CoreWebView2()
.unwrap()
.Settings()
.unwrap()
.cast()
.unwrap();
settings
.SetUserAgent("my custom user agent".to_string())
.unwrap();
}
#[cfg(target_os = "linux")]
{
use webkit2gtk::{WebViewExt, SettingsExt};
let webview = webview.inner();
let settings = webview.settings().unwrap();
settings.set_user_agent(Some("my user agent"));
}
// untested
#[cfg(target_os = "macos")]
unsafe {
use objc::msg_send;
use objc_foundation::{NSString, INSString};
let () = msg_send![webview.inner(), setCustomUserAgent: NSString::from_str("my custom user agent")];
}
}); |
It's definitely not making it into v1 |
Well the wry api was audited as far as i can see, and that was enough for other things 🤷 That's why i now always go with "most likely", "probably", etc., because i was wrong a couple of times already 😂 |
Works like a charm |
Where to write? |
I have included like this in my project: tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
my_function,
])
.setup(|app| {
let window = WindowBuilder::new(
app,
"main-window".to_string(),
tauri::WindowUrl::App("index.html".into()),
)
.build()?;
window.with_webview(|webview| {
#[cfg(windows)]
unsafe {
use webview2_com::Microsoft::Web::WebView2::Win32::ICoreWebView2Settings2;
use windows::core::Interface;
let settings: ICoreWebView2Settings2 = webview
.controller()
.CoreWebView2()
.unwrap()
.Settings()
.unwrap()
.cast()
.unwrap();
settings
.SetUserAgent("Chrome".to_string())
.unwrap();
}
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application"); And included the following in my # for webview
[dependencies]
webview2-com = "0.18.0"
# for windows::core
[dependencies.windows]
version = "0.39.0"
features = [
"Data_Xml_Dom",
"Win32_Foundation",
"Win32_Security",
"Win32_System_Threading",
"Win32_UI_WindowsAndMessaging",
] And I am getting this error:
I also tried downgrading to |
@elanzini You have to use the same versions as Tauri does. I got it to work with |
On the last part:
I'm getting below error:
Using |
The snippet above was written for windows 0.37 and webview2-com 0.16. It's definitely possible that something changed in the new versions that broke the script. |
I'm on the run right now so I can't show code examples, but it looks like you can use the new For runtime strings it's a bit tricky and needs some ugly functions (or a crate idk), but that's something I can't show on my phone... |
That worked.
Yours is slightly cleaner 😅 |
This is basically what we use in wry 👍 and also what I had in mind for runtime Strings (it's probably not the actual name, but what I mean is a string you don't know the content of at compile time) |
I'm on the latest tauri on linux and cannot find any up-to-date instructions for this. How do you do this on the latest version? |
@ArrowAced It's not released yet but will be part of v1.2 |
v1.2 is released, can this be found anywhere or was it delayed and i need to rely on the "manual" solution? |
@EtzBetz It's |
Describe the problem
Default
user-agent
isMozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko)
while Safari isMozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15
.No
browser/version
in user-agent make many website treat tauri as out-of-date browser. See #3519 for example.Describe the solution you'd like
Provide a rust API to set custom user-agent. Or at least use a modern browser user-agent.
Alternatives considered
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: