Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(firefox): implement execution contexts (#3962)
- Loading branch information
1 parent
5696096
commit a987535
Showing
6 changed files
with
110 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const {helper, assert, debugError} = require('./helper'); | ||
const {JSHandle, createHandle} = require('./JSHandle'); | ||
|
||
class ExecutionContext { | ||
/** | ||
* @param {!PageSession} session | ||
* @param {?Frame} frame | ||
* @param {string} executionContextId | ||
*/ | ||
constructor(session, frame, executionContextId) { | ||
this._session = session; | ||
this._frame = frame; | ||
this._executionContextId = executionContextId; | ||
} | ||
|
||
async evaluateHandle(pageFunction, ...args) { | ||
if (helper.isString(pageFunction)) { | ||
const payload = await this._session.send('Page.evaluate', { | ||
script: pageFunction, | ||
executionContextId: this._executionContextId, | ||
}); | ||
return createHandle(this, payload.result, payload.exceptionDetails); | ||
} | ||
args = args.map(arg => { | ||
if (arg instanceof JSHandle) | ||
return arg._protocolValue; | ||
if (Object.is(arg, Infinity)) | ||
return {unserializableValue: 'Infinity'}; | ||
if (Object.is(arg, -Infinity)) | ||
return {unserializableValue: '-Infinity'}; | ||
if (Object.is(arg, -0)) | ||
return {unserializableValue: '-0'}; | ||
if (Object.is(arg, NaN)) | ||
return {unserializableValue: 'NaN'}; | ||
return {value: arg}; | ||
}); | ||
const payload = await this._session.send('Page.evaluate', { | ||
functionText: pageFunction.toString(), | ||
args, | ||
executionContextId: this._executionContextId | ||
}); | ||
return createHandle(this, payload.result, payload.exceptionDetails); | ||
} | ||
|
||
frame() { | ||
return this._frame; | ||
} | ||
|
||
async evaluate(pageFunction, ...args) { | ||
try { | ||
const handle = await this.evaluateHandle(pageFunction, ...args); | ||
const result = await handle.jsonValue(); | ||
await handle.dispose(); | ||
return result; | ||
} catch (e) { | ||
if (e.message.includes('cyclic object value') || e.message.includes('Object is not serializable')) | ||
return undefined; | ||
throw e; | ||
} | ||
} | ||
|
||
} | ||
|
||
module.exports = {ExecutionContext}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters