Introduce Page.injectFile method

With this patch, page has two methods to include javascript:
- Page.addScriptTag(url) which is similar to phantom's includeJs.
- Page.injectFile(filePath) which is similar to phantom's injectJs.
This commit is contained in:
Andrey Lushnikov 2017-05-12 10:19:01 -07:00
parent e991b0e20a
commit 2de672ed1b
2 changed files with 12 additions and 4 deletions

View File

@ -66,7 +66,7 @@ class Page extends EventEmitter {
* @param {string} url
* @return {!Promise}
*/
async addScript(url) {
async addScriptTag(url) {
return this.evaluateAsync(addScriptTag, url);
/**
@ -81,6 +81,15 @@ class Page extends EventEmitter {
}
}
/**
* @param {string} filePath
* @return {!Promise}
*/
async injectFile(filePath) {
var scriptContent = fs.readFileSync(filePath, 'utf8');
await this.evaluate(scriptContent);
}
/**
* @param {string} name
* @param {function(?)} callback

View File

@ -84,7 +84,7 @@ class WebPage {
* @param {function()} callback
*/
includeJs(url, callback) {
this._page.addScript(url).then(callback);
this._page.addScriptTag(url).then(callback);
}
/**
@ -116,8 +116,7 @@ class WebPage {
filePath = path.resolve(this.libraryPath, filePath);
if (!fs.existsSync(filePath))
return;
var code = fs.readFileSync(filePath, 'utf8');
this.evaluate(code);
await(this._page.injectFile(filePath));
}
/**