#Open App Url from Rust

10 messages · Page 1 of 1 (latest)

vivid gale
#

I want to open a specific route on my frontend from rust, but I can't seem to find how to do that.
As some context: I want be able to double click an associated file format and then pass the path to that file to the (React) frontend the single instance of my app. While the file-association and single-instance process works in general I don't really get how to navigate to a route in my app, since the protocol/host/port etc. may change from debug to production.
I found that I can open an AppUrl when building the webview, but not afterwards :/

whole moat
#

you don't need to care about the protocol/host/port. The relative url is enough, like window.location.replace("index.html/#/SomeRoute") (or ig without the # for BrowserRouter).

If the frontend is for sure running already you can emit a tauri event that tells the frontend to navigate (either using the code above, or something else that your router may provide), or you can execute it from rust directly ```rs
let _ = my_window.eval("window.location.replace('index.html/#/SomeRoute')");

vivid gale
#

Thanks for the quick feedback!

#

This feels like it could be easily abused though right? e.g. by naming a file '); <evilcode> ('

#

I mean I'd urlencode the parameters from the single-instance args so ... I guess that shouldn't be a viable attack vector...

#
let file_path = &args[1];
let path = std::path::Path::new(file_path).canonicalize().unwrap();
let url_encoded_arg = urlencode(path.to_str().unwrap());
let new_url = format!("projects?selectedProjectPath={url_encoded_arg}");
window.eval(&format!("window.location.replace('{new_url}')"));
#

This should be safe, right?

whole moat
#

ehhh, i think so? I'm really not an expert when it comes to javascript security stuff tbh

vivid gale
#

Okay, if anyone has more suggestions I'm open to them - using .eval feels incredibly skechy to me 😅

#

But it works for the time being 🙂