mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Auto merge of #22697 - jdm:user-script, r=asajeffrey
Don't panic when executing userscripts - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #22685 - [x] These changes do not require tests because there is no automation for testing userscripts. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/22697) <!-- Reviewable:end -->
This commit is contained in:
commit
7400219adf
1 changed files with 18 additions and 8 deletions
|
@ -3,9 +3,10 @@
|
||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use crate::dom::bindings::inheritance::Castable;
|
use crate::dom::bindings::inheritance::Castable;
|
||||||
|
use crate::dom::bindings::refcounted::Trusted;
|
||||||
use crate::dom::globalscope::GlobalScope;
|
use crate::dom::globalscope::GlobalScope;
|
||||||
use crate::dom::htmlheadelement::HTMLHeadElement;
|
use crate::dom::htmlheadelement::HTMLHeadElement;
|
||||||
use crate::dom::node::Node;
|
use crate::dom::node::document_from_node;
|
||||||
use js::jsval::UndefinedValue;
|
use js::jsval::UndefinedValue;
|
||||||
use servo_config::opts;
|
use servo_config::opts;
|
||||||
use std::fs::{read_dir, File};
|
use std::fs::{read_dir, File};
|
||||||
|
@ -13,14 +14,18 @@ use std::io::Read;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub fn load_script(head: &HTMLHeadElement) {
|
pub fn load_script(head: &HTMLHeadElement) {
|
||||||
if let Some(ref path_str) = opts::get().userscripts {
|
let path_str = match opts::get().userscripts.clone() {
|
||||||
let node = head.upcast::<Node>();
|
Some(p) => p,
|
||||||
let doc = node.owner_doc();
|
None => return,
|
||||||
let win = doc.window();
|
};
|
||||||
|
let doc = document_from_node(head);
|
||||||
|
let win = Trusted::new(doc.window());
|
||||||
|
doc.add_delayed_task(task!(UserScriptExecute: move || {
|
||||||
|
let win = win.root();
|
||||||
let cx = win.get_cx();
|
let cx = win.get_cx();
|
||||||
rooted!(in(cx) let mut rval = UndefinedValue());
|
rooted!(in(cx) let mut rval = UndefinedValue());
|
||||||
|
|
||||||
let path = PathBuf::from(path_str);
|
let path = PathBuf::from(&path_str);
|
||||||
let mut files = read_dir(&path)
|
let mut files = read_dir(&path)
|
||||||
.expect("Bad path passed to --userscripts")
|
.expect("Bad path passed to --userscripts")
|
||||||
.filter_map(|e| e.ok())
|
.filter_map(|e| e.ok())
|
||||||
|
@ -35,7 +40,12 @@ pub fn load_script(head: &HTMLHeadElement) {
|
||||||
f.read_to_end(&mut contents).unwrap();
|
f.read_to_end(&mut contents).unwrap();
|
||||||
let script_text = String::from_utf8_lossy(&contents);
|
let script_text = String::from_utf8_lossy(&contents);
|
||||||
win.upcast::<GlobalScope>()
|
win.upcast::<GlobalScope>()
|
||||||
.evaluate_js_on_global_with_result(&script_text, rval.handle_mut());
|
.evaluate_script_on_global_with_result(
|
||||||
|
&script_text,
|
||||||
|
&file.to_string_lossy(),
|
||||||
|
rval.handle_mut(),
|
||||||
|
1,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue