cha*_*era 5 javascript google-chrome custom-protocol chromium google-chrome-app
我已经根据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);
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了两者pagehide,blur但都失败了。我认为这是因为从 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 会打印“因为该方案没有注册处理程序”。在控制台日志中,如下所示
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;
}
Run Code Online (Sandbox Code Playgroud)
但是我们如何shell_integration::GetApplicationNameForProtocol(url).empty()从 javascript 代码中获取呢?
另请检查此处https://support.google.com/chrome/thread/78279651?hl=en,也没有答案。
--- Chrome 89+ 更新 ---
我发现onBlur又可以了!我找不到明确的文档,但来自问题 1137801:回归:内置对话框未触发“onBlur”事件
ericlaw@microsoft.com 于 2021 年 3 月 24 日星期三上午 6:43 GMT+8 发表的评论 30
从 v89 开始,此处的原始投诉不再重现,这意味着我们现在引入了一个启用协议检测的隐私错误。Bisect 表明协议检测黑客再次开始工作
我测试了 Chrome 90+,它确实有效!