Skip to content

Instantly share code, notes, and snippets.

@latel
Last active December 17, 2019 07:55
Show Gist options
  • Save latel/5346026c965098c5df1780fe847cdd55 to your computer and use it in GitHub Desktop.
Save latel/5346026c965098c5df1780fe847cdd55 to your computer and use it in GitHub Desktop.
const waitForJSBridge = new Promise((resolve, reject) => {
if (typeof window.StockJSBridge === 'undefined') {
let initTimer;
let _resolve = () => {
window.clearTimeout(initTimer);
resolve();
};
initTimer = window.setTimeout(() => {
reject();
}, 6000);
if (document.addEventListener) {
document.addEventListener('StockJSBridgeReady', _resolve, false);
} else if (document.attachEvent) {
document.attachEvent('StockJSBridgeReady', _resolve);
document.attachEvent('onStockJSBridgeReady', _resolve);
}
} else {
// 安卓开发反馈客户端注入的时间可能比js执行的早
// 或者sdk在调用之前已经初始化好了
resolve();
}
});
const sdk = {
async closeWindow() {
await waitForJSBridge;
window.StockJSBridge.invoke('exit', {});
}
}
sdk.closeWindow();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment