diff --git a/core/src/legacy/legacy-handlers.ts b/core/src/legacy/legacy-handlers.ts index a3a3e7c6e8..b7b4366923 100644 --- a/core/src/legacy/legacy-handlers.ts +++ b/core/src/legacy/legacy-handlers.ts @@ -7,35 +7,37 @@ export const initLegacyHandlers = ( win: WindowCapacitor, cap: CapacitorInstance, ): void => { - // define cordova if it's not there already - win.cordova = win.cordova || {}; + if (cap.isNativePlatform()) { + // define cordova if it's not there already + win.cordova = win.cordova || {}; - const doc = win.document; - const nav = win.navigator; + const doc = win.document; + const nav = win.navigator; - if (nav) { - nav.app = nav.app || {}; - nav.app.exitApp = () => { - cap.nativeCallback('App', 'exitApp', {}); - }; - } + if (nav) { + nav.app = nav.app || {}; + nav.app.exitApp = () => { + cap.nativeCallback('App', 'exitApp', {}); + }; + } - if (doc) { - const docAddEventListener = doc.addEventListener; - doc.addEventListener = (...args: any[]) => { - const eventName = args[0]; - const handler = args[1]; - if (eventName === 'deviceready' && handler) { - Promise.resolve().then(handler); - } else if (eventName === 'backbutton' && cap.Plugins.App) { - // Add a dummy listener so Capacitor doesn't do the default - // back button action - cap.Plugins.App.addListener('backButton', () => { - // ignore - }); - } - return docAddEventListener.apply(doc, args); - }; + if (doc) { + const docAddEventListener = doc.addEventListener; + doc.addEventListener = (...args: any[]) => { + const eventName = args[0]; + const handler = args[1]; + if (eventName === 'deviceready' && handler) { + Promise.resolve().then(handler); + } else if (eventName === 'backbutton' && cap.Plugins.App) { + // Add a dummy listener so Capacitor doesn't do the default + // back button action + cap.Plugins.App.addListener('backButton', () => { + // ignore + }); + } + return docAddEventListener.apply(doc, args); + }; + } } // deprecated in v3, remove from v4 diff --git a/core/src/tests/legacy.spec.ts b/core/src/tests/legacy.spec.ts index 137863fdf3..70212af388 100644 --- a/core/src/tests/legacy.spec.ts +++ b/core/src/tests/legacy.spec.ts @@ -102,8 +102,8 @@ describe('legacy', () => { it('doc.addEventListener backbutton', done => { const AppWeb = class { - async addListener(eventName: string) { - expect(eventName).toBe('backButton'); + async addListener(event: any) { + expect(event.eventName).toBe('backButton'); done(); } }; @@ -116,10 +116,12 @@ describe('legacy', () => { expect(eventName).toBe('backbutton'); }, }, + androidBridge: { postMessage: noop }, }; cap = createCapacitor(win); cap.registerPlugin('App', { web: new AppWeb(), + android: new AppWeb(), }); win.document.addEventListener('backbutton', bbCallback); @@ -132,6 +134,7 @@ describe('legacy', () => { // ignore }, }, + androidBridge: { postMessage: noop }, }; createCapacitor(win); win.document.addEventListener('deviceready', done); @@ -140,13 +143,16 @@ describe('legacy', () => { it('add navigator.app.exitApp', () => { win = { navigator: {}, + androidBridge: { postMessage: noop }, }; createCapacitor(win); expect(win.navigator.app.exitApp).toBeDefined(); }); it('cordova global', () => { - win = {}; + win = { + androidBridge: { postMessage: noop }, + }; createCapacitor(win); expect(win.cordova).toBeDefined(); });