我已经根据 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);
}
}
最佳答案
我都试过了pagehide
和 blur
但他们都没有工作。我认为这是因为从 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)处理程序的结果。
我发现如果我没有注册自定义协议(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 dialogsComment 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/