11 11 / 2014
This is what is required when you sit to debug/enhance a crawler that you released into the wild-wild world of internet, with all kind oh HTML criminals roaming around. You often wonder whether they made mistakes, or if those were deliberate attempts to deface the internet. You hope that these people were in jail instead of producing code =P
Then the realization dawns upon you that the world of web is infinite, and you are but a minuscule drop in the ocean of world wide web. You make peace with yourself, and indulge in a lot of philosophy. Then you write code, which is all that really matters. Inner Peace!
10 6 / 2014
Smallest piece of code that’s going to change the developing nations :P
Soon after solving the First world problems, I am back to cross the bridge, as promised. This time around I am going to solve the same problem for the developing world aka Android. Yes you guessed it right - how to open a custom URL in the app if the app is installed, and open an alternate URL if the app is not available on the device, on an android browser. All for free ;)
As we all know, third (+ second) world problems are much harder to solve, in comparison to first world problems. This applies here as well =P. Given the fragmentation in android devices, it’s almost impossible to test it on all devices and browsers. So I would say that the solution below works for most of the android browsers, and will list down known issues towards the end.
Below is the working javascript code snippet. To explain it briefly, there are 3 approaches that are at work here. One is the iFrame approach which works in older browsers (older android versions and default Android Browser), where the browser shows up a different page when a URL is not found. The other uses the Page visibility API, which some modern browsers have implemented. I use a combination of both, and some inception (timeout within timeout) to make it work on most android browsers. The third approach is based on android intents (which only works on Chrome).
It looks like chrome specifically introduced a page to show up for such scenarios, so that you couldn’t use the iframe approach (for version 25 and later). They did all this to force you to use a feature they created - android intents. In essence they wrote more code, so that they can force you to use the code they had already written in the first place. F#@k you chrome! F#@k you google! You made me work harder =P And there are specific scenarios which intents don’t handle. What if I don’t want the user to visit play store and I have the same content in my apps, as well as on my website. For users with app installed, I take them to the app, whereas others are taken to a web URL. Think Inc, Forbes, and all the magazine apps. I personally have a lot of respect for google, and this is one of the shittiest moves I have seen from you. I hope the project manager who forced this onto the world sees this, and expresses his ‘intent’ about 'Android Intents’ ;) You owning a popular browser, doesn’t mean you can force others to do whatever you want (or may be you can).
Please note that you will need to create an intent such that it can handle all your custom URL schemes. I am not an android dev myself, but I checked with my colleagues, and it is very much possible.
One known issue is that the visibility API seemingly doesn’t work on Firefox for android (ironically though looks like Mozilla proposed it in the first place), and therefore the alternate URL is opened even when the custom URL works (app installed). Not that bad ehhh…!
Looks like there is a reason why developers prefer iOS over android. As end users you may not feel the pinch, but we definitely do ;)
![image](https://66.media.tumblr.com/0ecebcd1c0a81cc308a03273de9c42a1/tumblr_inline_p9n7hfIKCc1rj6oo2_500.jpg)
If you are elated by the solution, send me money. If you are pissed off by the analogies, or my stupid sense of humor, send me more money =P
PS: Initially I wanted to do this without intents, due to the reasons described above. I spent hours, if not days trying to work around the chrome stupidity, but to no avail. Eventually I found that link where it was mentioned that chrome doesn’t want you to do that. Looks like for now you will have to live with it. If you have suggestions to improve upon this, or better approaches to work around this problem, please drop a note in the comments section.