From 4d2ca2d8e2781787dd1fd0b34888dd5a7e3ebad3 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Sat, 28 Mar 2015 14:59:11 +0530 Subject: [PATCH 1/2] Allow passing a path to --userscripts --- components/script/dom/htmlheadelement.rs | 57 +++++++++++++----------- components/util/opts.rs | 13 +++--- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/components/script/dom/htmlheadelement.rs b/components/script/dom/htmlheadelement.rs index cf1cca4decf..ac906229130 100644 --- a/components/script/dom/htmlheadelement.rs +++ b/components/script/dom/htmlheadelement.rs @@ -19,6 +19,7 @@ use util::opts; use util::resource_files::resources_dir_path; use std::borrow::ToOwned; use std::fs::read_dir; +use std::path::PathBuf; #[dom_struct] pub struct HTMLHeadElement { @@ -51,34 +52,38 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLHeadElement> { Some(htmlelement as &VirtualMethods) } fn bind_to_tree(&self, _tree_in_doc: bool) { - if !opts::get().userscripts { - return; - } + if let Some(ref path_str) = opts::get().userscripts { + let node: &JSRef = NodeCast::from_borrowed_ref(self); + let first_child = node.GetFirstChild().root(); + let doc = node.owner_doc().root(); + let doc = doc.r(); - let node: &JSRef = NodeCast::from_borrowed_ref(self); - let first_child = node.GetFirstChild().root(); - let doc = node.owner_doc().root(); - let doc = doc.r(); - - let mut path = resources_dir_path(); - path.push("user-agent-js"); - let mut files = match read_dir(&path) { - Ok(d) => d.filter_map(|e| e.ok()).map(|e| e.path()).collect::>(), - Err(_) => return - }; - - files.sort(); - - for file in files { - let name = match file.into_os_string().into_string() { - Ok(ref s) if s.ends_with(".js") => "file://".to_owned() + &s[..], - _ => continue + let path = if &**path_str == "" { + let mut p = resources_dir_path(); + p.push("user-agent-js"); + p + } else { + PathBuf::new(path_str) }; - let new_script = doc.CreateElement("script".to_owned()).unwrap().root(); - let new_script = new_script.r(); - new_script.set_string_attribute(&atom!("src"), name); - let new_script_node: &JSRef = NodeCast::from_borrowed_ref(&new_script); - node.InsertBefore(*new_script_node, first_child.r()).unwrap(); + + let mut files = match read_dir(&path) { + Ok(d) => d.filter_map(|e| e.ok()).map(|e| e.path()).collect::>(), + Err(_) => return + }; + + files.sort(); + + for file in files { + let name = match file.into_os_string().into_string() { + Ok(ref s) if s.ends_with(".js") => "file://".to_owned() + &s[..], + _ => continue + }; + let new_script = doc.CreateElement("script".to_owned()).unwrap().root(); + let new_script = new_script.r(); + new_script.set_string_attribute(&atom!("src"), name); + let new_script_node: &JSRef = NodeCast::from_borrowed_ref(&new_script); + node.InsertBefore(*new_script_node, first_child.r()).unwrap(); + } } } } diff --git a/components/util/opts.rs b/components/util/opts.rs index 25bd06e61f4..3942ae0ff8e 100644 --- a/components/util/opts.rs +++ b/components/util/opts.rs @@ -59,7 +59,11 @@ pub struct Opts { pub nonincremental_layout: bool, pub nossl: bool, - pub userscripts: bool, + + /// Where to load userscripts from, if any. An empty string will load from + /// the resources/user-agent-js directory, and if the option isn't passed userscripts + /// won't be loaded + pub userscripts: Option, pub output_file: Option, pub headless: bool, @@ -181,7 +185,7 @@ pub fn default_opts() -> Opts { layout_threads: 1, nonincremental_layout: false, nossl: false, - userscripts: false, + userscripts: None, output_file: None, headless: true, hard_fail: true, @@ -222,7 +226,7 @@ pub fn from_cmdline_args(args: &[String]) -> bool { getopts::optopt("y", "layout-threads", "Number of threads to use for layout", "1"), getopts::optflag("i", "nonincremental-layout", "Enable to turn off incremental layout."), getopts::optflag("", "no-ssl", "Disables ssl certificate verification."), - getopts::optflag("", "userscripts", "Uses userscripts in resources/user-agent-js"), + getopts::optflagopt("", "userscripts", "Uses userscripts in resources/user-agent-js, or a specified full path",""), getopts::optflag("z", "headless", "Headless mode"), getopts::optflag("f", "hard-fail", "Exit on task failure instead of displaying about:failure"), getopts::optflagopt("", "devtools", "Start remote devtools server on port", "6000"), @@ -299,7 +303,6 @@ pub fn from_cmdline_args(args: &[String]) -> bool { let nonincremental_layout = opt_match.opt_present("i"); let nossl = opt_match.opt_present("no-ssl"); - let userscripts = opt_match.opt_present("userscripts"); let mut bubble_inline_sizes_separately = debug_options.contains(&"bubble-widths"); let trace_layout = debug_options.contains(&"trace-layout"); @@ -335,7 +338,7 @@ pub fn from_cmdline_args(args: &[String]) -> bool { layout_threads: layout_threads, nonincremental_layout: nonincremental_layout, nossl: nossl, - userscripts: userscripts, + userscripts: opt_match.opt_default("userscripts", ""), output_file: opt_match.opt_str("o"), headless: opt_match.opt_present("z"), hard_fail: opt_match.opt_present("f"), From f41acb589a3ccaa908608478889f6f58b6b3e36b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Wed, 1 Apr 2015 23:19:38 +0530 Subject: [PATCH 2/2] Give userscripts its own module; panic on broken paths --- components/script/dom/htmlheadelement.rs | 49 +++------------------- components/script/dom/mod.rs | 1 + components/script/dom/userscripts.rs | 52 ++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 44 deletions(-) create mode 100644 components/script/dom/userscripts.rs diff --git a/components/script/dom/htmlheadelement.rs b/components/script/dom/htmlheadelement.rs index ac906229130..cc02c8a0d39 100644 --- a/components/script/dom/htmlheadelement.rs +++ b/components/script/dom/htmlheadelement.rs @@ -3,23 +3,16 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use dom::bindings::codegen::Bindings::HTMLHeadElementBinding; -use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; -use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods; -use dom::bindings::codegen::InheritTypes::{HTMLElementCast, HTMLHeadElementDerived, NodeCast}; -use dom::bindings::js::{JSRef, OptionalRootable, Temporary, RootedReference}; +use dom::bindings::codegen::InheritTypes::{HTMLElementCast, HTMLHeadElementDerived}; +use dom::bindings::js::{JSRef, Temporary}; use dom::document::Document; use dom::eventtarget::{EventTarget, EventTargetTypeId}; use dom::element::ElementTypeId; -use dom::element::AttributeHandlers; use dom::htmlelement::{HTMLElement, HTMLElementTypeId}; -use dom::node::{Node, NodeHelpers, NodeTypeId}; +use dom::node::{Node, NodeTypeId}; +use dom::userscripts::load_script; use dom::virtualmethods::VirtualMethods; use util::str::DOMString; -use util::opts; -use util::resource_files::resources_dir_path; -use std::borrow::ToOwned; -use std::fs::read_dir; -use std::path::PathBuf; #[dom_struct] pub struct HTMLHeadElement { @@ -52,38 +45,6 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLHeadElement> { Some(htmlelement as &VirtualMethods) } fn bind_to_tree(&self, _tree_in_doc: bool) { - if let Some(ref path_str) = opts::get().userscripts { - let node: &JSRef = NodeCast::from_borrowed_ref(self); - let first_child = node.GetFirstChild().root(); - let doc = node.owner_doc().root(); - let doc = doc.r(); - - let path = if &**path_str == "" { - let mut p = resources_dir_path(); - p.push("user-agent-js"); - p - } else { - PathBuf::new(path_str) - }; - - let mut files = match read_dir(&path) { - Ok(d) => d.filter_map(|e| e.ok()).map(|e| e.path()).collect::>(), - Err(_) => return - }; - - files.sort(); - - for file in files { - let name = match file.into_os_string().into_string() { - Ok(ref s) if s.ends_with(".js") => "file://".to_owned() + &s[..], - _ => continue - }; - let new_script = doc.CreateElement("script".to_owned()).unwrap().root(); - let new_script = new_script.r(); - new_script.set_string_attribute(&atom!("src"), name); - let new_script_node: &JSRef = NodeCast::from_borrowed_ref(&new_script); - node.InsertBefore(*new_script_node, first_child.r()).unwrap(); - } - } + load_script(*self); } } diff --git a/components/script/dom/mod.rs b/components/script/dom/mod.rs index 63170f8b70b..1e70c1f73b3 100644 --- a/components/script/dom/mod.rs +++ b/components/script/dom/mod.rs @@ -316,6 +316,7 @@ pub mod treewalker; pub mod uievent; pub mod urlhelper; pub mod urlsearchparams; +pub mod userscripts; pub mod validitystate; pub mod virtualmethods; pub mod websocket; diff --git a/components/script/dom/userscripts.rs b/components/script/dom/userscripts.rs new file mode 100644 index 00000000000..08ed1836aaf --- /dev/null +++ b/components/script/dom/userscripts.rs @@ -0,0 +1,52 @@ +/* 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 http://mozilla.org/MPL/2.0/. */ + +use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; +use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods; +use dom::bindings::codegen::InheritTypes::NodeCast; +use dom::bindings::js::{JSRef, OptionalRootable, RootedReference}; +use dom::element::AttributeHandlers; +use dom::htmlheadelement::HTMLHeadElement; +use dom::node::{Node, NodeHelpers}; +use util::opts; +use util::resource_files::resources_dir_path; +use std::borrow::ToOwned; +use std::fs::read_dir; +use std::path::PathBuf; + + +pub fn load_script(head: JSRef) { + if let Some(ref path_str) = opts::get().userscripts { + let node: &JSRef = NodeCast::from_borrowed_ref(&head); + let first_child = node.GetFirstChild().root(); + let doc = node.owner_doc().root(); + let doc = doc.r(); + + let path = if &**path_str == "" { + let mut p = resources_dir_path(); + p.push("user-agent-js"); + p + } else { + PathBuf::new(path_str) + }; + + let mut files = read_dir(&path).ok().expect("Bad path passed to --userscripts") + .filter_map(|e| e.ok()) + .map(|e| e.path()).collect::>(); + + files.sort(); + + for file in files { + let name = match file.into_os_string().into_string() { + Ok(ref s) if s.ends_with(".js") => "file://".to_owned() + &s[..], + _ => continue + }; + let new_script = doc.CreateElement("script".to_owned()).unwrap().root(); + let new_script = new_script.r(); + new_script.set_string_attribute(&atom!("src"), name); + let new_script_node: &JSRef = NodeCast::from_borrowed_ref(&new_script); + node.InsertBefore(*new_script_node, first_child.r()).unwrap(); + } + } +}