Detect Custom Protocol handler in chrome 86

守給你的承諾、 提交于 2021-01-27 16:39:43

问题


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);
    }
}

回答1:


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.

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.



来源:https://stackoverflow.com/questions/64658489/detect-custom-protocol-handler-in-chrome-86

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!