From 2f356b00b5915ba31431a055ea24f8f74a2bb1df Mon Sep 17 00:00:00 2001 From: Ely Lucas Date: Mon, 15 Feb 2021 16:31:17 -0700 Subject: [PATCH 1/3] fix(core): only add cordova and native events when on native --- core/src/legacy/legacy-handlers.ts | 55 ++++++++++++++++-------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/core/src/legacy/legacy-handlers.ts b/core/src/legacy/legacy-handlers.ts index a3a3e7c6e8..b7d9752e70 100644 --- a/core/src/legacy/legacy-handlers.ts +++ b/core/src/legacy/legacy-handlers.ts @@ -7,35 +7,38 @@ export const initLegacyHandlers = ( win: WindowCapacitor, cap: CapacitorInstance, ): void => { - // define cordova if it's not there already - win.cordova = win.cordova || {}; - const doc = win.document; - const nav = win.navigator; + if (cap.isNativePlatform()) { + // define cordova if it's not there already + win.cordova = win.cordova || {}; - if (nav) { - nav.app = nav.app || {}; - nav.app.exitApp = () => { - cap.nativeCallback('App', 'exitApp', {}); - }; - } + const doc = win.document; + const nav = win.navigator; + + 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 From 49f22fa7fc67010524ac34ab62d0675230260cd0 Mon Sep 17 00:00:00 2001 From: Ely Lucas Date: Mon, 15 Feb 2021 17:09:14 -0700 Subject: [PATCH 2/3] fixing tests --- core/src/tests/legacy.spec.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/src/tests/legacy.spec.ts b/core/src/tests/legacy.spec.ts index 137863fdf3..d50d848189 100644 --- a/core/src/tests/legacy.spec.ts +++ b/core/src/tests/legacy.spec.ts @@ -7,7 +7,7 @@ import { WebPlugin } from '../web-plugin'; describe('legacy', () => { let win: WindowCapacitor; let cap: CapacitorGlobal; - const LegacyWebPlugin = class extends WebPlugin {}; + const LegacyWebPlugin = class extends WebPlugin { }; const orgConsoleWarn = console.warn; const noop = () => { // do nothing @@ -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(); }); From fe07816cf309a56d2644bf703f7fcdfbd20609f7 Mon Sep 17 00:00:00 2001 From: Ely Lucas Date: Tue, 16 Feb 2021 09:37:30 -0700 Subject: [PATCH 3/3] fix lint errors --- core/src/legacy/legacy-handlers.ts | 1 - core/src/tests/legacy.spec.ts | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/core/src/legacy/legacy-handlers.ts b/core/src/legacy/legacy-handlers.ts index b7d9752e70..b7b4366923 100644 --- a/core/src/legacy/legacy-handlers.ts +++ b/core/src/legacy/legacy-handlers.ts @@ -7,7 +7,6 @@ export const initLegacyHandlers = ( win: WindowCapacitor, cap: CapacitorInstance, ): void => { - if (cap.isNativePlatform()) { // define cordova if it's not there already win.cordova = win.cordova || {}; diff --git a/core/src/tests/legacy.spec.ts b/core/src/tests/legacy.spec.ts index d50d848189..70212af388 100644 --- a/core/src/tests/legacy.spec.ts +++ b/core/src/tests/legacy.spec.ts @@ -7,7 +7,7 @@ import { WebPlugin } from '../web-plugin'; describe('legacy', () => { let win: WindowCapacitor; let cap: CapacitorGlobal; - const LegacyWebPlugin = class extends WebPlugin { }; + const LegacyWebPlugin = class extends WebPlugin {}; const orgConsoleWarn = console.warn; const noop = () => { // do nothing @@ -103,7 +103,7 @@ describe('legacy', () => { it('doc.addEventListener backbutton', done => { const AppWeb = class { async addListener(event: any) { - expect(event.eventName).toBe("backButton"); + expect(event.eventName).toBe('backButton'); done(); } }; @@ -121,7 +121,7 @@ describe('legacy', () => { cap = createCapacitor(win); cap.registerPlugin('App', { web: new AppWeb(), - android: new AppWeb() + android: new AppWeb(), }); win.document.addEventListener('backbutton', bbCallback); @@ -134,7 +134,7 @@ describe('legacy', () => { // ignore }, }, - androidBridge: { postMessage: noop } + androidBridge: { postMessage: noop }, }; createCapacitor(win); win.document.addEventListener('deviceready', done); @@ -143,7 +143,7 @@ describe('legacy', () => { it('add navigator.app.exitApp', () => { win = { navigator: {}, - androidBridge: { postMessage: noop } + androidBridge: { postMessage: noop }, }; createCapacitor(win); expect(win.navigator.app.exitApp).toBeDefined();