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::script_runtime::CanGc;
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: |win: DomRoot<Window>| {
21        let cx = win.get_cx();
22        rooted!(in(*cx) let mut rval = UndefinedValue());
23
24        let global_scope = win.as_global_scope();
25        for user_script in userscripts {
26            _ = global_scope.evaluate_js_on_global(
27                user_script.script().into(),
28                &user_script.source_file().map(|path| path.to_string_lossy().to_string()).unwrap_or_default(),
29                None,
30                rval.handle_mut(),
31                CanGc::note(),
32            );
33        }
34    }));
35}