-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
core(driver): create eval code using interface #10816
Changes from 3 commits
90018b2
f7e38f5
78f386a
32856e7
e2dd63d
f38bf41
ce9839b
4c903de
0413a4a
d4bd2bf
02dc215
ce2827a
c81a636
70d8210
df5e8dd
4e59476
147b98c
a4051a1
77afd88
fd94564
9e7644e
f42ca84
b1fb5d3
c4d3a58
ee6f930
6be38eb
97b0525
81ea5b5
4843291
3c9f573
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -418,17 +418,86 @@ class Driver { | |
}); | ||
} | ||
|
||
/** | ||
* Deprecated: renamed to `evaluate`. | ||
* @param {string} expression | ||
* @param {{useIsolation?: boolean}=} options | ||
* @return {Promise<*>} | ||
*/ | ||
async evaluateAsync(expression, options = {}) { | ||
return this.evaluate(expression, options); | ||
} | ||
|
||
/** | ||
* Evaluate an expression in the context of the current page. If useIsolation is true, the expression | ||
* will be evaluated in a content script that has access to the page's DOM but whose JavaScript state | ||
* is completely separate. | ||
* Returns a promise that resolves on the expression's value. | ||
* See the documentation for `createEvalCode` in `page-functions.js`. | ||
* @template T, R | ||
* @param {string | ((...args: T[]) => R)} expressionOrMainFn | ||
* @param {{useIsolation?: boolean, args?: T[], deps?: (Function|string)[]}=} options | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI I would like to drop the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
could we drop it now and say "if you still want to use a string use the legacy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* @return {Promise<R>} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just split these into two eval functions?. Keep
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ("fun" fact: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the great feedback!
I took a second pass to incorporate this feedback, and added some docs. Running to a meeting, will respond to the rest later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you prefer splitting this out to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rather than add another
then in the next breaking change we either incorporate the guts of I see the reason for doing it the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little confused about the purpose of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I just forgot to delete |
||
*/ | ||
async evaluate(expressionOrMainFn, options = {}) { | ||
if (typeof expressionOrMainFn !== 'string') { | ||
expressionOrMainFn = pageFunctions.createEvalCode(expressionOrMainFn, {mode: 'iffe', ...options}); | ||
} | ||
return this._evaluate(expressionOrMainFn, options); | ||
} | ||
|
||
/** | ||
* Evaluate a statement in the context of a JavaScript object on the current page. | ||
* Returns a promise that resolves on the expression's value. | ||
* See the documentation for `createEvalCode` in `page-functions.js`. | ||
* @template T, R | ||
* @param {string | ((...args: T[]) => R)} statementOrMainFn | ||
* @param {{objectId: string, args?: T[], deps?: (Function|string)[]}} options | ||
* @return {Promise<R>} | ||
*/ | ||
async evaluateFunctionOnObject(statementOrMainFn, options) { | ||
connorjclark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (typeof statementOrMainFn !== 'string') { | ||
statementOrMainFn = pageFunctions.createEvalCode(statementOrMainFn, { | ||
mode: 'function', | ||
...options, | ||
}); | ||
} | ||
|
||
const response = await this.sendCommand('Runtime.callFunctionOn', { | ||
objectId: options.objectId, | ||
functionDeclaration: statementOrMainFn, | ||
returnByValue: true, | ||
awaitPromise: true, | ||
}); | ||
|
||
if (response.exceptionDetails) { | ||
// An error occurred before we could even create a Promise, should be *very* rare. | ||
// Also occurs when the expression is not valid JavaScript. | ||
const errorMessage = response.exceptionDetails.exception ? | ||
response.exceptionDetails.exception.description : | ||
response.exceptionDetails.text; | ||
return Promise.reject(new Error(`Evaluation exception: ${errorMessage}`)); | ||
} | ||
|
||
// TODO: check if __failedInBrowser happens here too. | ||
if (response && response.result && response.result.value) { | ||
return response.result.value; | ||
} | ||
return Promise.reject(); | ||
} | ||
|
||
/** | ||
* Evaluate an expression in the context of the current page. If useIsolation is true, the expression | ||
* will be evaluated in a content script that has access to the page's DOM but whose JavaScript state | ||
* is completely separate. | ||
* Returns a promise that resolves on the expression's value. | ||
* @param {string} expression | ||
* @param {{useIsolation?: boolean}=} options | ||
* @param {{useIsolation?: boolean}} options | ||
* @return {Promise<*>} | ||
*/ | ||
async evaluateAsync(expression, options = {}) { | ||
const contextId = options.useIsolation ? await this._getOrCreateIsolatedContextId() : undefined; | ||
async _evaluate(expression, options) { | ||
const contextId = | ||
brendankenny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
options.useIsolation ? await this._getOrCreateIsolatedContextId() : undefined; | ||
|
||
try { | ||
// `await` is not redundant here because we want to `catch` the async errors | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could drop this. if we like this, I'd follow up with a PR that changes all existing usages to this.