简体   繁体   中英

Registered or not HTML custom URL scheme detection

In my site I have a custom URL which triggers an installed app, for example

<a href="customapp:12345">Click</a>

If my Windows app has registered the URL scheme, Chrome prompts the user to open it.

The question is, if the app is not registered, is there a way for me to find it and prompt the user to install it?

Thanks a lot.

I found that window.onblur seems to be called if you attempt to open a scheme link and the handler is installed. This is because it either opens the registered app or opens a box asking if you want to open the app, both of which take the focus. If it's not installed the focus stays where it was.

This worked for me on Windows 10/Chrome, macOS/Chrome, android/Chrome, android/Silk & Linux/Firefox.

call tryScheme("yourScheme://blahblah", function() { <it's not installed code> })

var blurred = false;

window.onblur = function() {
    blurred = true;
}

function tryScheme(url, fallbackFunction) {    
    blurred = false;       

    document.location = url;

    // If we haven't lost focus in a fairly short time, call it as not installed.
    setTimeout(function(){
        if (blurred === true) {
            console.log("Focus away from page - the scheme's probably installed");               
            return;
        }
        
        console.log("Still on page - the scheme's probably not installed");                           
        fallbackFunction();
    }, 1000);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM