(win: WindowCapacitor)
| 919 | }; |
| 920 | |
| 921 | function initNativeBridge(win: WindowCapacitor) { |
| 922 | const cap = win.Capacitor || ({} as CapacitorInstance); |
| 923 | |
| 924 | // keep a collection of callbacks for native response data |
| 925 | const callbacks = new Map(); |
| 926 | |
| 927 | const webviewServerUrl = typeof win.WEBVIEW_SERVER_URL === 'string' ? win.WEBVIEW_SERVER_URL : ''; |
| 928 | cap.getServerUrl = () => webviewServerUrl; |
| 929 | cap.convertFileSrc = (filePath) => convertFileSrcServerUrl(webviewServerUrl, filePath); |
| 930 | |
| 931 | // Counter of callback ids, randomized to avoid |
| 932 | // any issues during reloads if a call comes back with |
| 933 | // an existing callback id from an old session |
| 934 | let callbackIdCount = Math.floor(Math.random() * 134217728); |
| 935 | |
| 936 | let postToNative: (data: CallData) => void | null = null; |
| 937 | |
| 938 | const isNativePlatform = () => true; |
| 939 | const getPlatform = () => getPlatformId(win); |
| 940 | |
| 941 | cap.getPlatform = getPlatform; |
| 942 | cap.isPluginAvailable = (name) => Object.prototype.hasOwnProperty.call(cap.Plugins, name); |
| 943 | cap.isNativePlatform = isNativePlatform; |
| 944 | |
| 945 | // create the postToNative() fn if needed |
| 946 | if (getPlatformId(win) === 'android') { |
| 947 | // android platform |
| 948 | postToNative = (data) => { |
| 949 | try { |
| 950 | win.androidBridge.postMessage(JSON.stringify(data)); |
| 951 | } catch (e) { |
| 952 | win?.console?.error(e); |
| 953 | } |
| 954 | }; |
| 955 | } else if (getPlatformId(win) === 'ios') { |
| 956 | // ios platform |
| 957 | postToNative = (data) => { |
| 958 | try { |
| 959 | data.type = data.type ? data.type : 'message'; |
| 960 | win.webkit.messageHandlers.bridge.postMessage(data); |
| 961 | } catch (e) { |
| 962 | win?.console?.error(e); |
| 963 | } |
| 964 | }; |
| 965 | } |
| 966 | |
| 967 | cap.handleWindowError = (msg, url, lineNo, columnNo, err) => { |
| 968 | const str = (msg as string).toLowerCase(); |
| 969 | |
| 970 | if (str.indexOf('script error') > -1) { |
| 971 | // Some IE issue? |
| 972 | } else { |
| 973 | const errObj: ErrorCallData = { |
| 974 | type: 'js.error', |
| 975 | error: { |
| 976 | message: msg as string, |
| 977 | url: url, |
| 978 | line: lineNo, |
no test coverage detected