|
| 1 | +function _registerEvent(target, eventType, cb) { |
| 2 | + if (target.addEventListener) { |
| 3 | + target.addEventListener(eventType, cb); |
| 4 | + return { |
| 5 | + remove: function () { |
| 6 | + target.removeEventListener(eventType, cb); |
| 7 | + } |
| 8 | + }; |
| 9 | + } else { |
| 10 | + target.attachEvent(eventType, cb); |
| 11 | + return { |
| 12 | + remove: function () { |
| 13 | + target.detachEvent(eventType, cb); |
| 14 | + } |
| 15 | + }; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function _createHiddenIframe(target, uri) { |
| 20 | + var iframe = document.createElement("iframe"); |
| 21 | + iframe.src = uri; |
| 22 | + iframe.id = "hiddenIframe"; |
| 23 | + iframe.style.display = "none"; |
| 24 | + target.appendChild(iframe); |
| 25 | + |
| 26 | + return iframe; |
| 27 | +} |
| 28 | + |
| 29 | +function openUriWithHiddenFrame(uri, failCb, successCb) { |
| 30 | + |
| 31 | + var timeout = setTimeout(function () { |
| 32 | + failCb(); |
| 33 | + handler.remove(); |
| 34 | + }, 1000); |
| 35 | + |
| 36 | + var iframe = document.querySelector("#hiddenIframe"); |
| 37 | + if (!iframe) { |
| 38 | + iframe = _createHiddenIframe(document.body, "about:blank"); |
| 39 | + } |
| 40 | + |
| 41 | + var handler = _registerEvent(window, "blur", onBlur); |
| 42 | + |
| 43 | + function onBlur() { |
| 44 | + clearTimeout(timeout); |
| 45 | + handler.remove(); |
| 46 | + successCb(); |
| 47 | + } |
| 48 | + |
| 49 | + iframe.contentWindow.location.href = uri; |
| 50 | +} |
| 51 | + |
| 52 | +function openUriWithTimeoutHack(uri, failCb, successCb) { |
| 53 | + |
| 54 | + var timeout = setTimeout(function () { |
| 55 | + failCb(); |
| 56 | + handler.remove(); |
| 57 | + }, 1000); |
| 58 | + |
| 59 | + //handle page running in an iframe (blur must be registered with top level window) |
| 60 | + var target = window; |
| 61 | + while (target != target.parent) { |
| 62 | + target = target.parent; |
| 63 | + } |
| 64 | + |
| 65 | + var handler = _registerEvent(target, "blur", onBlur); |
| 66 | + |
| 67 | + function onBlur() { |
| 68 | + clearTimeout(timeout); |
| 69 | + handler.remove(); |
| 70 | + successCb(); |
| 71 | + } |
| 72 | + |
| 73 | + window.location = uri; |
| 74 | +} |
| 75 | + |
| 76 | +function openUriUsingFirefox(uri, failCb, successCb) { |
| 77 | + var iframe = document.querySelector("#hiddenIframe"); |
| 78 | + |
| 79 | + if (!iframe) { |
| 80 | + iframe = _createHiddenIframe(document.body, "about:blank"); |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + iframe.contentWindow.location.href = uri; |
| 85 | + successCb(); |
| 86 | + } catch (e) { |
| 87 | + if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL") { |
| 88 | + failCb(); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function openUriUsingIEInOlderWindows(uri, failCb, successCb) { |
| 94 | + if (getInternetExplorerVersion() === 10) { |
| 95 | + openUriUsingIE10InWindows7(uri, failCb, successCb); |
| 96 | + } else if (getInternetExplorerVersion() === 9 || getInternetExplorerVersion() === 11) { |
| 97 | + openUriWithHiddenFrame(uri, failCb, successCb); |
| 98 | + } else { |
| 99 | + openUriInNewWindowHack(uri, failCb, successCb); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +function openUriUsingIE10InWindows7(uri, failCb, successCb) { |
| 104 | + var timeout = setTimeout(failCb, 1000); |
| 105 | + window.addEventListener("blur", function () { |
| 106 | + clearTimeout(timeout); |
| 107 | + successCb(); |
| 108 | + }); |
| 109 | + |
| 110 | + var iframe = document.querySelector("#hiddenIframe"); |
| 111 | + if (!iframe) { |
| 112 | + iframe = _createHiddenIframe(document.body, "about:blank"); |
| 113 | + } |
| 114 | + try { |
| 115 | + iframe.contentWindow.location.href = uri; |
| 116 | + } catch (e) { |
| 117 | + failCb(); |
| 118 | + clearTimeout(timeout); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +function openUriInNewWindowHack(uri, failCb, successCb) { |
| 123 | + var myWindow = window.open('', '', 'width=0,height=0'); |
| 124 | + |
| 125 | + myWindow.document.write("<iframe src='" + uri + "'></iframe>"); |
| 126 | + |
| 127 | + setTimeout(function () { |
| 128 | + try { |
| 129 | + myWindow.location.href; |
| 130 | + myWindow.setTimeout("window.close()", 1000); |
| 131 | + successCb(); |
| 132 | + } catch (e) { |
| 133 | + myWindow.close(); |
| 134 | + failCb(); |
| 135 | + } |
| 136 | + }, 1000); |
| 137 | +} |
| 138 | + |
| 139 | +function openUriWithMsLaunchUri(uri, failCb, successCb) { |
| 140 | + navigator.msLaunchUri(uri, |
| 141 | + successCb, |
| 142 | + failCb |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +function checkBrowser() { |
| 147 | + var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0; |
| 148 | + return { |
| 149 | + isOpera : isOpera, |
| 150 | + isFirefox : typeof InstallTrigger !== 'undefined', |
| 151 | + isSafari : Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0, |
| 152 | + isChrome : !!window.chrome && !isOpera, |
| 153 | + isIE : /*@cc_on!@*/false || !!document.documentMode // At least IE6 |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +function getInternetExplorerVersion() { |
| 158 | + var rv = -1; |
| 159 | + if (navigator.appName === "Microsoft Internet Explorer") { |
| 160 | + var ua = navigator.userAgent; |
| 161 | + var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); |
| 162 | + if (re.exec(ua) != null) |
| 163 | + rv = parseFloat(RegExp.$1); |
| 164 | + } |
| 165 | + else if (navigator.appName === "Netscape") { |
| 166 | + var ua = navigator.userAgent; |
| 167 | + var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})"); |
| 168 | + if (re.exec(ua) != null) { |
| 169 | + rv = parseFloat(RegExp.$1); |
| 170 | + } |
| 171 | + } |
| 172 | + return rv; |
| 173 | +} |
| 174 | + |
| 175 | +module.exports = function(uri, failCb, successCb) { |
| 176 | + function failCallback() { |
| 177 | + failCb && failCb(); |
| 178 | + } |
| 179 | + |
| 180 | + function successCallback() { |
| 181 | + successCb && successCb(); |
| 182 | + } |
| 183 | + |
| 184 | + if (navigator.msLaunchUri) { //for IE and Edge in Win 8 and Win 10 |
| 185 | + openUriWithMsLaunchUri(uri, failCb, successCb); |
| 186 | + } else { |
| 187 | + var browser = checkBrowser(); |
| 188 | + |
| 189 | + if (browser.isFirefox) { |
| 190 | + openUriUsingFirefox(uri, failCallback, successCallback); |
| 191 | + } else if (browser.isChrome) { |
| 192 | + openUriWithTimeoutHack(uri, failCallback, successCallback); |
| 193 | + } else if (browser.isIE) { |
| 194 | + openUriUsingIEInOlderWindows(uri, failCallback, successCallback); |
| 195 | + } else { |
| 196 | + //not supported, implement please |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments