I am implementing a deep link to an electron app from an invite-link html page. Currently, I run this code at the time of loading the page to check if the application is installed on the client and will present a download link or carry on to the deeplink respectively. I am running Safari on macOS Ventura.
function loadDesktopApp(nativeAppUri, onFailure, onMaybeLoaded) {
// Attempt to guess whether custom protocol scheme is registered based on the following assumptions:
// 1. if no handler for current scheme, browser does nothing and focus remains on page
// 2. if scheme registered, browser would attempt to load external app
// * dialog asking for permission will be shown (window looses focus)
// * or permission previously granted and remembered, app is loaded (window looses visibility)
//
// Note: We cannot really tell the difference if:
// - app loaded successfully
// - user refused permission
// - scheme registered but app failed to load
//
const dialogAppearTimeout = 1000; // how long do we give for dialog to show up before we consider load fail.
let hasLostFocus = false;
let hasLostVisibility = false;
// window blurred when dialog shown
function onBlur() {
hasLostFocus = true;
}
// document hidden when native app shown (or manually backgrounded :( )
function onVisibilityChange(e) {
if (e.target.visibilityState === 'hidden') {
hasLostVisibility = true;
}
}
window.addEventListener('blur', onBlur);
document.addEventListener('visibilitychange', onVisibilityChange);
setTimeout(function () {
window.removeEventListener('blur', onBlur);
document.removeEventListener('visibilitychange', onVisibilityChange);
if (!hasLostFocus && !hasLostVisibility) {
if (onFailure) onFailure();
} else {
if (onMaybeLoaded) onMaybeLoaded();
}
}, dialogAppearTimeout);
window.location = nativeAppUri;
}
^ code from https://github.com/shawnchin/jitsi-electron-loader
The problem is when I am using the Safari browser, I am returned with the "Safari cannot open the page because the address is invalid" every time it loads. Works fine on Chrome and Firefox.
Since the error message shows every time on Safari I assume there is some compatibility issue with where onBlur or onVisibilityChange doesn't approach. Not sure if this is the best way to do this anymore. Tried a onFallback and onReturn approach similar to https://gist.github.com/diachedelic/0d60233dab3dcae3215da8a4dfdcd434 but was having the same issue.