From 9ad4938fcb3e495663591339ca5ee34dfa0c2420 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Mon, 19 Jun 2017 16:39:41 -0700 Subject: [PATCH] Inline helper evaluate functions --- lib/Page.js | 15 ++++++------- lib/helpers.js | 57 +------------------------------------------------- 2 files changed, 9 insertions(+), 63 deletions(-) diff --git a/lib/Page.js b/lib/Page.js index 3f5220d1bfa..0b1233e9f03 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -33,8 +33,9 @@ class Page extends EventEmitter { client.send('Runtime.enable', {}), client.send('Security.enable', {}), ]); - var screenDPI = await helpers.evaluate(client, () => window.devicePixelRatio, []); - var page = new Page(client, screenDPI.result.value); + var expression = helpers.evaluationString(() => window.devicePixelRatio, []); + var {result:{value: screenDPI}} = await client.send('Runtime.evaluate', { expression, returnByValue: true }); + var page = new Page(client, screenDPI); // Initialize default page size. await page.setViewportSize({width: 400, height: 300}); return page; @@ -108,8 +109,8 @@ class Page extends EventEmitter { * @return {!Promise} */ async injectFile(filePath) { - var code = fs.readFileSync(filePath, 'utf8'); - await helpers.evaluateText(this._client, code, false /* awaitPromise */); + var expression = fs.readFileSync(filePath, 'utf8'); + await this._client.send('Runtime.evaluate', { expression, returnByValue: true }); } /** @@ -125,11 +126,11 @@ class Page extends EventEmitter { var sourceURL = '__in_page_callback__' + name; this._sourceURLToPageCallback.set(sourceURL, new InPageCallback(name, callback)); - var text = helpers.evaluationString(inPageCallback, [name], false /* wrapInPromise */, sourceURL); + var expression = helpers.evaluationString(inPageCallback, [name], false /* wrapInPromise */, sourceURL); await Promise.all([ this._client.send('Debugger.enable', {}), - this._client.send('Page.addScriptToEvaluateOnLoad', { scriptSource: text }), - helpers.evaluateText(this._client, text, false /* awaitPromise */) + this._client.send('Page.addScriptToEvaluateOnLoad', { scriptSource: expression }), + this._client.send('Runtime.evaluate', { expression, returnByValue: true }) ]); function inPageCallback(callbackName) { diff --git a/lib/helpers.js b/lib/helpers.js index b3419ab668c..ddd764db8d8 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -15,66 +15,11 @@ */ var helpers = module.exports = { - /** - * @param {!Connection} client - * @param {string} url - * @return {!Promise} - */ - waitForScriptWithURL: function(client, url) { - var fulfill; - var promise = new Promise(x => fulfill = x); - client.on('Debugger.scriptParsed', onScriptParsed); - client.on('Debugger.scriptFailedToParse', onScriptFailedToParse); - return promise; - - function onScriptParsed(event) { - if (event.url !== url) - return; - client.removeListener('Debugger.scriptParsed', onScriptParsed); - client.removeListener('Debugger.scriptFailedToParse', onScriptFailedToParse); - fulfill(event); - } - - function onScriptFailedToParse(event) { - if (event.url !== url) - return; - client.removeListener('Debugger.scriptParsed', onScriptParsed); - client.removeListener('Debugger.scriptFailedToParse', onScriptFailedToParse); - fulfill(null); - } - }, - - /** - * @param {!Connection} client - * @param {function()} fun - * @param {!Array<*>} args - * @param {boolean} awaitPromise - * @param {string=} sourceURL - * @return {!Promise} - */ - evaluate: function(client, fun, args, awaitPromise, sourceURL) { - var code = helpers.evaluationString(fun, args, awaitPromise, sourceURL); - return helpers.evaluateText(client, code, awaitPromise); - }, - - /** - * @param {!Connection} client - * @param {string} text - * @param {boolean} awaitPromise - * @return {!Promise} - */ - evaluateText: function(client, text, awaitPromise) { - return client.send('Runtime.evaluate', { - expression: text, - awaitPromise: awaitPromise, - returnByValue: true - }); - }, /** * @param {function()} fun * @param {!Array<*>} args - * @param {boolean} wrapInPromise + * @param {boolean=} wrapInPromise * @param {string=} sourceURL * @return {string} */