Skip to content

Commit

Permalink
fix(core): do not add window.cordova on web apps (#4214)
Browse files Browse the repository at this point in the history
  • Loading branch information
elylucas authored Feb 17, 2021
1 parent 91ddfbd commit 6d673ef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
54 changes: 28 additions & 26 deletions core/src/legacy/legacy-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions core/src/tests/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};
Expand All @@ -116,10 +116,12 @@ describe('legacy', () => {
expect(eventName).toBe('backbutton');
},
},
androidBridge: { postMessage: noop },
};
cap = createCapacitor(win);
cap.registerPlugin<any>('App', {
web: new AppWeb(),
android: new AppWeb(),
});

win.document.addEventListener('backbutton', bbCallback);
Expand All @@ -132,6 +134,7 @@ describe('legacy', () => {
// ignore
},
},
androidBridge: { postMessage: noop },
};
createCapacitor(win);
win.document.addEventListener('deviceready', done);
Expand All @@ -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();
});
Expand Down

0 comments on commit 6d673ef

Please sign in to comment.