From e991b0e20a3ba14bade5e031cdf586031bef4fad Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 12 May 2017 10:01:22 -0700 Subject: [PATCH] Implement puppeteer's Page.addScript --- lib/Page.js | 19 +++++++++++++++++++ phantomjs/WebPage.js | 10 +--------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/Page.js b/lib/Page.js index 5553cd16c50..1fb125348e8 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -62,6 +62,25 @@ class Page extends EventEmitter { client.on('Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails)); } + /** + * @param {string} url + * @return {!Promise} + */ + async addScript(url) { + return this.evaluateAsync(addScriptTag, url); + + /** + * @param {string} url + */ + function addScriptTag(url) { + var script = document.createElement('script'); + script.src = url; + var promise = new Promise(x => script.onload = x); + document.head.appendChild(script); + return promise; + } + } + /** * @param {string} name * @param {function(?)} callback diff --git a/phantomjs/WebPage.js b/phantomjs/WebPage.js index 2ea290a2311..63ddf09e5d6 100644 --- a/phantomjs/WebPage.js +++ b/phantomjs/WebPage.js @@ -84,15 +84,7 @@ class WebPage { * @param {function()} callback */ includeJs(url, callback) { - this._page.evaluateAsync(include, url).then(callback); - - function include(url) { - var script = document.createElement('script'); - script.src = url; - var promise = new Promise(x => script.onload = x); - document.head.appendChild(script); - return promise; - } + this._page.addScript(url).then(callback); } /**