Skip to content

Commit 3909133

Browse files
committed
migrate module to npm package
1 parent 535e459 commit 3909133

File tree

4 files changed

+387
-164
lines changed

4 files changed

+387
-164
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

index.js

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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+
}

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "custom-protocol-detection",
3+
"version": "1.0.0",
4+
"description": "Detect whether a custom protocol is available in browser (FF, Chrome, IE8, IE9, IE10, IE11, and Edge)",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "browserify index.js --standalone protocolCheck > protocolcheck.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/ismailhabib/custom-protocol-detection.git"
12+
},
13+
"keywords": [],
14+
"author": "Ismail Habib Muhammad (https://github.com/ismailhabib)",
15+
"bugs": {
16+
"url": "https://github.com/ismailhabib/custom-protocol-detection/issues"
17+
},
18+
"homepage": "https://github.com/ismailhabib/custom-protocol-detection#readme",
19+
"devDependencies": {
20+
"browserify": "^13.1.1"
21+
}
22+
}

0 commit comments

Comments
 (0)