mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Merge Page.evaluate and Page.evaluateAsync together
This patch makes Page.evaluate await promise if one is returned by the evaluated code. This makes the Page.evaluateAsync unneeded, so the patch removes it. Fixes #11.
This commit is contained in:
41
lib/Page.js
41
lib/Page.js
@@ -89,7 +89,7 @@ class Page extends EventEmitter {
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async addScriptTag(url) {
|
||||
return this.evaluateAsync(addScriptTag, url);
|
||||
return this.evaluate(addScriptTag, url);
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
@@ -125,7 +125,7 @@ class Page extends EventEmitter {
|
||||
|
||||
var sourceURL = '__in_page_callback__' + name;
|
||||
this._sourceURLToPageCallback.set(sourceURL, new InPageCallback(name, callback));
|
||||
var text = helpers.evaluationString(inPageCallback, [name], false /* awaitPromise */, sourceURL);
|
||||
var text = helpers.evaluationString(inPageCallback, [name], false /* wrapInPromise */, sourceURL);
|
||||
await Promise.all([
|
||||
this._client.send('Debugger.enable', {}),
|
||||
this._client.send('Page.addScriptToEvaluateOnLoad', { scriptSource: text }),
|
||||
@@ -326,25 +326,32 @@ class Page extends EventEmitter {
|
||||
* @return {!Promise<(!Object|undefined)>}
|
||||
*/
|
||||
async evaluate(fun, ...args) {
|
||||
var response = await helpers.evaluate(this._client, fun, args, false /* awaitPromise */);
|
||||
var code = helpers.evaluationString(fun, args, false /* wrapInPromise */);
|
||||
var response = await this._client.send('Runtime.evaluate', {
|
||||
expression: code
|
||||
});
|
||||
if (response.exceptionDetails) {
|
||||
this._handleException(response.exceptionDetails);
|
||||
return;
|
||||
}
|
||||
var remoteObject = response.result;
|
||||
if (remoteObject.type !== 'object')
|
||||
return remoteObject.value;
|
||||
var isPromise = remoteObject.type === 'object' && remoteObject.subtype === 'promise';
|
||||
var response = await this._client.send('Runtime.callFunctionOn', {
|
||||
objectId: remoteObject.objectId,
|
||||
functionDeclaration: 'function() { return this; }',
|
||||
returnByValue: true,
|
||||
awaitPromise: isPromise
|
||||
});
|
||||
await this._client.send('Runtime.releaseObject', {
|
||||
objectId: remoteObject.objectId
|
||||
});
|
||||
if (response.exceptionDetails) {
|
||||
this._handleException(response.exceptionDetails);
|
||||
return;
|
||||
}
|
||||
return response.result.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function()} fun
|
||||
* @param {!Array<*>} args
|
||||
* @return {!Promise<(!Object|undefined)>}
|
||||
*/
|
||||
async evaluateAsync(fun, ...args) {
|
||||
var response = await helpers.evaluate(this._client, fun, args, true /* awaitPromise */);
|
||||
if (response.exceptionDetails) {
|
||||
this._handleException(response.exceptionDetails);
|
||||
return;
|
||||
}
|
||||
return response.result.value;
|
||||
}
|
||||
|
||||
@@ -354,7 +361,7 @@ class Page extends EventEmitter {
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async evaluateOnInitialized(fun, ...args) {
|
||||
var code = helpers.evaluationString(fun, args, false);
|
||||
var code = helpers.evaluationString(fun, args, false /* wrapInPromise */);
|
||||
await this._client.send('Page.addScriptToEvaluateOnLoad', {
|
||||
scriptSource: code
|
||||
});
|
||||
|
||||
@@ -74,14 +74,14 @@ var helpers = module.exports = {
|
||||
/**
|
||||
* @param {function()} fun
|
||||
* @param {!Array<*>} args
|
||||
* @param {boolean} awaitPromise
|
||||
* @param {boolean} wrapInPromise
|
||||
* @param {string=} sourceURL
|
||||
* @return {string}
|
||||
*/
|
||||
evaluationString: function(fun, args, awaitPromise, sourceURL) {
|
||||
evaluationString: function(fun, args, wrapInPromise, sourceURL) {
|
||||
var argsString = args.map(x => JSON.stringify(x)).join(',');
|
||||
var code = `(${fun.toString()})(${argsString})`;
|
||||
if (awaitPromise)
|
||||
if (wrapInPromise)
|
||||
code = `Promise.resolve(${code})`;
|
||||
if (sourceURL)
|
||||
code += `\n//# sourceURL=${sourceURL}`;
|
||||
|
||||
Reference in New Issue
Block a user