Files
servo/components/script/dom/userscripts.rs
Mukilan Thiyagarajan 79ed814ec1 libservo: Expand the UserContentManager API. (#42288)
The patch adds the following functionality to the per-WebView
`UserContentManager` API.
- Removing a `UserScript` at that was previously added.
- Adding a new `UserStyleSheet` representing a user-origin style sheet.
allow removing user script
- Removing a previously added `UserStyleSheet`.

There might be scope for some improvements in the API:
- `UserScript` and `UserStyleSheet` have different ways of representing
the source location - a `PathBuf` and `Url` respectively. This is due to
how those values are used by the underlying evaluation APIs in script
and stylo. More investigation is needed here and could be addressed in
future patches.

Testing: New unit tests are added for the user stylesheet APIs. Existing
tests have been updated to test the removal of user scripts.
2026-02-05 13:26:54 +00:00

36 lines
1.2 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 js::jsval::UndefinedValue;
use script_bindings::root::DomRoot;
use crate::dom::html::htmlheadelement::HTMLHeadElement;
use crate::dom::node::NodeTraits;
use crate::dom::window::Window;
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 win = DomRoot::from_ref(doc.window());
doc.add_delayed_task(task!(UserScriptExecute: |win: DomRoot<Window>| {
let cx = win.get_cx();
rooted!(in(*cx) let mut rval = UndefinedValue());
let global_scope = win.as_global_scope();
for user_script in userscripts {
_ = global_scope.evaluate_js_on_global(
user_script.script().into(),
&user_script.source_file().map(|path| path.to_string_lossy().to_string()).unwrap_or_default(),
None,
rval.handle_mut(),
CanGc::note(),
);
}
}));
}