mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
Give userscripts its own module; panic on broken paths
This commit is contained in:
parent
4d2ca2d8e2
commit
f41acb589a
3 changed files with 58 additions and 44 deletions
|
@ -3,23 +3,16 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use dom::bindings::codegen::Bindings::HTMLHeadElementBinding;
|
use dom::bindings::codegen::Bindings::HTMLHeadElementBinding;
|
||||||
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
use dom::bindings::codegen::InheritTypes::{HTMLElementCast, HTMLHeadElementDerived};
|
||||||
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
use dom::bindings::js::{JSRef, Temporary};
|
||||||
use dom::bindings::codegen::InheritTypes::{HTMLElementCast, HTMLHeadElementDerived, NodeCast};
|
|
||||||
use dom::bindings::js::{JSRef, OptionalRootable, Temporary, RootedReference};
|
|
||||||
use dom::document::Document;
|
use dom::document::Document;
|
||||||
use dom::eventtarget::{EventTarget, EventTargetTypeId};
|
use dom::eventtarget::{EventTarget, EventTargetTypeId};
|
||||||
use dom::element::ElementTypeId;
|
use dom::element::ElementTypeId;
|
||||||
use dom::element::AttributeHandlers;
|
|
||||||
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
|
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 dom::virtualmethods::VirtualMethods;
|
||||||
use util::str::DOMString;
|
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]
|
#[dom_struct]
|
||||||
pub struct HTMLHeadElement {
|
pub struct HTMLHeadElement {
|
||||||
|
@ -52,38 +45,6 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLHeadElement> {
|
||||||
Some(htmlelement as &VirtualMethods)
|
Some(htmlelement as &VirtualMethods)
|
||||||
}
|
}
|
||||||
fn bind_to_tree(&self, _tree_in_doc: bool) {
|
fn bind_to_tree(&self, _tree_in_doc: bool) {
|
||||||
if let Some(ref path_str) = opts::get().userscripts {
|
load_script(*self);
|
||||||
let node: &JSRef<Node> = 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::<Vec<_>>(),
|
|
||||||
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<Node> = NodeCast::from_borrowed_ref(&new_script);
|
|
||||||
node.InsertBefore(*new_script_node, first_child.r()).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -316,6 +316,7 @@ pub mod treewalker;
|
||||||
pub mod uievent;
|
pub mod uievent;
|
||||||
pub mod urlhelper;
|
pub mod urlhelper;
|
||||||
pub mod urlsearchparams;
|
pub mod urlsearchparams;
|
||||||
|
pub mod userscripts;
|
||||||
pub mod validitystate;
|
pub mod validitystate;
|
||||||
pub mod virtualmethods;
|
pub mod virtualmethods;
|
||||||
pub mod websocket;
|
pub mod websocket;
|
||||||
|
|
52
components/script/dom/userscripts.rs
Normal file
52
components/script/dom/userscripts.rs
Normal file
|
@ -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<HTMLHeadElement>) {
|
||||||
|
if let Some(ref path_str) = opts::get().userscripts {
|
||||||
|
let node: &JSRef<Node> = 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::<Vec<_>>();
|
||||||
|
|
||||||
|
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<Node> = NodeCast::from_borrowed_ref(&new_script);
|
||||||
|
node.InsertBefore(*new_script_node, first_child.r()).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue