From 7e642effd00b14dc6c9d9f7e6fdb990bdfcfb6dd Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 18 Nov 2024 19:30:46 +0800 Subject: [PATCH] Allow changing user agent through cli --- src/compositor.rs | 2 +- src/config.rs | 15 +++++++++++++-- src/verso.rs | 7 ++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/compositor.rs b/src/compositor.rs index 41697e06..2f081940 100644 --- a/src/compositor.rs +++ b/src/compositor.rs @@ -17,7 +17,7 @@ use euclid::{vec2, Point2D, Scale, Size2D, Transform3D, Vector2D}; use gleam::gl; use ipc_channel::ipc::{self, IpcSharedMemory}; use log::{debug, error, trace, warn}; -use profile_traits::time::{self as profile_time, profile, ProfilerCategory}; +use profile_traits::time::{self as profile_time, ProfilerCategory}; use profile_traits::{mem, time, time_profile}; use script_traits::CompositorEvent::{MouseButtonEvent, MouseMoveEvent, TouchEvent, WheelEvent}; use script_traits::{ diff --git a/src/config.rs b/src/config.rs index bdac2af2..696d38d7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -39,6 +39,8 @@ pub struct CliArgs { /// Path to resource directory. If None, Verso will try to get default directory. And if that /// still doesn't exist, all resource configuration will set to default values. pub resource_dir: Option, + /// Override the user agent + pub user_agent: Option, } /// Configuration of Verso instance. @@ -91,6 +93,13 @@ fn parse_cli_args() -> Result { "out.html", ); + opts.optopt( + "", + "user-agent", + "Override the user agent", + "'VersoView/1.0'", + ); + opts.optopt( "w", "width", @@ -159,6 +168,8 @@ fn parse_cli_args() -> Result { None }; + let user_agent = matches.opt_str("user-agent"); + let mut window_attributes = winit::window::Window::default_attributes(); let width = matches.opt_get::("width").unwrap_or_else(|e| { @@ -216,12 +227,12 @@ fn parse_cli_args() -> Result { window_attributes, devtools_port, profiler_settings, + user_agent, }) } impl Config { - /// Create a new configuration for creating Verso instance. It must provide the path of - /// resources directory. + /// Create a new configuration for creating Verso instance. pub fn new() -> Self { let mut opts = default_opts(); let args = parse_cli_args().unwrap_or_default(); diff --git a/src/verso.rs b/src/verso.rs index d222e79a..1b4b02cb 100644 --- a/src/verso.rs +++ b/src/verso.rs @@ -108,6 +108,12 @@ impl Verso { let initial_url = config.args.url.clone(); let with_panel = !config.args.no_panel; let window_settings = config.args.window_attributes.clone(); + let user_agent: Cow<'static, str> = config + .args + .user_agent + .clone() + .unwrap_or_else(|| default_user_agent_string().to_string()) + .into(); config.init(); // Reserving a namespace to create TopLevelBrowsingContextId. @@ -275,7 +281,6 @@ impl Verso { BluetoothThreadFactory::new(embedder_sender.clone()); // Create resource thread pool - let user_agent: Cow<'static, str> = default_user_agent_string().into(); let (public_resource_threads, private_resource_threads) = resource_thread::new_resource_threads( user_agent.clone(),