我有一个自定义协议检查器,用于检查是否已安装协议。
对于Safari(与chrome相同),它着重于元素,触发协议并监听onblur。
但是在Safari中,如果未安装该协议,浏览器将弹出警告esc弹出窗口,提示:“ Safari无法打开页面,因为地址无效。”依次触发onblur事件。
有人找到一种更好的方法来管理它了吗?如果需要,它可以是Safari专用的解决方案。
//Chrome (and default for other browsers)
function checkChrome(){
bodyElement.append("<input type='text' id='focusInput' style='background: transparent;border: none;height: 0px;width: 0px;' />");
var focusBodyElement = $('#focusInput')[0], temporaryResult = false;
focusBodyElement.focus();
focusBodyElement.onblur = function () {
updateResult(true);
return;
};
//will trigger onblur
location.href = protocolStr;
//Note: timeout could vary as per the browser version, have a higher value
setTimeout(function () {
focusBodyElement.onblur = null;
if (protocolSupport[protocolStr]===null) {
updateResult(false)
}
}, 1000);
}
虽然我的目标是所有浏览器,但我过去在这里都使用过custom-protocol-detection。
话虽这么说-在挖掘其来源时,似乎他们的策略是将内容与JavaScript创建的内容一起嵌入隐藏的框架中。
function openUriWithHiddenFrame(uri, failCb, successCb) {
var timeout = setTimeout(function () {
failCb();
handler.remove();
}, 1000);
var iframe = document.querySelector("#hiddenIframe");
if (!iframe) {
iframe = _createHiddenIframe(document.body, "about:blank");
}
var handler = _registerEvent(window, "blur", onBlur);
function onBlur() {
clearTimeout(timeout);
handler.remove();
successCb();
}
iframe.contentWindow.location.href = uri;
}
源代码还包含所有浏览器的策略。