aboutsummaryrefslogtreecommitdiffstats
path: root/ports
diff options
context:
space:
mode:
Diffstat (limited to 'ports')
-rw-r--r--ports/servoshell/desktop/app.rs27
-rw-r--r--ports/servoshell/egl/android/simpleservo.rs1
-rw-r--r--ports/servoshell/egl/ohos/simpleservo.rs1
-rw-r--r--ports/servoshell/prefs.rs8
4 files changed, 36 insertions, 1 deletions
diff --git a/ports/servoshell/desktop/app.rs b/ports/servoshell/desktop/app.rs
index b8667ec3b4f..6df9945ee7a 100644
--- a/ports/servoshell/desktop/app.rs
+++ b/ports/servoshell/desktop/app.rs
@@ -6,6 +6,7 @@
use std::cell::Cell;
use std::collections::HashMap;
+use std::path::Path;
use std::rc::Rc;
use std::time::Instant;
use std::{env, fs};
@@ -16,6 +17,7 @@ use servo::config::opts::Opts;
use servo::config::prefs::Preferences;
use servo::servo_config::pref;
use servo::servo_url::ServoUrl;
+use servo::user_content_manager::{UserContentManager, UserScript};
use servo::webxr::glwindow::GlWindowDiscovery;
#[cfg(target_os = "windows")]
use servo::webxr::openxr::{AppInfo, OpenXrDiscovery};
@@ -146,6 +148,13 @@ impl App {
}
}
+ let mut user_content_manager = UserContentManager::new();
+ for script in load_userscripts(self.servoshell_preferences.userscripts_directory.as_deref())
+ .expect("Loading userscripts failed")
+ {
+ user_content_manager.add_script(script);
+ }
+
let servo = Servo::new(
self.opts.clone(),
self.preferences.clone(),
@@ -153,6 +162,7 @@ impl App {
embedder,
Rc::new(UpcastedWindow(window.clone())),
self.servoshell_preferences.user_agent.clone(),
+ user_content_manager,
);
servo.setup_logging();
@@ -442,3 +452,20 @@ impl ApplicationHandler<WakerEvent> for App {
self.suspended.set(true);
}
}
+
+fn load_userscripts(userscripts_directory: Option<&Path>) -> std::io::Result<Vec<UserScript>> {
+ let mut userscripts = Vec::new();
+ if let Some(userscripts_directory) = &userscripts_directory {
+ let mut files = std::fs::read_dir(userscripts_directory)?
+ .map(|e| e.map(|entry| entry.path()))
+ .collect::<Result<Vec<_>, _>>()?;
+ files.sort();
+ for file in files {
+ userscripts.push(UserScript {
+ script: std::fs::read_to_string(&file)?,
+ source_file: Some(file),
+ });
+ }
+ }
+ Ok(userscripts)
+}
diff --git a/ports/servoshell/egl/android/simpleservo.rs b/ports/servoshell/egl/android/simpleservo.rs
index 6ef42c95789..348faa8d4e0 100644
--- a/ports/servoshell/egl/android/simpleservo.rs
+++ b/ports/servoshell/egl/android/simpleservo.rs
@@ -99,6 +99,7 @@ pub fn init(
embedder_callbacks,
window_callbacks.clone(),
None,
+ Default::default(),
);
APP.with(|app| {
diff --git a/ports/servoshell/egl/ohos/simpleservo.rs b/ports/servoshell/egl/ohos/simpleservo.rs
index 708140e19a8..621b2f446ed 100644
--- a/ports/servoshell/egl/ohos/simpleservo.rs
+++ b/ports/servoshell/egl/ohos/simpleservo.rs
@@ -131,6 +131,7 @@ pub fn init(
embedder_callbacks,
window_callbacks.clone(),
None, /* user_agent */
+ Default::default(),
);
let app_state = RunningAppState::new(
diff --git a/ports/servoshell/prefs.rs b/ports/servoshell/prefs.rs
index 925ea65ba60..5157615f44a 100644
--- a/ports/servoshell/prefs.rs
+++ b/ports/servoshell/prefs.rs
@@ -56,6 +56,9 @@ pub(crate) struct ServoShellPreferences {
pub output_image_path: Option<String>,
/// Whether or not to exit after Servo detects a stable output image in all WebViews.
pub exit_after_stable_image: bool,
+ /// Where to load userscripts from, if any.
+ /// and if the option isn't passed userscripts won't be loaded.
+ pub userscripts_directory: Option<PathBuf>,
}
impl Default for ServoShellPreferences {
@@ -74,6 +77,7 @@ impl Default for ServoShellPreferences {
user_agent: None,
output_image_path: None,
exit_after_stable_image: false,
+ userscripts_directory: None,
}
}
}
@@ -622,6 +626,9 @@ pub(crate) fn parse_command_line_arguments(args: Vec<String>) -> ArgumentParsing
screen_size_override,
output_image_path,
exit_after_stable_image: exit_after_load,
+ userscripts_directory: opt_match
+ .opt_default("userscripts", "resources/user-agent-js")
+ .map(PathBuf::from),
..Default::default()
};
@@ -636,7 +643,6 @@ pub(crate) fn parse_command_line_arguments(args: Vec<String>) -> ArgumentParsing
time_profiling,
time_profiler_trace_path: opt_match.opt_str("profiler-trace-path"),
nonincremental_layout,
- userscripts: opt_match.opt_default("userscripts", "resources/user-agent-js"),
user_stylesheets,
hard_fail: opt_match.opt_present("f") && !opt_match.opt_present("F"),
webdriver_port,