Delay user script execution until DOM is stable.

This commit is contained in:
Josh Matthews 2019-01-15 15:20:25 -05:00
parent 2cf9a00c99
commit 18cfb9378d

View file

@ -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())
@ -37,5 +42,5 @@ pub fn load_script(head: &HTMLHeadElement) {
win.upcast::<GlobalScope>() win.upcast::<GlobalScope>()
.evaluate_js_on_global_with_result(&script_text, rval.handle_mut()); .evaluate_js_on_global_with_result(&script_text, rval.handle_mut());
} }
} }));
} }