1 const getClient = () => {
3 navigator: { standalone, userAgent },
5 const lowercaseUserAgent = userAgent.toLowerCase();
6 const safari = /safari/.test(lowercaseUserAgent);
7 const ios = /iphone|ipod|ipad/.test(lowercaseUserAgent);
9 if (ios && typeof (window as any)?.webkit?.messageHandlers?.iOS !== 'undefined') {
10 if (!standalone && safari) {
12 } else if (standalone && !safari) {
14 } else if (!standalone && !safari) {
20 if (typeof (window as any)?.AndroidInterface !== 'undefined') {
24 if ((window as any).chrome && (window as any).chrome.webview) {
31 const broadcast = <T>(message: T) => {
32 const client = getClient();
34 const serialized = JSON.stringify(message);
38 (window as any)?.webkit?.messageHandlers?.iOS?.postMessage?.(serialized);
43 (window as any)?.AndroidInterface?.dispatch?.(serialized);
48 // This is an embedded chrome browser. It uses different message passing mechanism.
49 // (window as any).chrome.webview.postMessage(message);
54 window.parent.postMessage(serialized, '*');
62 export default broadcast;