mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix(frames): make sure evaluation does not hang in detached iframes (#3770)
Fix #3261
This commit is contained in:
@@ -424,6 +424,8 @@ class Frame {
|
||||
* @return {!Promise<!ExecutionContext>}
|
||||
*/
|
||||
executionContext() {
|
||||
if (this._detached)
|
||||
throw new Error(`Execution Context is not available in detached frame "${this.url()}" (are you trying to evaluate?)`);
|
||||
return this._contextPromise;
|
||||
}
|
||||
|
||||
@@ -433,7 +435,7 @@ class Frame {
|
||||
* @return {!Promise<!Puppeteer.JSHandle>}
|
||||
*/
|
||||
async evaluateHandle(pageFunction, ...args) {
|
||||
const context = await this._contextPromise;
|
||||
const context = await this.executionContext();
|
||||
return context.evaluateHandle(pageFunction, ...args);
|
||||
}
|
||||
|
||||
@@ -443,7 +445,7 @@ class Frame {
|
||||
* @return {!Promise<*>}
|
||||
*/
|
||||
async evaluate(pageFunction, ...args) {
|
||||
const context = await this._contextPromise;
|
||||
const context = await this.executionContext();
|
||||
return context.evaluate(pageFunction, ...args);
|
||||
}
|
||||
|
||||
@@ -463,7 +465,7 @@ class Frame {
|
||||
async _document() {
|
||||
if (this._documentPromise)
|
||||
return this._documentPromise;
|
||||
this._documentPromise = this._contextPromise.then(async context => {
|
||||
this._documentPromise = this.executionContext().then(async context => {
|
||||
const document = await context.evaluateHandle('document');
|
||||
return document.asElement();
|
||||
});
|
||||
@@ -601,7 +603,7 @@ class Frame {
|
||||
} = options;
|
||||
if (url !== null) {
|
||||
try {
|
||||
const context = await this._contextPromise;
|
||||
const context = await this.executionContext();
|
||||
return (await context.evaluateHandle(addScriptUrl, url, type)).asElement();
|
||||
} catch (error) {
|
||||
throw new Error(`Loading script from ${url} failed`);
|
||||
@@ -611,12 +613,12 @@ class Frame {
|
||||
if (path !== null) {
|
||||
let contents = await readFileAsync(path, 'utf8');
|
||||
contents += '//# sourceURL=' + path.replace(/\n/g, '');
|
||||
const context = await this._contextPromise;
|
||||
const context = await this.executionContext();
|
||||
return (await context.evaluateHandle(addScriptContent, contents, type)).asElement();
|
||||
}
|
||||
|
||||
if (content !== null) {
|
||||
const context = await this._contextPromise;
|
||||
const context = await this.executionContext();
|
||||
return (await context.evaluateHandle(addScriptContent, content, type)).asElement();
|
||||
}
|
||||
|
||||
@@ -671,7 +673,7 @@ class Frame {
|
||||
} = options;
|
||||
if (url !== null) {
|
||||
try {
|
||||
const context = await this._contextPromise;
|
||||
const context = await this.executionContext();
|
||||
return (await context.evaluateHandle(addStyleUrl, url)).asElement();
|
||||
} catch (error) {
|
||||
throw new Error(`Loading style from ${url} failed`);
|
||||
@@ -681,12 +683,12 @@ class Frame {
|
||||
if (path !== null) {
|
||||
let contents = await readFileAsync(path, 'utf8');
|
||||
contents += '/*# sourceURL=' + path.replace(/\n/g, '') + '*/';
|
||||
const context = await this._contextPromise;
|
||||
const context = await this.executionContext();
|
||||
return (await context.evaluateHandle(addStyleContent, contents)).asElement();
|
||||
}
|
||||
|
||||
if (content !== null) {
|
||||
const context = await this._contextPromise;
|
||||
const context = await this.executionContext();
|
||||
return (await context.evaluateHandle(addStyleContent, content)).asElement();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user