diff --git a/docs/api.md b/docs/api.md index a0e43868..3fc4b289 100644 --- a/docs/api.md +++ b/docs/api.md @@ -16,8 +16,8 @@ * [page.addScriptTag(url)](#pageaddscripttagurl) * [page.click(selector)](#pageclickselector) * [page.close()](#pageclose) - * [page.evaluate(fun, ...args)](#pageevaluatefun-args) - * [page.evaluateOnInitialized(fun, ...args)](#pageevaluateoninitializedfun-args) + * [page.evaluate(pageFunction, ...args)](#pageevaluatepagefunction-args) + * [page.evaluateOnInitialized(pageFunction, ...args)](#pageevaluateoninitializedpagefunction-args) * [page.focus(selector)](#pagefocusselector) * [page.frames()](#pageframes) * [page.httpHeaders()](#pagehttpheaders) @@ -47,7 +47,7 @@ * [dialog.type](#dialogtype) - [class: Frame](#class-frame) * [frame.childFrames()](#framechildframes) - * [frame.evaluate(fun, ...args)](#frameevaluatefun-args) + * [frame.evaluate(pageFunction, ...args)](#frameevaluatepagefunction-args) * [frame.isDetached()](#frameisdetached) * [frame.isMainFrame()](#frameismainframe) * [frame.name()](#framename) @@ -193,16 +193,16 @@ Adds a `` tag to the page with the desired url. Alternatively, #### page.close() - returns: <[Promise]> Returns promise which resolves when page gets closed. -#### page.evaluate(fun, ...args) -- `fun` <[function]> Function to be evaluated in browser context -- `...args` <...[string]> Arguments to pass to `fun` +#### page.evaluate(pageFunction, ...args) +- `pageFunction` <[function]> Function to be evaluated in browser context +- `...args` <...[string]> Arguments to pass to `pageFunction` - returns: <[Promise]<[Object]>> Promise which resolves to function return value This is a shortcut for [page.mainFrame().evaluate()](#frameevaluatefun-args) method. -#### page.evaluateOnInitialized(fun, ...args) -- `fun` <[function]> Function to be evaluated in browser context -- `...args` <...[string]> Arguments to pass to `fun` +#### page.evaluateOnInitialized(pageFunction, ...args) +- `pageFunction` <[function]> Function to be evaluated in browser context +- `...args` <...[string]> Arguments to pass to `pageFunction` - returns: <[Promise]<[Object]>> Promise which resolves to function `page.evaluateOnInitialized` adds a function which would run on every page navigation before any page's javascript. This is useful to amend javascript environment, e.g. to seed [Math.random](https://github.com/GoogleChrome/puppeteer/blob/master/examples/unrandomize.js) @@ -391,9 +391,9 @@ Dialog's type, could be one of the `alert`, `beforeunload`, `confirm` and `promp - returns: <[Array]<[Frame]>> -#### frame.evaluate(fun, ...args) -- `fun` <[function]> Function to be evaluated in browser context -- `...args` <[Array]<[string]>> Arguments to pass to `fun` +#### frame.evaluate(pageFunction, ...args) +- `pageFunction` <[function]> Function to be evaluated in browser context +- `...args` <[Array]<[string]>> Arguments to pass to `pageFunction` - returns: <[Promise]<[Object]>> Promise which resolves to function return value If the function, passed to the `page.evaluate`, returns a [Promise], then `page.evaluate` would wait for the promise to resolve and return it's value. diff --git a/lib/FrameManager.js b/lib/FrameManager.js index 3f048ec0..cb534531 100644 --- a/lib/FrameManager.js +++ b/lib/FrameManager.js @@ -154,17 +154,17 @@ class FrameManager extends EventEmitter { /** * @param {!Frame} frame - * @param {function()} fun + * @param {function()} pageFunction * @param {!Array<*>} args * @return {!Promise<(!Object|undefined)>} */ - async _evaluateOnFrame(frame, fun, ...args) { + async _evaluateOnFrame(frame, pageFunction, ...args) { let contextId = undefined; if (!frame.isMainFrame()) { contextId = this._frameIdToExecutionContextId.get(frame._id); console.assert(contextId, 'Frame does not have default context to evaluate in!'); } - let syncExpression = helper.evaluationString(fun, ...args); + let syncExpression = helper.evaluationString(pageFunction, ...args); let expression = `Promise.resolve(${syncExpression})`; let { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true }); if (exceptionDetails) { @@ -260,12 +260,12 @@ class Frame { } /** - * @param {function()} fun + * @param {function()} pageFunction * @param {!Array<*>} args * @return {!Promise<(!Object|undefined)>} */ - async evaluate(fun, ...args) { - return this._frameManager._evaluateOnFrame(this, fun, ...args); + async evaluate(pageFunction, ...args) { + return this._frameManager._evaluateOnFrame(this, pageFunction, ...args); } /** diff --git a/lib/Page.js b/lib/Page.js index 9939cd29..e090c7b2 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -312,21 +312,21 @@ class Page extends EventEmitter { } /** - * @param {function()} fun + * @param {function()} pageFunction * @param {!Array<*>} args * @return {!Promise<(!Object|undefined)>} */ - async evaluate(fun, ...args) { - return this._frameManager.mainFrame().evaluate(fun, ...args); + async evaluate(pageFunction, ...args) { + return this._frameManager.mainFrame().evaluate(pageFunction, ...args); } /** - * @param {function()} fun + * @param {function()} pageFunction * @param {!Array<*>} args * @return {!Promise} */ - async evaluateOnInitialized(fun, ...args) { - let scriptSource = helper.evaluationString(fun, ...args); + async evaluateOnInitialized(pageFunction, ...args) { + let scriptSource = helper.evaluationString(pageFunction, ...args); await this._client.send('Page.addScriptToEvaluateOnLoad', { scriptSource }); }