5

我已经根据https://gist.github.com/aaronk6/d801d750f14ac31845e8实施了一个解决方案, 并且在 chrome 85 之前它工作正常。最新的 chrome Update Onblur 未检测到开放协议处理程序弹出窗口。有没有办法使用 Chrome 86 新版本识别在 Windows 中注册的自定义协议。我已经实现的代码在下面提到,它适用于 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);
    }
}
4

2 回答 2

2

我都试过了pagehideblur但他们都没有工作。我认为这是因为从 chrome 86+ 开始,外部协议处理程序不再模糊当前页面。

Detecting Custom Protocol Handler in Windows 8+ with Chrome提到使用HTML5 Visibility API我也尝试过,但它也无法用于Chrome 86+。

我觉得我们可能没有针对 Chrome 86+ 的解决方案。vscode 处理这个问题的方式可以给我们一些提示。当您第一次访问https://marketplace.visualstudio.com/时,无论您是否安装了 vscode,它都会弹出此对话框。

如果您没有安装 vscode 并且您在外部协议处理程序中单击打开按钮,您将不会收到任何错误。这可能表明他们也无法获得外部协议处理程序的结果。

代码

我发现如果我没有注册自定义协议,chrome 会打印“因为该方案没有注册的处理程序”。在控制台日志中,如此处所示

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

但是我们如何shell_integration::GetApplicationNameForProtocol(url).empty()从 javascript 代码中获取呢?

还要在这里查看https://support.google.com/chrome/thread/78279651?hl=en,也没有答案。

--- Chrome 89+ 更新 ---

我发现这onBlur又有效了!我找不到明确的文档,但来自Issue 1137801: Regression: "onBlur" event is not trigger for built-in dialogs

ericlaw@microsoft.com 于 2021 年 3 月 24 日星期三上午 6:43 GMT+8 发表评论 30

从 v89 开始,这里的原始投诉不再重复,这意味着我们现在引入了一个启用协议检测的隐私漏洞。Bisect 表明协议检测黑客再次开始工作

我测试了 Chrome 90+,它确实有效!

于 2020-12-24T04:12:10.400 回答
0

澄清一下,出于隐私和安全原因,故意无法从 JavaScript 检测本地协议处理程序;看到这个帖子

这停止工作的具体原因是https://crbug.com/1137801

于 2021-03-23T22:26:30.060 回答