mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix: make exposeFunction work for frames (#1295)
This patch fixes page.exposeFunction method so that it adds exposed binding to all existing frames. Fixes #690
This commit is contained in:
parent
8c9332b62e
commit
3cb0f1af34
@ -289,8 +289,8 @@ class Page extends EventEmitter {
|
||||
this._pageBindings[name] = puppeteerFunction;
|
||||
|
||||
const expression = helper.evaluationString(addPageBinding, name);
|
||||
await this._client.send('Page.addScriptToEvaluateOnNewDocument', { source: expression });
|
||||
await this._client.send('Runtime.evaluate', { expression, returnByValue: true });
|
||||
await this._client.send('Page.addScriptToEvaluateOnNewDocument', {source: expression});
|
||||
await Promise.all(this.frames().map(frame => frame.evaluate(expression).catch(debugError)));
|
||||
|
||||
function addPageBinding(bindingName) {
|
||||
window[bindingName] = async(...args) => {
|
||||
@ -375,7 +375,7 @@ class Page extends EventEmitter {
|
||||
const {name, seq, args} = JSON.parse(event.args[1].value);
|
||||
const result = await this._pageBindings[name](...args);
|
||||
const expression = helper.evaluationString(deliverResult, name, seq, result);
|
||||
this._client.send('Runtime.evaluate', { expression }).catch(debugError);
|
||||
this._client.send('Runtime.evaluate', { expression, contextId: event.executionContextId }).catch(debugError);
|
||||
|
||||
function deliverResult(name, seq, result) {
|
||||
window[name]['callbacks'].get(seq)(result);
|
||||
|
24
test/test.js
24
test/test.js
@ -1188,6 +1188,30 @@ describe('Page', function() {
|
||||
});
|
||||
expect(result).toBe(15);
|
||||
}));
|
||||
it('should work on frames', SX(async function() {
|
||||
await page.exposeFunction('compute', function(a, b) {
|
||||
return Promise.resolve(a * b);
|
||||
});
|
||||
|
||||
await page.goto(PREFIX + '/frames/nested-frames.html');
|
||||
const frame = page.frames()[1];
|
||||
const result = await frame.evaluate(async function() {
|
||||
return await compute(3, 5);
|
||||
});
|
||||
expect(result).toBe(15);
|
||||
}));
|
||||
it('should work on frames before navigation', SX(async function() {
|
||||
await page.goto(PREFIX + '/frames/nested-frames.html');
|
||||
await page.exposeFunction('compute', function(a, b) {
|
||||
return Promise.resolve(a * b);
|
||||
});
|
||||
|
||||
const frame = page.frames()[1];
|
||||
const result = await frame.evaluate(async function() {
|
||||
return await compute(3, 5);
|
||||
});
|
||||
expect(result).toBe(15);
|
||||
}));
|
||||
});
|
||||
|
||||
describe('Page.setRequestInterception', function() {
|
||||
|
Loading…
Reference in New Issue
Block a user