blob: af7c2b6d3f1dac1e71a6ddee627ec68136fc7776 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
/* 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 crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlheadelement::HTMLHeadElement;
use crate::dom::node::document_from_node;
use js::jsval::UndefinedValue;
use std::fs::{read_dir, File};
use std::io::Read;
use std::path::PathBuf;
pub fn load_script(head: &HTMLHeadElement) {
let doc = document_from_node(head);
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 = String::from_utf8_lossy(&contents);
win.upcast::<GlobalScope>()
.evaluate_script_on_global_with_result(
&script_text,
&file.to_string_lossy(),
rval.handle_mut(),
1,
);
}
}));
}
|