Skip to content

Instantly share code, notes, and snippets.

@ilya
Last active August 29, 2015 14:00
Show Gist options
  • Save ilya/eb141aca45705c6b4055 to your computer and use it in GitHub Desktop.
Save ilya/eb141aca45705c6b4055 to your computer and use it in GitHub Desktop.
$ curl -A "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3" http://urx.io/107.170.233.189
<!doctype html>
<html>
<head>
<title>Redirecting...</title>
<style type="text/css">
body {
background-color: #FFFFFF;
}
</style>
<script>
var deeplink = "sample://page1",
fallbackLink = "http://107.170.233.189";
var redirect = function() {
if (deeplink) {
window.location.replace(deeplink);
}
setTimeout(function () {
if(document.webkitHidden === false || document.hidden === false) {
window.location.replace(fallbackLink);
}
}, 100);
};
window.onload = redirect;
</script>
</head>
<body></body>
</html>
$ curl -A "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1" http://urx.io/107.170.233.189
<!doctype html>
<html>
<head>
<title>Redirecting...</title>
<style type="text/css">
body {
background-color: #FFFFFF;
}
</style>
<script>
var deeplink = "sample://page1",
fallbackLink = "http://107.170.233.189";
var useFallback = function(reason) {
console.log("using fallback because " + reason);
window.location.replace(fallbackLink);
}
var now = function() { return new Date().getTime(); }
var redirect = function() {
var currentBrowser, browser, version;
if ( browser = window.navigator.userAgent.match(/Chrome\/(\d+)\./) ) {
currentBrowser = "Chrome";
} else if ( browser = window.navigator.userAgent.match(/Firefox\/(\d+)\./) ) {
currentBrowser = "Firefox";
}
if(browser) {
version = parseInt(browser[1], 10);
}
// For firefox and chrome before version 25, try redirect using iframe
if ( currentBrowser === "Firefox" ||
(currentBrowser === "Chrome" && version < 25) ) {
var fallbackTimeout = setTimeout(function(){
useFallback("fallbackTimeout triggered");
}, 500);
var hiddenIFrame = document.createElement('iframe');
hiddenIFrame.style.width = '1px';
hiddenIFrame.style.height = '1px';
hiddenIFrame.border = 'none';
hiddenIFrame.addEventListener('load', function() {
clearTimeout(fallbackTimeout);
});
hiddenIFrame.src = deeplink;
document.body.appendChild(hiddenIFrame);
} else if(currentBrowser === "Chrome" && version > 25){
// try the deeplink, can't handle failure here.
window.location.replace(deeplink);
} else {
// else redirect to fallback
useFallback("not supported " + browser);
}
}
window.onload = redirect;
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment