Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): do not add window.cordova on web apps #4214

Merged
merged 4 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
imhoffd marked this conversation as resolved.
Show resolved Hide resolved
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