mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
fix(page): migrate exposeFunction from console.debug to Runtime.installBinding #2631
New way is faster and cleaner.
This commit is contained in:
parent
af0bd15d88
commit
b20cde67c6
33
lib/Page.js
33
lib/Page.js
@ -127,6 +127,7 @@ class Page extends EventEmitter {
|
|||||||
client.on('Page.domContentEventFired', event => this.emit(Page.Events.DOMContentLoaded));
|
client.on('Page.domContentEventFired', event => this.emit(Page.Events.DOMContentLoaded));
|
||||||
client.on('Page.loadEventFired', event => this.emit(Page.Events.Load));
|
client.on('Page.loadEventFired', event => this.emit(Page.Events.Load));
|
||||||
client.on('Runtime.consoleAPICalled', event => this._onConsoleAPI(event));
|
client.on('Runtime.consoleAPICalled', event => this._onConsoleAPI(event));
|
||||||
|
client.on('Runtime.bindingCalled', event => this._onBindingCalled(event));
|
||||||
client.on('Page.javascriptDialogOpening', event => this._onDialog(event));
|
client.on('Page.javascriptDialogOpening', event => this._onDialog(event));
|
||||||
client.on('Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails));
|
client.on('Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails));
|
||||||
client.on('Security.certificateError', event => this._onCertificateError(event));
|
client.on('Security.certificateError', event => this._onCertificateError(event));
|
||||||
@ -387,10 +388,12 @@ class Page extends EventEmitter {
|
|||||||
this._pageBindings[name] = puppeteerFunction;
|
this._pageBindings[name] = puppeteerFunction;
|
||||||
|
|
||||||
const expression = helper.evaluationString(addPageBinding, name);
|
const expression = helper.evaluationString(addPageBinding, name);
|
||||||
|
await this._client.send('Runtime.addBinding', {name: name});
|
||||||
await this._client.send('Page.addScriptToEvaluateOnNewDocument', {source: expression});
|
await this._client.send('Page.addScriptToEvaluateOnNewDocument', {source: expression});
|
||||||
await Promise.all(this.frames().map(frame => frame.evaluate(expression).catch(debugError)));
|
await Promise.all(this.frames().map(frame => frame.evaluate(expression).catch(debugError)));
|
||||||
|
|
||||||
function addPageBinding(bindingName) {
|
function addPageBinding(bindingName) {
|
||||||
|
const binding = window[bindingName];
|
||||||
window[bindingName] = async(...args) => {
|
window[bindingName] = async(...args) => {
|
||||||
const me = window[bindingName];
|
const me = window[bindingName];
|
||||||
let callbacks = me['callbacks'];
|
let callbacks = me['callbacks'];
|
||||||
@ -401,8 +404,7 @@ class Page extends EventEmitter {
|
|||||||
const seq = (me['lastSeq'] || 0) + 1;
|
const seq = (me['lastSeq'] || 0) + 1;
|
||||||
me['lastSeq'] = seq;
|
me['lastSeq'] = seq;
|
||||||
const promise = new Promise(fulfill => callbacks.set(seq, fulfill));
|
const promise = new Promise(fulfill => callbacks.set(seq, fulfill));
|
||||||
// eslint-disable-next-line no-console
|
binding(JSON.stringify({name: bindingName, seq, args}));
|
||||||
console.debug('driver:page-binding', JSON.stringify({name: bindingName, seq, args}));
|
|
||||||
return promise;
|
return promise;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -474,22 +476,25 @@ class Page extends EventEmitter {
|
|||||||
* @param {!Protocol.Runtime.consoleAPICalledPayload} event
|
* @param {!Protocol.Runtime.consoleAPICalledPayload} event
|
||||||
*/
|
*/
|
||||||
async _onConsoleAPI(event) {
|
async _onConsoleAPI(event) {
|
||||||
if (event.type === 'debug' && event.args.length && event.args[0].value === 'driver:page-binding') {
|
|
||||||
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, contextId: event.executionContextId }).catch(debugError);
|
|
||||||
|
|
||||||
function deliverResult(name, seq, result) {
|
|
||||||
window[name]['callbacks'].get(seq)(result);
|
|
||||||
window[name]['callbacks'].delete(seq);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const values = event.args.map(arg => this._frameManager.createJSHandle(event.executionContextId, arg));
|
const values = event.args.map(arg => this._frameManager.createJSHandle(event.executionContextId, arg));
|
||||||
this._addConsoleMessage(event.type, values);
|
this._addConsoleMessage(event.type, values);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {!Protocol.Runtime.bindingCalledPayload} event
|
||||||
|
*/
|
||||||
|
async _onBindingCalled(event) {
|
||||||
|
const {name, seq, args} = JSON.parse(event.payload);
|
||||||
|
const result = await this._pageBindings[name](...args);
|
||||||
|
const expression = helper.evaluationString(deliverResult, name, seq, result);
|
||||||
|
this._client.send('Runtime.evaluate', { expression, contextId: event.executionContextId }).catch(debugError);
|
||||||
|
|
||||||
|
function deliverResult(name, seq, result) {
|
||||||
|
window[name]['callbacks'].get(seq)(result);
|
||||||
|
window[name]['callbacks'].delete(seq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} type
|
* @param {string} type
|
||||||
* @param {!Array<!Puppeteer.JSHandle>} args
|
* @param {!Array<!Puppeteer.JSHandle>} args
|
||||||
|
Loading…
Reference in New Issue
Block a user