mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(ExecutionContext): introduce ExecutionContext.frame() (#1972)
This patch introduces ExecutionContext.frame() that returns Frame associated with this Execution Context. This allows to associate console messages with the originating frame, if any.
This commit is contained in:
@@ -21,17 +21,22 @@ class ExecutionContext {
|
||||
* @param {!Puppeteer.CDPSession} client
|
||||
* @param {!Object} contextPayload
|
||||
* @param {function(*):!JSHandle} objectHandleFactory
|
||||
* @param {?Puppeteer.Frame} frame
|
||||
*/
|
||||
constructor(client, contextPayload, objectHandleFactory) {
|
||||
constructor(client, contextPayload, objectHandleFactory, frame) {
|
||||
this._client = client;
|
||||
this._frame = frame;
|
||||
this._contextId = contextPayload.id;
|
||||
|
||||
const auxData = contextPayload.auxData || {isDefault: true};
|
||||
this._frameId = auxData.frameId || null;
|
||||
this._isDefault = !!auxData.isDefault;
|
||||
this._objectHandleFactory = objectHandleFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {?Puppeteer.Frame}
|
||||
*/
|
||||
frame() {
|
||||
return this._frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Function|string} pageFunction
|
||||
* @param {...*} args
|
||||
|
||||
@@ -154,11 +154,11 @@ class FrameManager extends EventEmitter {
|
||||
}
|
||||
|
||||
_onExecutionContextCreated(contextPayload) {
|
||||
const context = new ExecutionContext(this._client, contextPayload, this.createJSHandle.bind(this, contextPayload.id));
|
||||
const frameId = contextPayload.auxData && contextPayload.auxData.isDefault ? contextPayload.auxData.frameId : null;
|
||||
const frame = frameId ? this._frames.get(frameId) : null;
|
||||
const context = new ExecutionContext(this._client, contextPayload, this.createJSHandle.bind(this, contextPayload.id), frame);
|
||||
this._contextIdToContext.set(contextPayload.id, context);
|
||||
|
||||
const frame = context._frameId ? this._frames.get(context._frameId) : null;
|
||||
if (frame && context._isDefault)
|
||||
if (frame)
|
||||
frame._setDefaultContext(context);
|
||||
}
|
||||
|
||||
@@ -166,9 +166,8 @@ class FrameManager extends EventEmitter {
|
||||
* @param {!ExecutionContext} context
|
||||
*/
|
||||
_removeContext(context) {
|
||||
const frame = context._frameId ? this._frames.get(context._frameId) : null;
|
||||
if (frame && context._isDefault)
|
||||
frame._setDefaultContext(null);
|
||||
if (context.frame())
|
||||
context.frame()._setDefaultContext(null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user