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:
parent
e991b0e20a
commit
2de672ed1b
11
lib/Page.js
11
lib/Page.js
@ -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
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user