From 5d6d3e0a816eeb2c48da270fd8542bcc2790595c Mon Sep 17 00:00:00 2001 From: Eric Bidelman Date: Mon, 21 Aug 2017 16:32:39 -0700 Subject: [PATCH] cleanup: Use ES6 default params (#447) --- lib/Input.js | 22 +++++++++------------- lib/Launcher.js | 27 +++++++++++++-------------- lib/Page.js | 7 ++----- phantom_shim/WebPage.js | 3 +-- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/lib/Input.js b/lib/Input.js index 9050052b..e07d2b4c 100644 --- a/lib/Input.js +++ b/lib/Input.js @@ -31,17 +31,17 @@ class Keyboard { * @param {{text: (string|undefined)}} options * @return {!Promise} */ - async down(key, options) { - let {text} = options || {}; - let autoRepeat = this._pressedKeys.has(key); + async down(key, options = {}) { + const text = options.text; + const autoRepeat = this._pressedKeys.has(key); this._pressedKeys.add(key); this._modifiers |= this._modifierBit(key); await this._client.send('Input.dispatchKeyEvent', { type: text ? 'keyDown' : 'rawKeyDown', modifiers: this._modifiers, windowsVirtualKeyCode: codeForKey(key), - key: key, - text: text, + key, + text, unmodifiedText: text, autoRepeat }); @@ -127,10 +127,10 @@ class Mouse { * @param {number} y * @param {!Object=} options */ - async click(x, y, options) { + async click(x, y, options = {}) { this.move(x, y); this.down(options); - if (options && options.delay) + if (typeof options.delay === 'number') await new Promise(f => setTimeout(f, options.delay)); await this.up(options); } @@ -138,9 +138,7 @@ class Mouse { /** * @param {!Object=} options */ - async down(options) { - if (!options) - options = {}; + async down(options = {}) { this._button = (options.button || 'left'); await this._client.send('Input.dispatchMouseEvent', { type: 'mousePressed', @@ -155,9 +153,7 @@ class Mouse { /** * @param {!Object=} options */ - async up(options) { - if (!options) - options = {}; + async up(options = {}) { this._button = 'none'; await this._client.send('Input.dispatchMouseEvent', { type: 'mouseReleased', diff --git a/lib/Launcher.js b/lib/Launcher.js index e35e9b31..eea366ee 100644 --- a/lib/Launcher.js +++ b/lib/Launcher.js @@ -47,55 +47,54 @@ const DEFAULT_ARGS = [ class Launcher { /** - * @param {!Object} options + * @param {!Object=} options * @return {!Promise} */ - static async launch(options) { - options = options || {}; - let userDataDir = [ + static async launch(options = {}) { + const userDataDir = [ CHROME_PROFILE_PATH, process.pid, ++browserId, crypto.randomBytes(8 / 2).toString('hex') // add random salt 8 characters long. ].join('-'); - let chromeArguments = DEFAULT_ARGS.concat([ + const chromeArguments = DEFAULT_ARGS.concat([ `--user-data-dir=${userDataDir}`, ]); if (typeof options.headless !== 'boolean' || options.headless) { chromeArguments.push( - `--headless`, - `--disable-gpu`, - `--hide-scrollbars`, + '--headless', + '--disable-gpu', + '--hide-scrollbars', '--mute-audio' ); } let chromeExecutable = options.executablePath; if (typeof chromeExecutable !== 'string') { - let revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), ChromiumRevision); + const revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), ChromiumRevision); console.assert(revisionInfo.downloaded, `Chromium revision is not downloaded. Run "npm install"`); chromeExecutable = revisionInfo.executablePath; } if (Array.isArray(options.args)) chromeArguments.push(...options.args); - let chromeProcess = childProcess.spawn(chromeExecutable, chromeArguments, {}); + const chromeProcess = childProcess.spawn(chromeExecutable, chromeArguments, {}); if (options.dumpio) { chromeProcess.stdout.pipe(process.stdout); chromeProcess.stderr.pipe(process.stderr); } // Cleanup as processes exit. - let listeners = [ + const listeners = [ helper.addEventListener(process, 'exit', killChromeAndCleanup), helper.addEventListener(chromeProcess, 'exit', killChromeAndCleanup), ]; if (options.handleSIGINT !== false) listeners.push(helper.addEventListener(process, 'SIGINT', killChromeAndCleanup)); try { - let connectionDelay = options.slowMo || 0; - let browserWSEndpoint = await waitForWSEndpoint(chromeProcess, options.timeout || 30 * 1000); - let connection = await Connection.create(browserWSEndpoint, connectionDelay); + const connectionDelay = options.slowMo || 0; + const browserWSEndpoint = await waitForWSEndpoint(chromeProcess, options.timeout || 30 * 1000); + const connection = await Connection.create(browserWSEndpoint, connectionDelay); return new Browser(connection, !!options.ignoreHTTPSErrors, killChromeAndCleanup); } catch (e) { killChromeAndCleanup(); diff --git a/lib/Page.js b/lib/Page.js index fd30b0e9..0fa835eb 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -427,8 +427,7 @@ class Page extends EventEmitter { * @param {!Object=} options * @return {!Promise} */ - async screenshot(options) { - options = options || {}; + async screenshot(options = {}) { let screenshotType = null; if (options.path) { let mimeType = mime.lookup(options.path); @@ -506,9 +505,7 @@ class Page extends EventEmitter { * @param {!Object=} options * @return {!Promise} */ - async pdf(options) { - options = options || {}; - + async pdf(options = {}) { let scale = options.scale || 1; let displayHeaderFooter = !!options.displayHeaderFooter; let printBackground = !!options.printBackground; diff --git a/phantom_shim/WebPage.js b/phantom_shim/WebPage.js index 0619b70e..a5213d83 100644 --- a/phantom_shim/WebPage.js +++ b/phantom_shim/WebPage.js @@ -28,11 +28,10 @@ class WebPage { * @param {string} scriptPath * @param {!Object=} options */ - constructor(browser, scriptPath, options) { + constructor(browser, scriptPath, options = {}) { this._page = await(browser.newPage()); this.settings = new WebPageSettings(this._page); - options = options || {}; options.settings = options.settings || {}; if (options.settings.userAgent) this.settings.userAgent = options.settings.userAgent;