6

I have implemented a solution according to https://gist.github.com/aaronk6/d801d750f14ac31845e8 and it worked fine till chrome 85 .With latest chrome Update Onblur not detecting open protocol handler popup. Is there a way to identify Custom protocol registered in windows using Chrome 86 new version .The code i have implemented mentioned below and it's working fine for Firefox

function LinkClicked() {
        launchUri($(this).attr("href"), function () {
            // SUCCESS APPLICATION INSTALLED
        }, function () {
            // PROTOCOL NOT REGISTERD IN REGISTRY
            setTimeout(showAppInstallWarningMessage, 4000);
        }, function () {
            // STATUS CANNOT IDENTIFY
            setTimeout(showAppInstallWarningMessage, 4000);
        });
    }





function launchUri(uri, successCallback, noHandlerCallback, unknownCallback) {
    var res, parent, popup, iframe, timer, timeout, blurHandler, timeoutHandler, browser;

    function callback(cb) {
        if (typeof cb === 'function') cb();
    }

    function createHiddenIframe(parent) {
        var iframe;
        if (!parent) parent = document.body;
        iframe = document.createElement('iframe');
        iframe.style.display = 'none';
        parent.appendChild(iframe);
        return iframe;
    }

    function removeHiddenIframe(parent) {
        if (!iframe) return;
        if (!parent) parent = document.body;
        parent.removeChild(iframe);
        iframe = null;
    }

    browser = { isChrome: false, isFirefox: false, isIE: false };

    if (window.chrome && !navigator.userAgent.match(/Opera|OPR\//)) {
        browser.isChrome = true;
    } else if (typeof InstallTrigger !== 'undefined') {
        browser.isFirefox = true;
    } else if ('ActiveXObject' in window) {
        browser.isIE = true;
    }

    // EVALUATE msLaunchUri for IE 10+ browser in  Windows 8+
    if (navigator.msLaunchUri) {
        navigator.msLaunchUri(uri, successCallback, noHandlerCallback);
    }
    // Evaluating Blur-hack Chrome and FireFox
    else if (browser.isChrome || browser.isFirefox) {
        blurHandler = function () {
            window.clearTimeout(timeout);
            window.removeEventListener('blur', blurHandler);
            callback(successCallback);
        };
        timeoutHandler = function () {
            window.removeEventListener('blur', blurHandler);
            callback(noHandlerCallback);
        };
        window.addEventListener('blur', blurHandler);
        timeout = window.setTimeout(timeoutHandler, 500);
        window.location.href = uri;
    }
    else if (browser.isIE) {
        popup = window.open('', 'launcher', 'width=0,height=0');
        popup.location.href = uri;
        try {
            popup.location.href = 'about:blank';
            callback(successCallback);
            timer = window.setInterval(function () {
                popup.close();
                if (popup.closed) window.clearInterval(timer);
            }, 500);
        } catch (e) {
            popup = window.open('about:blank', 'launcher');
            popup.close();
            callback(noHandlerCallback);
        }
    }
    else {
        iframe = createHiddenIframe();
        iframe.contentWindow.location.href = uri;
        window.setTimeout(function () {
            removeHiddenIframe(parent);
            callback(unknownCallback);
        }, 500);
    }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375

2 Answers2

3

I tried both pagehide and blur but they both failed to work. I think it is because from chrome 86+ the external protocol handler does not blur the current page anymore.

Detecting Custom Protocol Handler in Windows 8+ with Chrome mentioned using HTML5 Visibility API I tried it too, but it also failed to work for Chrome 86+.

I feel we probably don't have a solution for Chrome 86+. The way vscode handles this can give us some hint. When you first visit https://marketplace.visualstudio.com/ no matter you have installed vscode or not it will pop this dialog.

And if you don't have vscode installed and you click open button in the external protocol handler, you won't get any error. This is a probably a sign that they can't get result of the external protocol handler either.

vscode

I find if I don't have custom protocol registered, chrome will print "because the scheme does not have a registered handler." in console log as here shows

https://chromium.googlesource.com/chromium/src/+/master/chrome/browser/external_protocol/external_protocol_handler.cc#119

  if (shell_integration::GetApplicationNameForProtocol(url).empty()) {
    web_contents->GetMainFrame()->AddMessageToConsole(
        blink::mojom::ConsoleMessageLevel::kError,
        "Failed to launch '" + url.possibly_invalid_spec() +
            "' because the scheme does not have a registered handler.");
    return;
  }

But how can we get shell_integration::GetApplicationNameForProtocol(url).empty() from javascript code?

Also check here https://support.google.com/chrome/thread/78279651?hl=en, no answer either.

--- update for Chrome 89+ ---

I found that onBlur works again! I can't find a clear document for that, but from Issue 1137801: Regression: "onBlur" event is not triggered for built-in dialogs

Comment 30 by ericlaw@microsoft.com on Wed, Mar 24, 2021, 6:43 AM GMT+8

As of v89, the original complaint here no longer repros, which means we've now introduced a privacy bug that enables protocol detection. Bisect shows that the protocol detection hack started working again

I test Chrome 90+, it does work!

Qiulang
  • 10,295
  • 11
  • 80
  • 129
1

To clarify, detection of local protocol handlers is intentionally not possible from JavaScript for Privacy and Security reasons; see this post.

The specific reason this stopped working is https://crbug.com/1137801.

EricLaw
  • 56,563
  • 7
  • 151
  • 196