javascript - 在 chrome 86 中检测自定义协议(protocol)处理程序

标签 javascript google-chrome chromium google-chrome-app custom-protocol

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

最佳答案

我都试过了pagehideblur但他们都没有工作。我认为这是因为从 chrome 86+ 开始,外部协议(protocol)处理程序不再模糊当前页面。
Detecting Custom Protocol Handler in Windows 8+ with Chrome提到使用 HTML5 Visibility API我也试过了,但它也无法用于 Chrome 86+。
我觉得我们可能没有针对 Chrome 86+ 的解决方案。 vscode 处理这个问题的方式可以给我们一些提示。当您第一次访问 https://marketplace.visualstudio.com/无论您是否安装了 vscode,它都会弹出此对话框。
如果您没有安装 vscode 并且您在外部协议(protocol)处理程序中单击打开按钮,您将不会收到任何错误。这可能表明他们也无法获得外部协议(protocol)处理程序的结果。
vscode
我发现如果我没有注册自定义协议(protocol),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 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


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

关于javascript - 在 chrome 86 中检测自定义协议(protocol)处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64658489/

相关文章:

javascript - 使用 JavaScript 添加新的 HTML 标签到文本 block

jquery - CSS 翻转卡 - Chrome 中的错误

node.js - 如何在 Docker 中使用 Chromium v​​77 运行 NodeJS puppeteer?

javascript - 如何在传单 MarkerClusterGroup 中打开特定标记的弹出窗口?

javascript - 为什么这个 javascript 计时器代码在 Chrome 调试器中可以工作,但在正常运行时却不能工作

javascript - ajax下载的excel文件已损坏

google-chrome - Webkit 表格单元格的行跨度问题的非脚本解决方法

google-chrome - Google JavaScript API - chrome.printerProvider

google-chrome - 通过远程调试协议(protocol)将 javascript 代码发送到 chrome 应用程序

build-process - gclient 运行 Hook 失败