mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix: allow user gesture restricted code to be run in page.evaluate (#2503)
Fixes #2502
This commit is contained in:
parent
1db4986d12
commit
1d225cfa17
@ -64,7 +64,13 @@ class ExecutionContext {
|
||||
if (helper.isString(pageFunction)) {
|
||||
const contextId = this._contextId;
|
||||
const expression = /** @type {string} */ (pageFunction);
|
||||
const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', { expression, contextId, returnByValue: false, awaitPromise: true});
|
||||
const { exceptionDetails, result: remoteObject } = await this._client.send('Runtime.evaluate', {
|
||||
expression,
|
||||
contextId,
|
||||
returnByValue: false,
|
||||
awaitPromise: true,
|
||||
userGesture: true
|
||||
});
|
||||
if (exceptionDetails)
|
||||
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
|
||||
return this._objectHandleFactory(remoteObject);
|
||||
@ -75,7 +81,8 @@ class ExecutionContext {
|
||||
executionContextId: this._contextId,
|
||||
arguments: args.map(convertArgument.bind(this)),
|
||||
returnByValue: false,
|
||||
awaitPromise: true
|
||||
awaitPromise: true,
|
||||
userGesture: true
|
||||
});
|
||||
if (exceptionDetails)
|
||||
throw new Error('Evaluation failed: ' + helper.getExceptionMessage(exceptionDetails));
|
||||
|
@ -196,6 +196,18 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
|
||||
const isFive = await page.evaluate(e => Object.is(e, 5), aHandle);
|
||||
expect(isFive).toBeTruthy();
|
||||
});
|
||||
it('should simulate a user gesture', async({page, server}) => {
|
||||
await page.evaluate(playAudio);
|
||||
// also test evaluating strings
|
||||
await page.evaluate(`(${playAudio})()`);
|
||||
|
||||
function playAudio() {
|
||||
const audio = document.createElement('audio');
|
||||
audio.src = 'data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEARKwAAIhYAQACABAAZGF0YQAAAAA=';
|
||||
// This returns a promise which throws if it was not triggered by a user gesture.
|
||||
return audio.play();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('Page.setOfflineMode', function() {
|
||||
|
Loading…
Reference in New Issue
Block a user