web: Changes from review

This commit is contained in:
Toad06 2021-01-10 15:04:31 +01:00 committed by Mike Welsh
parent 0131665149
commit 7480e836ec
3 changed files with 48 additions and 29 deletions

View File

@ -172,6 +172,9 @@ pub trait NavigatorBackend {
vars_method: Option<(NavigationMethod, IndexMap<String, String>)>, vars_method: Option<(NavigationMethod, IndexMap<String, String>)>,
); );
/// Execute a JavaScript code.
fn run_script(&self, js_code: &str);
/// Fetch data at a given URL and return it some time in the future. /// Fetch data at a given URL and return it some time in the future.
fn fetch(&self, url: &str, request_options: RequestOptions) -> OwnedFuture<Vec<u8>, Error>; fn fetch(&self, url: &str, request_options: RequestOptions) -> OwnedFuture<Vec<u8>, Error>;
@ -359,6 +362,8 @@ impl NavigatorBackend for NullNavigatorBackend {
) { ) {
} }
fn run_script(&self, _js_code: &str) {}
fn fetch(&self, url: &str, _opts: RequestOptions) -> OwnedFuture<Vec<u8>, Error> { fn fetch(&self, url: &str, _opts: RequestOptions) -> OwnedFuture<Vec<u8>, Error> {
let mut path = self.relative_base_path.clone(); let mut path = self.relative_base_path.clone();
path.push(url); path.push(url);

View File

@ -111,6 +111,8 @@ impl NavigatorBackend for ExternalNavigatorBackend {
}; };
} }
fn run_script(&self, _js_code: &str) {}
fn fetch(&self, url: &str, options: RequestOptions) -> OwnedFuture<Vec<u8>, Error> { fn fetch(&self, url: &str, options: RequestOptions) -> OwnedFuture<Vec<u8>, Error> {
// TODO: honor sandbox type (local-with-filesystem, local-with-network, remote, ...) // TODO: honor sandbox type (local-with-filesystem, local-with-network, remote, ...)
let full_url = match self.movie_url.clone().join(url) { let full_url = match self.movie_url.clone().join(url) {

View File

@ -43,26 +43,11 @@ impl NavigatorBackend for WebNavigatorBackend {
vars_method: Option<(NavigationMethod, IndexMap<String, String>)>, vars_method: Option<(NavigationMethod, IndexMap<String, String>)>,
) { ) {
if let Some(window) = window() { if let Some(window) = window() {
let document = match window.document() { if let Some(url) = url.trim().strip_prefix("javascript:") {
Some(document) => document,
None => return,
};
let body = match document.body() {
Some(body) => body,
None => return,
};
if url.trim().starts_with("javascript:") {
let target = window_spec.unwrap_or_else(|| "".to_string()); let target = window_spec.unwrap_or_else(|| "".to_string());
if target.is_empty() || target == "_self" || target == "undefined" { if target.is_empty() || target == "_self" || target == "undefined" {
let code = url.replacen("javascript:", "", 1); self.run_script(url);
let script = document.create_element("script").unwrap();
script.set_inner_html(&code);
let _append = body.append_child(&script);
let _remove = body.remove_child(&script);
} }
} else { } else {
let window_url = if url.is_empty() { let window_url = if url.is_empty() {
@ -80,16 +65,24 @@ impl NavigatorBackend for WebNavigatorBackend {
}; };
//TODO: Should we return a result for failed opens? Does Flash care? //TODO: Should we return a result for failed opens? Does Flash care?
#[allow(unused_must_use)]
match (vars_method, window_spec) { match (vars_method, window_spec) {
(Some((navmethod, formvars)), window_spec) => { (Some((navmethod, formvars)), window_spec) => {
let document = match window.document() {
Some(document) => document,
None => return,
};
let body = match document.body() {
Some(body) => body,
None => return,
};
let form = document let form = document
.create_element("form") .create_element("form")
.unwrap() .unwrap()
.dyn_into::<web_sys::HtmlFormElement>() .dyn_into::<web_sys::HtmlFormElement>()
.unwrap(); .unwrap();
form.set_attribute( let _ = form.set_attribute(
"method", "method",
match navmethod { match navmethod {
NavigationMethod::GET => "get", NavigationMethod::GET => "get",
@ -97,33 +90,33 @@ impl NavigatorBackend for WebNavigatorBackend {
}, },
); );
form.set_attribute("action", &form_url); let _ = form.set_attribute("action", &form_url);
if let Some(target) = window_spec { if let Some(target) = window_spec {
form.set_attribute("target", &target); let _ = form.set_attribute("target", &target);
} }
for (k, v) in formvars.iter() { for (k, v) in formvars.iter() {
let hidden = document.create_element("input").unwrap(); let hidden = document.create_element("input").unwrap();
hidden.set_attribute("type", "hidden"); let _ = hidden.set_attribute("type", "hidden");
hidden.set_attribute("name", k); let _ = hidden.set_attribute("name", k);
hidden.set_attribute("value", v); let _ = hidden.set_attribute("value", v);
form.append_child(&hidden); let _ = form.append_child(&hidden);
} }
body.append_child(&form); let _ = body.append_child(&form);
form.submit(); let _ = form.submit();
} }
(_, Some(ref window_name)) if !window_name.is_empty() => { (_, Some(ref window_name)) if !window_name.is_empty() => {
if !window_url.is_empty() { if !window_url.is_empty() {
window.open_with_url_and_target(&window_url, window_name); let _ = window.open_with_url_and_target(&window_url, window_name);
} }
} }
_ => { _ => {
if !window_url.is_empty() { if !window_url.is_empty() {
window.location().assign(&window_url); let _ = window.location().assign(&window_url);
} }
} }
}; };
@ -131,6 +124,25 @@ impl NavigatorBackend for WebNavigatorBackend {
} }
} }
fn run_script(&self, js_code: &str) {
if let Some(window) = window() {
let document = match window.document() {
Some(document) => document,
None => return,
};
let body = match document.body() {
Some(body) => body,
None => return,
};
let script = document.create_element("script").unwrap();
script.set_inner_html(&js_code);
let _ = body.append_child(&script);
let _ = body.remove_child(&script);
}
}
fn time_since_launch(&mut self) -> Duration { fn time_since_launch(&mut self) -> Duration {
let dt = self.performance.now() - self.start_time; let dt = self.performance.now() - self.start_time;
Duration::from_millis(dt as u64) Duration::from_millis(dt as u64)