Implement puppeteer's Page.addScript
This commit is contained in:
parent
2cda8c18d1
commit
e991b0e20a
19
lib/Page.js
19
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
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user