Skip to content

Commit 1d225cf

Browse files
JoelEinbinderaslushnikov
authored andcommitted
fix: allow user gesture restricted code to be run in page.evaluate (#2503)
Fixes #2502
1 parent 1db4986 commit 1d225cf

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

lib/ExecutionContext.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ class ExecutionContext {
6464
if (helper.isString(pageFunction)) {
6565
const contextId = this._contextId;
6666
const expression = /** @type {string} */ (pageFunction);
67-
const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true});
67+
const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', {
68+
expression,
69+
contextId,
70+
returnByValue: false,
71+
awaitPromise: true,
72+
userGesture: true
73+
});
6874
if (exceptionDetails)
6975
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
7076
return this._objectHandleFactory(remoteObject);
@@ -75,7 +81,8 @@ class ExecutionContext {
7581
executionContextId: this._contextId,
7682
arguments: args.map(convertArgument.bind(this)),
7783
returnByValue: false,
78-
awaitPromise: true
84+
awaitPromise: true,
85+
userGesture: true
7986
});
8087
if (exceptionDetails)
8188
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));

test/page.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
196196
const isFive = await page.evaluate(e => Object.is(e, 5), aHandle);
197197
expect(isFive).toBeTruthy();
198198
});
199+
it('should simulate a user gesture', async({page, server}) => {
200+
await page.evaluate(playAudio);
201+
// also test evaluating strings
202+
await page.evaluate(`(${playAudio})()`);
203+
204+
function playAudio() {
205+
const audio = document.createElement('audio');
206+
audio.src = 'data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQAAAAA=';
207+
// This returns a promise which throws if it was not triggered by a user gesture.
208+
return audio.play();
209+
}
210+
});
199211
});
200212

201213
describe('Page.setOfflineMode', function() {

0 commit comments

Comments
 (0)