MCPcopy
hub / github.com/ionic-team/capacitor / initNativeBridge

Function initNativeBridge

android/capacitor/src/main/assets/native-bridge.js:826–1020  ·  view source on GitHub ↗
(win)

Source from the content-addressed store, hash-verified

824 win.Capacitor = cap;
825 };
826 function initNativeBridge(win) {
827 const cap = win.Capacitor || {};
828 // keep a collection of callbacks for native response data
829 const callbacks = new Map();
830 const webviewServerUrl = typeof win.WEBVIEW_SERVER_URL === 'string' ? win.WEBVIEW_SERVER_URL : '';
831 cap.getServerUrl = () => webviewServerUrl;
832 cap.convertFileSrc = (filePath) => convertFileSrcServerUrl(webviewServerUrl, filePath);
833 // Counter of callback ids, randomized to avoid
834 // any issues during reloads if a call comes back with
835 // an existing callback id from an old session
836 let callbackIdCount = Math.floor(Math.random() * 134217728);
837 let postToNative = null;
838 const isNativePlatform = () => true;
839 const getPlatform = () => getPlatformId(win);
840 cap.getPlatform = getPlatform;
841 cap.isPluginAvailable = (name) => Object.prototype.hasOwnProperty.call(cap.Plugins, name);
842 cap.isNativePlatform = isNativePlatform;
843 // create the postToNative() fn if needed
844 if (getPlatformId(win) === 'android') {
845 // android platform
846 postToNative = (data) => {
847 var _a;
848 try {
849 win.androidBridge.postMessage(JSON.stringify(data));
850 }
851 catch (e) {
852 (_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.error(e);
853 }
854 };
855 }
856 else if (getPlatformId(win) === 'ios') {
857 // ios platform
858 postToNative = (data) => {
859 var _a;
860 try {
861 data.type = data.type ? data.type : 'message';
862 win.webkit.messageHandlers.bridge.postMessage(data);
863 }
864 catch (e) {
865 (_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.error(e);
866 }
867 };
868 }
869 cap.handleWindowError = (msg, url, lineNo, columnNo, err) => {
870 const str = msg.toLowerCase();
871 if (str.indexOf('script error') > -1) ;
872 else {
873 const errObj = {
874 type: 'js.error',
875 error: {
876 message: msg,
877 url: url,
878 line: lineNo,
879 col: columnNo,
880 errorObject: JSON.stringify(err),
881 },
882 };
883 if (err !== null) {

Callers 1

initBridgeFunction · 0.70

Calls 11

errorMethod · 0.80
warnMethod · 0.80
convertFileSrcServerUrlFunction · 0.70
getPlatformIdFunction · 0.70
initLoggerFunction · 0.70
returnResultFunction · 0.70
initEventsFunction · 0.70
initLegacyHandlersFunction · 0.70
initVendorFunction · 0.70
postMessageMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected