Implement puppeteer's Page.addScript

This commit is contained in:
Andrey Lushnikov 2017-05-12 10:01:22 -07:00
parent 2cda8c18d1
commit e991b0e20a
2 changed files with 20 additions and 9 deletions

View File

@ -62,6 +62,25 @@ class Page extends EventEmitter {
client.on('Runtime.exceptionThrown', exception => this._handleException(exception.exceptionDetails)); 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 {string} name
* @param {function(?)} callback * @param {function(?)} callback

View File

@ -84,15 +84,7 @@ class WebPage {
* @param {function()} callback * @param {function()} callback
*/ */
includeJs(url, callback) { includeJs(url, callback) {
this._page.evaluateAsync(include, url).then(callback); this._page.addScript(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;
}
} }
/** /**