script/dom/
userscripts.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use js::jsval::UndefinedValue;
6use script_bindings::root::DomRoot;
7
8use crate::dom::html::htmlheadelement::HTMLHeadElement;
9use crate::dom::node::NodeTraits;
10use crate::dom::window::Window;
11use crate::realms::enter_auto_realm;
12
13pub(crate) fn load_script(head: &HTMLHeadElement) {
14    let doc = head.owner_document();
15    let userscripts = doc.window().userscripts().to_owned();
16    if userscripts.is_empty() {
17        return;
18    }
19    let win = DomRoot::from_ref(doc.window());
20    doc.add_delayed_task(task!(UserScriptExecute: |cx, win: DomRoot<Window>| {
21        let global_scope = win.as_global_scope();
22        let mut realm = enter_auto_realm(cx, global_scope);
23        let cx = &mut realm.current_realm();
24
25        rooted!(&in(cx) let mut rval = UndefinedValue());
26        for user_script in userscripts {
27            _ = global_scope.evaluate_js_on_global(
28                cx,
29                user_script.script().into(),
30                &user_script.source_file().map(|path| path.to_string_lossy().to_string()).unwrap_or_default(),
31                None,
32                rval.handle_mut(),
33            );
34        }
35    }));
36}