Skip to content

Instantly share code, notes, and snippets.

@jaceju
Last active August 29, 2015 14:02
Show Gist options
  • Save jaceju/f499112d48029fb55542 to your computer and use it in GitHub Desktop.
Save jaceju/f499112d48029fb55542 to your computer and use it in GitHub Desktop.
Use custom protocol to open KKBOX app on desktop or mobile.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>App Redirection</title>
<!-- see https://gist.github.com/pulletsforever/2662899 -->
</head>
<body>
<iframe style="display:none" height="0" width="0" id="loader"></iframe>
<script>
(function() {
// 定義 iframe 的處理
var loader = document.getElementById('loader');
// 如果無法打開自訂連結的話,就判斷目前平台來開啟 App 下載頁
loader.addEventListener('error', function () {
// 預設是 Desktop
var fallbackLink = 'http://www.kkbox.com/tw/tc/what-is-kkbox/supported-platforms.shtml';
// 判斷 Mobile 平台
var isiOS = navigator.userAgent.match('iPad') ||
navigator.userAgent.match('iPhone') ||
navigator.userAgent.match('iPod'),
isAndroid = navigator.userAgent.match('Android');
// 決定要從何處下載
if (isiOS || isAndroid) {
fallbackLink = isAndroid ?
// 在 Google Play 可以找到該 App 的連結
'https://play.google.com/store/apps/details?id=com.skysoft.kkbox.android' :
// 在 iTunes 裡可以找到該 App 的分享用連結
'itms-apps://itunes.apple.com/tw/app/kkbox/id300915900?l=zh&mt=8';
}
// 切換到下載頁
window.setTimeout(function() {
window.location.replace(fallbackLink);
}, 1);
});
// 載入成功就不做任何事
loader.addEventListener('load', function() {
clearTimeout(timeout);
});
// 試試看能不能呼叫 App 播放
loader.src = 'kkbox://playlist_144_98847';
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment