mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
Auto merge of #15874 - sendilkumarn:user-script, r=nox
apply user scripts correctly <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #15082 <!-- 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/15874) <!-- Reviewable:end -->
This commit is contained in:
commit
d70c1e53ae
1 changed files with 12 additions and 14 deletions
|
@ -2,25 +2,25 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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 http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
|
||||||
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
|
||||||
use dom::bindings::inheritance::Castable;
|
use dom::bindings::inheritance::Castable;
|
||||||
use dom::bindings::js::RootedReference;
|
use dom::globalscope::GlobalScope;
|
||||||
use dom::bindings::str::DOMString;
|
|
||||||
use dom::htmlheadelement::HTMLHeadElement;
|
use dom::htmlheadelement::HTMLHeadElement;
|
||||||
use dom::node::Node;
|
use dom::node::Node;
|
||||||
|
use js::jsval::UndefinedValue;
|
||||||
use servo_config::opts;
|
use servo_config::opts;
|
||||||
use servo_config::resource_files::resources_dir_path;
|
use servo_config::resource_files::resources_dir_path;
|
||||||
use std::borrow::ToOwned;
|
use std::fs::{File, read_dir};
|
||||||
use std::fs::read_dir;
|
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 {
|
if let Some(ref path_str) = opts::get().userscripts {
|
||||||
let node = head.upcast::<Node>();
|
let node = head.upcast::<Node>();
|
||||||
let first_child = node.GetFirstChild();
|
|
||||||
let doc = node.owner_doc();
|
let doc = node.owner_doc();
|
||||||
|
let win = doc.window();
|
||||||
|
let cx = win.get_cx();
|
||||||
|
rooted!(in(cx) let mut rval = UndefinedValue());
|
||||||
|
|
||||||
let path = if &**path_str == "" {
|
let path = if &**path_str == "" {
|
||||||
if let Ok(mut p) = resources_dir_path() {
|
if let Ok(mut p) = resources_dir_path() {
|
||||||
|
@ -40,13 +40,11 @@ pub fn load_script(head: &HTMLHeadElement) {
|
||||||
files.sort();
|
files.sort();
|
||||||
|
|
||||||
for file in files {
|
for file in files {
|
||||||
let name = match file.into_os_string().into_string() {
|
let mut f = File::open(&file).unwrap();
|
||||||
Ok(ref s) if s.ends_with(".js") => "file://".to_owned() + &s[..],
|
let mut contents = vec![];
|
||||||
_ => continue
|
f.read_to_end(&mut contents).unwrap();
|
||||||
};
|
let script_text = String::from_utf8_lossy(&contents);
|
||||||
let new_script = doc.CreateElement(DOMString::from("script")).unwrap();
|
win.upcast::<GlobalScope>().evaluate_js_on_global_with_result(&script_text, rval.handle_mut());
|
||||||
new_script.set_string_attribute(&local_name!("src"), DOMString::from(name));
|
|
||||||
node.InsertBefore(new_script.upcast(), first_child.r()).unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue