On every repo, GitHub has a button that is labelled "Clone in Desktop" (example: https://github.com/github/developer.github.com). If you have GitHub for Mac installed, the href
is something like:
github-mac://openRepo/https://github.com/github/developer.github.com
This opens GitHub for Mac and offers to clone the repo. If you don't, the href
is:
http://mac.github.io`
This is a download page for GitHub for Mac. I would like to do something similar on my website: open my app if installed and redirect to download if not. How can this be best accomplished?
This could be achieved with Javascript or PHP...
For Javascript you have a cool library is.js that you can use for environment checks. See here: http://arasatasaygin.github.io/is.js/#environment-checks
You just create a div in your HTML and than use javascript to populate it with content depending on the result of functions like: is_windows() or !is_windows()
You can also use PHP. For this you can use the code in the first answer of this question: Get users OS and version number
And now based on the return of getOS() and/or getBrowser function you can present your users different content ... that's a simples if else block code :)
Enjoy!
How does GitHub do it?
The GitHub for Mac client includes a local service called GitHub Conduit that runs in the background. The GitHub page communicates with this service through the the URL
https://ghconduit.com:25035/status
.Excerpt from the GitHub Conduit help page:
If you have the GitHub Conduit service running, JavaScript on the page fetches data from this URL and give the Clone in Desktop button a
github-mac://
URL. Otherwise, the URL returns a 404 response and it assumes you do not have GitHub for Mac installed, and gives you the link to download it.How to implement functionality like this?
Unfortunately, there is no JavaScript API to do this in the browser. Protocols the browser does not recognize are handled by the OS itself. I tried my best, but I was only able to hack-up a decent JavaScript-only solution for Firefox on Mac, and an ugly half-baked solution for Safari. Both hacks hinge on undefined behavior, and neither work for Chrome. You can see my research code below.
If you want to do it the GitHub way, you will have to create a local HTTP server that runs as a service over a known port on your user's machines. Then you can use JavaScript to connect to it and retrieve information about the installed application. Doing this would not be trivial, and unless it provides some amazing functionality, I would advise against doing this.
The JavaScript code to do this would be fairly simple though. Assuming you return the appropriate CORS headers, you could just make a simple AJAX request. Here's a jQuery-based example.
Research Code:
The following code is my super-hacky and rather fragile code for Firefox and Safari. While it is working on my end, I make absolutely no guarantee that it will work as expected, or will continue to work in the future. It relies on browser-specific undefined behavior, and should be considered unstable. I also have no idea what this code will do on non-OS X systems.
Firefox:
This code relies on opening the link in an iframe that will trigger an error when a protocol is unrecognized (on success it will open the URL as normal).
Example Usage:
Safari:
This code relies on opening the URL in a new tab, whose
win.location.href
will beforeundefined
within a second (but probably less time) if the URL was not recognized. The "There is no application set to open the URL" dialog will still open if it fails to open the protocol unfortunately.Example Usage: