aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/userscripts.rs
diff options
context:
space:
mode:
authorTony <68118705+Legend-Master@users.noreply.github.com>2025-03-27 11:00:08 +0800
committerGitHub <noreply@github.com>2025-03-27 03:00:08 +0000
commit5a76906d64a34ebb0608f64dd558e7457675f9c6 (patch)
treeec570d0a20e591752abe7b63a109139a9b75428f /components/script/dom/userscripts.rs
parent53a2e61fecd42d4d35b7ff5479095cf86e12c1ae (diff)
downloadservo-5a76906d64a34ebb0608f64dd558e7457675f9c6.tar.gz
servo-5a76906d64a34ebb0608f64dd558e7457675f9c6.zip
Allow setting userscripts directly without the need of files (#35388)
* Allow settings userscripts through preferences Signed-off-by: Tony <legendmastertony@gmail.com> * mach fmt instead of cargo fmt Signed-off-by: Tony <legendmastertony@gmail.com> * Fix pref loading not working for array values Signed-off-by: Tony <legendmastertony@gmail.com> * Use pref! in userscripts instead Signed-off-by: Tony <legendmastertony@gmail.com> * Implement the model jdm suggested - Remove userscripts from all places and move it to servoshell - Add in `UserContentManager` struct and passing it through `Servo::new` all the way down to script thread Signed-off-by: Tony <legendmastertony@gmail.com> * Apply suggestions from code review and format Signed-off-by: Tony <legendmastertony@gmail.com> * Revert unrelated change Signed-off-by: Tony <legendmastertony@gmail.com> --------- Signed-off-by: Tony <legendmastertony@gmail.com> Signed-off-by: Tony <68118705+Legend-Master@users.noreply.github.com>
Diffstat (limited to 'components/script/dom/userscripts.rs')
-rw-r--r--components/script/dom/userscripts.rs30
1 files changed, 7 insertions, 23 deletions
diff --git a/components/script/dom/userscripts.rs b/components/script/dom/userscripts.rs
index f48c8cf7f5e..79c2f58dbec 100644
--- a/components/script/dom/userscripts.rs
+++ b/components/script/dom/userscripts.rs
@@ -2,9 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
-use std::fs::{File, read_dir};
-use std::io::Read;
-use std::path::PathBuf;
use std::rc::Rc;
use js::jsval::UndefinedValue;
@@ -19,37 +16,24 @@ use crate::script_runtime::CanGc;
pub(crate) fn load_script(head: &HTMLHeadElement) {
let doc = head.owner_document();
- let path_str = match doc.window().get_userscripts_path() {
- Some(p) => p,
- None => return,
- };
+ let userscripts = doc.window().userscripts().to_owned();
+ if userscripts.is_empty() {
+ return;
+ }
let window = Trusted::new(doc.window());
doc.add_delayed_task(task!(UserScriptExecute: move || {
let win = window.root();
let cx = win.get_cx();
rooted!(in(*cx) let mut rval = UndefinedValue());
- let path = PathBuf::from(&path_str);
- let mut files = read_dir(path)
- .expect("Bad path passed to --userscripts")
- .filter_map(|e| e.ok())
- .map(|e| e.path())
- .collect::<Vec<_>>();
-
- files.sort();
-
- for file in files {
- let mut f = File::open(&file).unwrap();
- let mut contents = vec![];
- f.read_to_end(&mut contents).unwrap();
+ for user_script in userscripts {
let script_text = SourceCode::Text(
- Rc::new(DOMString::from_string(String::from_utf8_lossy(&contents).to_string()))
+ Rc::new(DOMString::from_string(user_script.script))
);
-
let global_scope = win.as_global_scope();
global_scope.evaluate_script_on_global_with_result(
&script_text,
- &file.to_string_lossy(),
+ &user_script.source_file.map(|path| path.to_string_lossy().to_string()).unwrap_or_default(),
rval.handle_mut(),
1,
ScriptFetchOptions::default_classic_script(global_scope),