mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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
|
* @param {string} url
|
||||||
* @return {!Promise}
|
* @return {!Promise}
|
||||||
*/
|
*/
|
||||||
async addScript(url) {
|
async addScriptTag(url) {
|
||||||
return this.evaluateAsync(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 {string} name
|
||||||
* @param {function(?)} callback
|
* @param {function(?)} callback
|
||||||
|
@ -84,7 +84,7 @@ class WebPage {
|
|||||||
* @param {function()} callback
|
* @param {function()} callback
|
||||||
*/
|
*/
|
||||||
includeJs(url, 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);
|
filePath = path.resolve(this.libraryPath, filePath);
|
||||||
if (!fs.existsSync(filePath))
|
if (!fs.existsSync(filePath))
|
||||||
return;
|
return;
|
||||||
var code = fs.readFileSync(filePath, 'utf8');
|
await(this._page.injectFile(filePath));
|
||||||
this.evaluate(code);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user