feat(Frame): Support options in addScriptTag and addStyleTag (#996)

This patch:
- deprecates injectFile as it was confused with the addScriptTag
- accepts an options object in addScriptTag which supports properties url, path and content.
- accepts an options object in addStyleTag which supports properties url, path and content.

Fixes #949.

BREAKING CHANGE:
- the addStyleTag/addScriptTag have changed;
- the injectFile was removed in favor of (addStyleTag({path:}).
This commit is contained in:
Sam Verschueren
2017-10-12 10:26:44 +02:00
committed by Andrey Lushnikov
parent 0426e3c068
commit c310f139a0
4 changed files with 161 additions and 88 deletions

View File

@@ -20,6 +20,8 @@ const {helper} = require('./helper');
const {ExecutionContext, JSHandle} = require('./ExecutionContext');
const ElementHandle = require('./ElementHandle');
const readFileAsync = helper.promisify(fs.readFile);
class FrameManager extends EventEmitter {
/**
* @param {!Puppeteer.Session} client
@@ -303,48 +305,71 @@ class Frame {
}
/**
* @param {string} filePath
* @return {!Promise<*>}
* @param {Object} options
* @return {!Promise}
*/
async injectFile(filePath) {
let contents = await new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) return reject(err);
resolve(data);
});
});
contents += `//# sourceURL=` + filePath.replace(/\n/g,'');
return this.evaluate(contents);
}
async addScriptTag(options) {
if (typeof options.url === 'string')
return this.evaluate(addScriptUrl, options.url);
/**
* @param {string} url
*/
async addScriptTag(url) {
return this.evaluate(addScriptTag, url);
if (typeof options.path === 'string') {
let contents = await readFileAsync(options.path, 'utf8');
contents += '//# sourceURL=' + options.path.replace(/\n/g, '');
return this.evaluate(addScriptContent, contents);
}
if (typeof options.content === 'string')
return this.evaluate(addScriptContent, options.content);
throw new Error('Provide an object with a `url`, `path` or `content` property');
/**
* @param {string} url
*/
function addScriptTag(url) {
function addScriptUrl(url) {
const script = document.createElement('script');
script.src = url;
const promise = new Promise(x => script.onload = x);
document.head.appendChild(script);
return promise;
}
/**
* @param {string} content
*/
function addScriptContent(content) {
const script = document.createElement('script');
script.type = 'text/javascript';
script.text = content;
document.head.appendChild(script);
}
}
/**
* @param {string} url
* @param {Object} options
* @return {!Promise}
*/
async addStyleTag(url) {
return this.evaluate(addStyleTag, url);
async addStyleTag(options) {
if (typeof options.url === 'string')
return this.evaluate(addStyleUrl, options.url);
if (typeof options.path === 'string') {
let contents = await readFileAsync(options.path, 'utf8');
contents += '/*# sourceURL=' + options.path.replace(/\n/g, '') + '*/';
return this.evaluate(addStyleContent, contents);
}
if (typeof options.content === 'string')
return this.evaluate(addStyleContent, options.content);
throw new Error('Provide an object with a `url`, `path` or `content` property');
/**
* @param {string} url
*/
function addStyleTag(url) {
function addStyleUrl(url) {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
@@ -352,6 +377,16 @@ class Frame {
document.head.appendChild(link);
return promise;
}
/**
* @param {string} content
*/
function addStyleContent(content) {
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(content));
document.head.appendChild(style);
}
}
/**

View File

@@ -254,24 +254,17 @@ class Page extends EventEmitter {
}
/**
* @param {string} url
* @param {Object} options
*/
async addScriptTag(url) {
return this.mainFrame().addScriptTag(url);
async addScriptTag(options) {
return this.mainFrame().addScriptTag(options);
}
/**
* @param {string} url
* @param {Object} options
*/
async addStyleTag(url) {
return this.mainFrame().addStyleTag(url);
}
/**
* @param {string} filePath
*/
async injectFile(filePath) {
return this.mainFrame().injectFile(filePath);
async addStyleTag(options) {
return this.mainFrame().addStyleTag(options);
}
/**