Skip to content

Instantly share code, notes, and snippets.

@blackmiaool
Last active January 22, 2018 03:30
Show Gist options
  • Save blackmiaool/38979d5cd2921a55f090341923a795ce to your computer and use it in GitHub Desktop.
Save blackmiaool/38979d5cd2921a55f090341923a795ce to your computer and use it in GitHub Desktop.
try to open app
function checkOpen() {
const clickTime = Date.now();
let timeout;
let count = 0;
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
count++;
timeout = Date.now() - clickTime > 3000;
if (count >= 10 || timeout) {
clearInterval(interval);
if (timeout || document.hidden || document.webkitHidden) {
resolve(true);
} else {
resolve(false);
}
}
}, 200);
});
}
/**
* 尝试唤起app
* @param targetUrl 需要打开的url,可以以https?开头
* @returns {Promise} 返回是否成功唤起的promise
*/
function tryOpenApp(targetUrl = 'youpin://mijiayoupin.com/shop/main') {
targetUrl = targetUrl.replace(/https?:\/\//, "youpin://");
const ifr = document.createElement('iframe');
ifr.src = targetUrl;
ifr.style.display = 'none';
document.body.appendChild(ifr);
setTimeout(() => {
document.body.removeChild(ifr);
}, 2000);
return checkOpen();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment