forked from ismailhabib/custom-protocol-detection
-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
executable file
·271 lines (229 loc) · 6.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const browser = {
getUserAgent: function() {
return window.navigator.userAgent;
},
userAgentContains: function(browserName) {
browserName = browserName.toLowerCase();
return (
this.getUserAgent()
.toLowerCase()
.indexOf(browserName) > -1
);
},
isOSX: function() {
return this.userAgentContains("Macintosh");
},
isFirefox: function() {
return this.userAgentContains("firefox");
},
isInternetExplorer: function() {
return this.userAgentContains("trident");
},
/**
* Detects IE 11 and older
* @return {Boolean} Returns true when IE 11 and older
*/
isIE: function() {
var ua = this.getUserAgent().toLowerCase();
// Test values.
// Uncomment to check result
// IE 10
// ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';
// IE 11
// ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko/20100101 Firefox/12.0';
var msie = ua.indexOf("msie");
if (msie > 0) {
// IE 10 or older
return true;
}
var trident = ua.indexOf("trident/");
if (trident > 0) {
// IE 11
return true;
}
// other browser
return false;
},
isEdge: function() {
var ua = this.getUserAgent().toLowerCase();
// Test values.
// Uncomment to check result
// Edge
// ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240';
var edge = ua.indexOf("edge");
if (edge > 0) {
return true;
}
return false;
},
isChrome: function() {
// IE11 returns undefined for window.chrome
// and new Opera 30 outputs true for window.chrome
// but needs to check if window.opr is not undefined
// and new IE Edge outputs to true for window.chrome
// and if not iOS Chrome check
const isChromium = window.chrome;
const winNav = window.navigator;
const vendorName = winNav.vendor;
const isOpera = typeof window.opr !== "undefined";
const isIEedge = winNav.userAgent.indexOf("Edge") > -1;
const isIOSChrome = winNav.userAgent.match("CriOS");
return (
(isChromium !== null &&
typeof isChromium !== "undefined" &&
vendorName === "Google Inc." &&
isOpera === false &&
isIEedge === false) ||
isIOSChrome
);
},
isOpera: function() {
return this.userAgentContains(" OPR/");
}
};
let DEFAULT_CUSTOM_PROTOCOL_FAIL_CALLBACK_TIMEOUT;
const registerEvent = (target, eventType, cb) => {
if (target.addEventListener) {
target.addEventListener(eventType, cb);
return {
remove: function() {
target.removeEventListener(eventType, cb);
}
};
} else {
target.attachEvent(eventType, cb);
return {
remove: function() {
target.detachEvent(eventType, cb);
}
};
}
};
const createHiddenIframe = (target, uri) => {
let iframe = document.createElement("iframe");
iframe.src = uri;
iframe.id = "hiddenIframe";
iframe.style.display = "none";
target.appendChild(iframe);
return iframe;
};
const openUriWithHiddenFrame = (uri, failCb, successCb) => {
const timeout = setTimeout(function() {
failCb();
handler.remove();
}, DEFAULT_CUSTOM_PROTOCOL_FAIL_CALLBACK_TIMEOUT);
let iframe = document.querySelector("#hiddenIframe");
if (!iframe) {
iframe = createHiddenIframe(document.body, "about:blank");
}
onBlur = () => {
clearTimeout(timeout);
handler.remove();
successCb();
};
const handler = registerEvent(window, "blur", onBlur);
iframe.contentWindow.location.href = uri;
};
const openUriWithTimeoutHack = (uri, failCb, successCb) => {
const timeout = setTimeout(function() {
failCb();
handler.remove();
}, DEFAULT_CUSTOM_PROTOCOL_FAIL_CALLBACK_TIMEOUT);
//handle page running in an iframe (blur must be registered with top level window)
let target = window;
while (target.parent && target != target.parent) {
target = target.parent;
}
onBlur = () => {
clearTimeout(timeout);
handler.remove();
successCb();
};
const handler = registerEvent(target, "blur", onBlur);
window.location.href = uri;
};
const openUriUsingFirefox = (uri, failCb, successCb) => {
let iframe = document.querySelector("#hiddenIframe");
if (!iframe) {
iframe = createHiddenIframe(document.body, "about:blank");
}
try {
iframe.contentWindow.location.href = uri;
successCb();
} catch (e) {
if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL") {
failCb();
}
}
};
const openUriWithMsLaunchUri = (uri, failCb, successCb) => {
navigator.msLaunchUri(uri, successCb, failCb);
};
const getBrowserVersion = () => {
const ua = window.navigator.userAgent;
let tem,
M =
ua.match(
/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i
) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return parseFloat(tem[1]) || "";
}
if (M[1] === "Chrome") {
tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
if (tem != null) {
return parseFloat(tem[2]);
}
}
M = M[2]
? [M[1], M[2]]
: [window.navigator.appName, window.navigator.appVersion, "-?"];
if ((tem = ua.match(/version\/(\d+)/i)) != null) M.splice(1, 1, tem[1]);
return parseFloat(M[1]);
};
const protocolCheck = (uri, failCb, successCb, timeout = 2000, unsupportedCb) => {
const failCallback = () => {
failCb && failCb();
};
const successCallback = () => {
successCb && successCb();
};
const unsupportedCallback = () => {
unsupportedCb && unsupportedCb();
};
const openUri = () => {
if (browser.isFirefox()) {
const browserVersion = getBrowserVersion();
if (browserVersion >= 64) {
openUriWithHiddenFrame(uri, failCallback, successCallback);
} else {
openUriUsingFirefox(uri, failCallback, successCallback);
}
} else if (browser.isChrome()) {
openUriWithTimeoutHack(uri, failCallback, successCallback);
} else if (browser.isOSX()) {
openUriWithHiddenFrame(uri, failCallback, successCallback);
} else {
//not supported, implement please
unsupportedCallback();
}
};
if (timeout) {
DEFAULT_CUSTOM_PROTOCOL_FAIL_CALLBACK_TIMEOUT = timeout;
}
if (browser.isEdge() || browser.isIE()) {
//for IE and Edge in Win 8 and Win 10
openUriWithMsLaunchUri(uri, failCb, successCb);
} else {
if (document.hasFocus()) {
openUri();
} else {
let focusHandler = registerEvent(window, "focus", () => {
focusHandler.remove();
openUri();
});
}
}
};
module.exports = protocolCheck;