From e0025aec25fe7e4dfd3e45b6f90c01eef96d91ff Mon Sep 17 00:00:00 2001 From: Josh Grime <33323152+joshgrime@users.noreply.github.com> Date: Thu, 27 Aug 2020 01:20:36 +0100 Subject: [PATCH 1/3] Added default fallback for exposeFunction using imported modules --- src/common/Page.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/common/Page.ts b/src/common/Page.ts index 211b1bcba8475..25d37f7dd1349 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -1052,6 +1052,14 @@ export class Page extends EventEmitter { throw new Error( `Failed to add page binding with name ${name}: window['${name}'] already exists!` ); + + if ( + typeof puppeteerFunction !== 'function' && + typeof puppeteerFunction['default'] === 'function' + ) { + puppeteerFunction = puppeteerFunction['default']; + } + this._pageBindings.set(name, puppeteerFunction); const expression = helper.evaluationString(addPageBinding, name); From c82bd64c85c1fce83f8663e5fb6ba3a7ee0f9786 Mon Sep 17 00:00:00 2001 From: Josh Grime <33323152+joshgrime@users.noreply.github.com> Date: Mon, 14 Sep 2020 16:38:24 +0100 Subject: [PATCH 2/3] Added test for default fallback --- test/page.spec.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/page.spec.ts b/test/page.spec.ts index 9d7c5138868ec..4a4e5ab012404 100644 --- a/test/page.spec.ts +++ b/test/page.spec.ts @@ -900,6 +900,21 @@ describe('Page', function () { ); expect(result.x).toBe(7); }); + it('should fallback to default export when passed a module object', async () => { + const { page, server } = getTestState(); + await page.goto(server.EMPTY_PAGE); + var moduleObject = { + default: function(a, b) { + return a * b; + } + }; + // @ts-expect-error + await page.exposeFunction('compute', moduleObject); + const result = await page.evaluate(async function () { + return await globalThis.compute(9, 4); + }); + expect(result).toBe(36); + }); }); describeFailsFirefox('Page.Events.PageError', function () { @@ -1274,6 +1289,9 @@ describe('Page', function () { .catch((error_) => (error = error_)); expect(error).toBeTruthy(); }); + + + }); describe('Page.addStyleTag', function () { From feeb48ed279b38cd925e57662234660ce6330c12 Mon Sep 17 00:00:00 2001 From: Jan Scheffler Date: Wed, 15 Sep 2021 21:22:53 +0200 Subject: [PATCH 3/3] chore: update documentation and types --- docs/api.md | 2 +- src/common/Page.ts | 18 +++++++++++------- test/page.spec.ts | 14 +++++--------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/api.md b/docs/api.md index cd56decf2fbd0..434f3434a4859 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1868,7 +1868,7 @@ await page.evaluateOnNewDocument(preloadFile); #### page.exposeFunction(name, puppeteerFunction) - `name` <[string]> Name of the function on the window object -- `puppeteerFunction` <[function]> Callback function which will be called in Puppeteer's context. +- `puppeteerFunction` <[function]> Callback function which will be called in Puppeteer's context. Can also be a module with a default export. - returns: <[Promise]> The method adds a function called `name` on the page's `window` object. diff --git a/src/common/Page.ts b/src/common/Page.ts index 4fae37357d3c2..d42418208f5c6 100644 --- a/src/common/Page.ts +++ b/src/common/Page.ts @@ -1360,21 +1360,25 @@ export class Page extends EventEmitter { */ async exposeFunction( name: string, - puppeteerFunction: Function + puppeteerFunction: Function | { default: Function } ): Promise { if (this._pageBindings.has(name)) throw new Error( `Failed to add page binding with name ${name}: window['${name}'] already exists!` ); - if ( - typeof puppeteerFunction !== 'function' && - typeof puppeteerFunction['default'] === 'function' - ) { - puppeteerFunction = puppeteerFunction['default']; + let exposedFunction: Function; + if (typeof puppeteerFunction === 'function') { + exposedFunction = puppeteerFunction; + } else if (typeof puppeteerFunction.default === 'function') { + exposedFunction = puppeteerFunction.default; + } else { + throw new Error( + `Failed to add page binding with name ${name}: ${puppeteerFunction} is not a function or a module with a default export.` + ); } - this._pageBindings.set(name, puppeteerFunction); + this._pageBindings.set(name, exposedFunction); const expression = helper.pageBindingInitString('exposedFun', name); await this._client.send('Runtime.addBinding', { name: name }); diff --git a/test/page.spec.ts b/test/page.spec.ts index 48eb23e72de6a..39304f84e6f39 100644 --- a/test/page.spec.ts +++ b/test/page.spec.ts @@ -1018,13 +1018,12 @@ describe('Page', function () { }); it('should fallback to default export when passed a module object', async () => { const { page, server } = getTestState(); + const moduleObject = { + default: function (a, b) { + return a * b; + }, + }; await page.goto(server.EMPTY_PAGE); - var moduleObject = { - default: function(a, b) { - return a * b; - } - }; - // @ts-expect-error await page.exposeFunction('compute', moduleObject); const result = await page.evaluate(async function () { return await globalThis.compute(9, 4); @@ -1452,9 +1451,6 @@ describe('Page', function () { .catch((error_) => (error = error_)); expect(error).toBeTruthy(); }); - - - }); describe('Page.addStyleTag', function () {