Created
September 22, 2017 02:47
-
-
Save Hackforid/af5855b7a54b01017ae616542bdf278c to your computer and use it in GitHub Desktop.
Deeplink兼容实现
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
<!DOCTYPE html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<button class="test" style="width:100%; height:300px" onclick="test()">test</button> | |
<script> | |
document.getElementsByClassName("test").onclick = function() { | |
} | |
function test() { | |
function isChrome() { | |
return window.chrome | |
} | |
//window.location.href="https://m-link.starmakerstudios.com/party_room?roomid=4&a=1" | |
//window.location.href="intent://m-link.starmakerstudios.com/party_room?roomId=4&a=1#Intent;scheme=https;package=com.starmakerinteractive.starmaker;S.browser_fallback_url=https://m-link.starmakerstudios.com/party_room?roomid=4;end" | |
function openApp(openUrl, appUrl) { | |
//检查app是否打开 | |
function checkOpen(cb){ | |
var _clickTime = +(new Date()); | |
function check(elsTime) { | |
if ( elsTime > 3000 || document.hidden || document.webkitHidden) { | |
console.log("> 3000") | |
cb(true); | |
} else { | |
console.log("< 3000") | |
cb(false); | |
} | |
} | |
//启动间隔20ms运行的定时器,并检测累计消耗时间是否超过3000ms,超过则结束 | |
var _count = 0, intHandle; | |
intHandle = setInterval(function(){ | |
_count++; | |
var elsTime = +(new Date()) - _clickTime; | |
if (_count>=100 || elsTime > 3000 ) { | |
clearInterval(intHandle); | |
check(elsTime); | |
} | |
}, 20); | |
} | |
//在iframe 中打开APP | |
var ifr = document.createElement('iframe'); | |
ifr.src = openUrl; | |
ifr.style.display = 'none'; | |
if (appUrl) { | |
checkOpen(function(opened){ | |
if (!opened) { | |
window.location.href = appUrl | |
} | |
}); | |
} | |
document.body.appendChild(ifr); | |
console.log('ifr load url ' + openUrl) | |
setTimeout(function() { | |
document.body.removeChild(ifr); | |
}, 2000); | |
} | |
if (isChrome()) { | |
window.location.href="intent://m-link.starmakerstudios.com/party_room?roomId=4&a=1#Intent;scheme=https;package=com.starmakerinteractive.starmaker;S.browser_fallback_url=https://baidu.com?roomid=4;end" | |
} else { | |
openApp("sm://party_room?roomid=4", "https://baidu.com") | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment