servo/components/script/dom/userscripts.rs
Martin Robinson e8f75c9aea
script: Expose node helpers as NodeTraits and give more descriptive names (#34832)
This puts a few commonly used `Node` helpers into a trait (`NodeTraits`)
and gives them more descriptive names and documentation. The renames:

- `document_from_node` -> `NodeTraits::owner_document`
- `window_from_node` -> `NodeTraits::owner_window`
- `stylesheets_owner_from_node<T:` -> `NodeTraits::stylesheet_list_owner`
- `containing_shadow_root` -> `NodeTraits::containing_shadow_root`

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-01-03 18:55:01 +00:00

62 lines
2.1 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::fs::{read_dir, File};
use std::io::Read;
use std::path::PathBuf;
use std::rc::Rc;
use js::jsval::UndefinedValue;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
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 fn load_script(head: &HTMLHeadElement) {
let doc = head.owner_document();
let path_str = match doc.window().get_userscripts_path() {
Some(p) => p,
None => return,
};
let win = Trusted::new(doc.window());
doc.add_delayed_task(task!(UserScriptExecute: move || {
let win = win.root();
let cx = win.get_cx();
rooted!(in(*cx) let mut rval = UndefinedValue());
let path = PathBuf::from(&path_str);
let mut files = read_dir(path)
.expect("Bad path passed to --userscripts")
.filter_map(|e| e.ok())
.map(|e| e.path())
.collect::<Vec<_>>();
files.sort();
for file in files {
let mut f = File::open(&file).unwrap();
let mut contents = vec![];
f.read_to_end(&mut contents).unwrap();
let script_text = SourceCode::Text(
Rc::new(DOMString::from_string(String::from_utf8_lossy(&contents).to_string()))
);
let global = win.upcast::<GlobalScope>();
global.evaluate_script_on_global_with_result(
&script_text,
&file.to_string_lossy(),
rval.handle_mut(),
1,
ScriptFetchOptions::default_classic_script(global),
global.api_base_url(),
CanGc::note(),
);
}
}));
}