-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfb-deep-link.js
executable file
·63 lines (50 loc) · 1.47 KB
/
fb-deep-link.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
/*
* fb-deep-link.js v0.1
* Facebook deep linking
* Redirect Facebook web links to Facebook App on iOS and Android.
*
* https://github.com/imnotadeveloper/facebook-deep-linking
* Licensed under MIT License
*/
(function() {
// OS patterns detection based on:
// https://github.com/hgoebl/mobile-detect.js/blob/master/mobile-detect.js
var isIOS = osPattern('\\biPhone.*Mobile|\\biPod|\\biPad|AppleCoreMedia');
var isAndroidOS = osPattern('Android');
// Listen fbLinks clicks on iOs and AndroidOS
if (isIOS || isAndroidOS) {
var fbLinks = document.querySelectorAll('[data-fbid]');
for (var i = 0; i < fbLinks.length; i++) {
fbLinks[i].addEventListener('click', function() {
event.preventDefault();
var url = event.toElement.getAttribute('href');
var fbid = event.toElement.dataset.fbid;
// URIs
if (isIOS) {
var uri = 'fb://profile/' + fbid;
}
else if (isAndroidOS) {
var uri = 'fb://facewebmodal/f?href=' + url;
}
else {
var uri = url;
}
// Redirect to URI
window.location = uri;
// Fallback to URL
setTimeout(function() {
window.location = url;
}, 1);
});
}
}
/**
* Check OS Pattern in User-Agent.
*
* @param {string} pattern
* @return {boolean}
*/
function osPattern(pattern) {
return new RegExp(pattern, 'i').test(navigator.userAgent) ? true : false;
}
})();