fune/servo/components/script/dom/userscripts.rs
SendilKumar N 5b26f691fa servo: Merge #15874 - apply user scripts correctly (from sendilkumarn:user-script); r=nox
<!-- 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

Source-Repo: https://github.com/servo/servo
Source-Revision: d70c1e53ae06d2dc2216c0d59709b12396378be7

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : 1eaf8a369fd635f6abf9838742fedc94c9c44380
2017-03-13 07:01:52 -07:00

50 lines
1.7 KiB
Rust

/* 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::inheritance::Castable;
use dom::globalscope::GlobalScope;
use dom::htmlheadelement::HTMLHeadElement;
use dom::node::Node;
use js::jsval::UndefinedValue;
use servo_config::opts;
use servo_config::resource_files::resources_dir_path;
use std::fs::{File, read_dir};
use std::io::Read;
use std::path::PathBuf;
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 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)
};
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();
for file in files {
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());
}
}
}