Forked from amitsaxena/gist:9cb0712e572c39a41edc
Last active
August 29, 2015 14:10
-
-
Save PatrickJS/e94ecb99e5a96d2f58fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script type="text/javascript"> | |
| var custom = "myapp://custom_url"; | |
| var alt = "http://mywebsite.com/alternate/content"; | |
| var g_intent = "intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end"; | |
| var timer; | |
| var heartbeat; | |
| var iframe_timer; | |
| function clearTimers() { | |
| clearTimeout(timer); | |
| clearTimeout(heartbeat); | |
| clearTimeout(iframe_timer); | |
| } | |
| function intervalHeartbeat() { | |
| if (document.webkitHidden || document.hidden) { | |
| clearTimers(); | |
| } | |
| } | |
| function tryIframeApproach() { | |
| var iframe = document.createElement("iframe"); | |
| iframe.style.border = "none"; | |
| iframe.style.width = "1px"; | |
| iframe.style.height = "1px"; | |
| iframe.onload = function () { | |
| document.location = alt; | |
| }; | |
| iframe.src = custom; | |
| document.body.appendChild(iframe); | |
| } | |
| function tryWebkitApproach() { | |
| document.location = custom; | |
| timer = setTimeout(function () { | |
| document.location = alt; | |
| }, 2500); | |
| } | |
| function useIntent() { | |
| document.location = g_intent; | |
| } | |
| function launch_app_or_alt_url(el) { | |
| heartbeat = setInterval(intervalHeartbeat, 200); | |
| if (navigator.userAgent.match(/Chrome/)) { | |
| useIntent(); | |
| } else if (navigator.userAgent.match(/Firefox/)) { | |
| tryWebkitApproach(); | |
| iframe_timer = setTimeout(function () { | |
| tryIframeApproach(); | |
| }, 1500); | |
| } else { | |
| tryIframeApproach(); | |
| } | |
| } | |
| $(".source_url").click(function (event) { | |
| launch_app_or_alt_url($(this)); | |
| event.preventDefault(); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment