mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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));
|
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
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user