servo/components/script/dom/userscripts.rs
Tony 5a76906d64
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>
2025-03-27 03:00:08 +00:00

45 lines
1.6 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* 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::rc::Rc;
use js::jsval::UndefinedValue;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::str::DOMString;
use crate::dom::htmlheadelement::HTMLHeadElement;
use crate::dom::htmlscriptelement::SourceCode;
use crate::dom::node::NodeTraits;
use crate::script_module::ScriptFetchOptions;
use crate::script_runtime::CanGc;
pub(crate) fn load_script(head: &HTMLHeadElement) {
let doc = head.owner_document();
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());
for user_script in userscripts {
let script_text = SourceCode::Text(
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,
&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),
global_scope.api_base_url(),
CanGc::note(),
);
}
}));
}