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.loadEventFired', event => this.emit(Page.Events.Load));
|
||||
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('Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails));
|
||||
client.on('Security.certificateError', event => this._onCertificateError(event));
|
||||
@ -387,10 +388,12 @@ class Page extends EventEmitter {
|
||||
this._pageBindings[name] = puppeteerFunction;
|
||||
|
||||
const expression = helper.evaluationString(addPageBinding, name);
|
||||
await this._client.send('Runtime.addBinding', {name: name});
|
||||
await this._client.send('Page.addScriptToEvaluateOnNewDocument', {source: expression});
|
||||
await Promise.all(this.frames().map(frame => frame.evaluate(expression).catch(debugError)));
|
||||
|
||||
function addPageBinding(bindingName) {
|
||||
const binding = window[bindingName];
|
||||
window[bindingName] = async(...args) => {
|
||||
const me = window[bindingName];
|
||||
let callbacks = me['callbacks'];
|
||||
@ -401,8 +404,7 @@ class Page extends EventEmitter {
|
||||
const seq = (me['lastSeq'] || 0) + 1;
|
||||
me['lastSeq'] = seq;
|
||||
const promise = new Promise(fulfill => callbacks.set(seq, fulfill));
|
||||
// eslint-disable-next-line no-console
|
||||
console.debug('driver:page-binding', JSON.stringify({name: bindingName, seq, args}));
|
||||
binding(JSON.stringify({name: bindingName, seq, args}));
|
||||
return promise;
|
||||
};
|
||||
}
|
||||
@ -474,22 +476,25 @@ class Page extends EventEmitter {
|
||||
* @param {!Protocol.Runtime.consoleAPICalledPayload} 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));
|
||||
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 {!Array<!Puppeteer.JSHandle>} args
|
||||
|
Loading…
Reference in New Issue
Block a user