diff --git a/.changes/history.md b/.changes/history.md new file mode 100644 index 000000000..fcc0b5154 --- /dev/null +++ b/.changes/history.md @@ -0,0 +1,5 @@ +--- +"wry": minor +--- + +Fix `history.pushState` in webview2. diff --git a/examples/README.md b/examples/README.md index 0bf285a9d..647f67dd1 100644 --- a/examples/README.md +++ b/examples/README.md @@ -10,4 +10,4 @@ Run the `cargo run --example ` to see how each example works. - `dragndrop`: example for file drop handler. - `custom_titlebar`: A frameless window with custom title-bar to show `drag-region` class in action. - `custom_user_data`: uses a custom data directory (Windows only). - +- `custom_protocol`: uses a custom protocol to load files from bytes. diff --git a/examples/custom_protocol.rs b/examples/custom_protocol.rs new file mode 100644 index 000000000..5d3d8b45e --- /dev/null +++ b/examples/custom_protocol.rs @@ -0,0 +1,91 @@ +// Copyright 2019-2021 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +fn main() -> wry::Result<()> { + use wry::{ + application::{ + event::{Event, StartCause, WindowEvent}, + event_loop::{ControlFlow, EventLoop}, + window::WindowBuilder, + }, + webview::WebViewBuilder, + }; + + let event_loop = EventLoop::new(); + let window = WindowBuilder::new() + .with_title("Hello World") + .build(&event_loop) + .unwrap(); + + let _webview = WebViewBuilder::new(window) + .unwrap() + .with_custom_protocol("wry.dev".into(), move |requested_asset_path| { + // remove the protocol from the path for easiest match + let requested_asset_path = requested_asset_path.replace("wry.dev://", ""); + + // sample index.html file + // files can be bundled easilly into the binary + // with https://doc.rust-lang.org/std/macro.include_bytes.html + + let index_html = r#" + + + + + + + + +

Welcome to WRY!

+ Link + + + "#; + + // sample hello.js file + let hello_js = "console.log(\"hello from javascript\");"; + + // sample hello.html file + let hello_html = r#" + + + + + + + + +

Sample page!

+ Back home + + + "#; + + match requested_asset_path.as_str() { + // if our path match /hello.html + "/hello.html" => Ok(hello_html.as_bytes().into()), + // if our path match /hello.js + "/hello.js" => Ok(hello_js.as_bytes().into()), + // other paths should resolve index + // more logic can be applied here + _ => Ok(index_html.as_bytes().into()), + } + }) + // tell the webview to load the custom protocol + .with_url("wry.dev://")? + .build()?; + + event_loop.run(move |event, _, control_flow| { + *control_flow = ControlFlow::Poll; + + match event { + Event::NewEvents(StartCause::Init) => println!("Wry application started!"), + Event::WindowEvent { + event: WindowEvent::CloseRequested, + .. + } => *control_flow = ControlFlow::Exit, + _ => (), + } + }); +} diff --git a/src/webview/win32/mod.rs b/src/webview/win32/mod.rs index 00999c246..f809558d6 100644 --- a/src/webview/win32/mod.rs +++ b/src/webview/win32/mod.rs @@ -117,7 +117,7 @@ impl InnerWebView { // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 custom_protocol_names.insert(name.clone()); w.add_web_resource_requested_filter( - &format!("file://custom-protocol-{}*", name), + &format!("http://custom-protocol-{}*", name), webview2::WebResourceContext::All, )?; let env_clone = env_.clone(); @@ -125,7 +125,7 @@ impl InnerWebView { let uri = args.get_request()?.get_uri()?; // Undo the protocol workaround when giving path to resolver let path = &uri.replace( - &format!("file://custom-protocol-{}", name), + &format!("http://custom-protocol-{}", name), &format!("{}://", name), ); @@ -175,7 +175,7 @@ impl InnerWebView { // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 url_string = url.as_str().replace( &format!("{}://", name), - &format!("file://custom-protocol-{}", name), + &format!("http://custom-protocol-{}", name), ) } w.navigate(&url_string)?; diff --git a/src/webview/winrt/mod.rs b/src/webview/winrt/mod.rs index 9aa3c2e41..ee6127caa 100644 --- a/src/webview/winrt/mod.rs +++ b/src/webview/winrt/mod.rs @@ -141,7 +141,7 @@ impl InnerWebView { // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 custom_protocol_names.insert(name.clone()); w.AddWebResourceRequestedFilter( - format!("file://custom-protocol-{}*", name).as_str(), + format!("http://custom-protocol-{}*", name).as_str(), webview2::CoreWebView2WebResourceContext::All, )?; let env_ = env.clone(); @@ -154,7 +154,7 @@ impl InnerWebView { if let Ok(uri) = String::from_utf16(args.Request()?.Uri()?.as_wide()) { // Undo the protocol workaround when giving path to resolver let path = uri.replace( - &format!("file://custom-protocol-{}", name), + &format!("http://custom-protocol-{}", name), &format!("{}://", name), ); @@ -208,7 +208,7 @@ impl InnerWebView { // See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73 url_string = url.as_str().replace( &format!("{}://", name), - &format!("file://custom-protocol-{}", name), + &format!("http://custom-protocol-{}", name), ) } w.Navigate(url_string.as_str())?;