diff options
Diffstat (limited to 'components/script/dom/userscripts.rs')
-rw-r--r-- | components/script/dom/userscripts.rs | 70 |
1 files changed, 39 insertions, 31 deletions
diff --git a/components/script/dom/userscripts.rs b/components/script/dom/userscripts.rs index fd5ae5f023e..047b3345b0c 100644 --- a/components/script/dom/userscripts.rs +++ b/components/script/dom/userscripts.rs @@ -1,41 +1,39 @@ /* 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/. */ + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use dom::bindings::inheritance::Castable; -use dom::globalscope::GlobalScope; -use dom::htmlheadelement::HTMLHeadElement; -use dom::node::Node; +use crate::dom::bindings::inheritance::Castable; +use crate::dom::bindings::refcounted::Trusted; +use crate::dom::bindings::str::DOMString; +use crate::dom::globalscope::GlobalScope; +use crate::dom::htmlheadelement::HTMLHeadElement; +use crate::dom::htmlscriptelement::SourceCode; +use crate::dom::node::document_from_node; +use crate::script_module::ScriptFetchOptions; use js::jsval::UndefinedValue; -use servo_config::opts; -use servo_config::resource_files::resources_dir_path; -use std::fs::{File, read_dir}; +use std::fs::{read_dir, File}; use std::io::Read; use std::path::PathBuf; - +use std::rc::Rc; pub fn load_script(head: &HTMLHeadElement) { - if let Some(ref path_str) = opts::get().userscripts { - let node = head.upcast::<Node>(); - let doc = node.owner_doc(); - let win = doc.window(); + 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 = if &**path_str == "" { - if let Ok(mut p) = resources_dir_path() { - p.push("user-agent-js"); - p - } else { - return - } - } else { - PathBuf::from(path_str) - }; + rooted!(in(*cx) let mut rval = UndefinedValue()); - let mut files = read_dir(&path).expect("Bad path passed to --userscripts") - .filter_map(|e| e.ok()) - .map(|e| e.path()).collect::<Vec<_>>(); + 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(); @@ -43,8 +41,18 @@ pub fn load_script(head: &HTMLHeadElement) { 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_js_on_global_with_result(&script_text, rval.handle_mut()); + let script_text = SourceCode::Text( + Rc::new(DOMString::from_string(String::from_utf8_lossy(&contents).to_string())) + ); + let global = win.upcast::<GlobalScope>(); + global.evaluate_script_on_global_with_result( + &script_text, + &file.to_string_lossy(), + rval.handle_mut(), + 1, + ScriptFetchOptions::default_classic_script(&global), + global.api_base_url(), + ); } - } + })); } |