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 script_bindings::root::DomRoot;
6
7use crate::dom::html::htmlheadelement::HTMLHeadElement;
8use crate::dom::node::NodeTraits;
9use crate::dom::window::Window;
10use crate::realms::enter_auto_realm;
11
12pub(crate) fn load_script(head: &HTMLHeadElement) {
13    let doc = head.owner_document();
14    let userscripts = doc.window().userscripts().to_owned();
15    if userscripts.is_empty() {
16        return;
17    }
18    let win = DomRoot::from_ref(doc.window());
19    doc.add_delayed_task(task!(UserScriptExecute: |cx, win: DomRoot<Window>| {
20        let global_scope = win.as_global_scope();
21        let mut realm = enter_auto_realm(cx, global_scope);
22        let cx = &mut realm.current_realm();
23
24        for user_script in userscripts {
25            _ = global_scope.evaluate_js_on_global(
26                cx,
27                user_script.script().into(),
28                &user_script.source_file().map(|path| path.to_string_lossy().to_string()).unwrap_or_default(),
29                None,
30                None,
31            );
32        }
33    }));
34}