From b20cde67c63a6344321c2fc40ef2d3cbf8c3ae24 Mon Sep 17 00:00:00 2001 From: Aleksey Date: Mon, 18 Jun 2018 13:41:03 -0700 Subject: [PATCH] fix(page): migrate exposeFunction from console.debug to Runtime.installBinding #2631 New way is faster and cleaner. --- lib/Page.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/Page.js b/lib/Page.js index c507c03a..5663097c 100644 --- a/lib/Page.js +++ b/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} args