I have code that is written by others in my app and I want links of anchor tags in this code to be opened in the system default browser, not in the tauri window. One approach I tried is injecting javascript that listens for click events and then checks whether it's an anchor tag and the href starts with http and if so, invoke a command to the rust backend to open the link in the system browser (after sanitizing it). However this approach does not seem to work with anchor tags inside shadow DOM's.
#Is there a way to force open anchor tags in the default browser instead of the tauri window
3 messages · Page 1 of 1 (latest)
In case someone else has the same issue, I converged to this solution that so far seems to do what I want. I added the following code as an initialization_script when creating the window:
window.addEventListener("click", (e) => {
const maybeHref = e.composedPath()[0].href;
if (maybeHref) {
// alert(`Got composed path with href: ${maybeHref}`);
if ( (maybeHref.startsWith('http://') || maybeHref.startsWith('https://')) && !(maybeHref.includes("tauri.localhost")) ) {
e.preventDefault();
window.__TAURI_INVOKE__('open_url_cmd', { 'url': maybeHref } )
}
}
});
if I understand correctly you are opening a default browser to show something? have you been able to return something from the browser to the tauri app? like a auth access token I am trying to get by opening the default browser?