mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(Frame): addStyleTag and addScriptTag now return ElementHandles.
This patch teaches `page.addStyleTag` and `page.addScriptTag` to return ElementHandles to the added tags. Fixes #1179.
This commit is contained in:
committed by
Andrey Lushnikov
parent
f08f33458f
commit
e0f5b93923
@@ -323,86 +323,92 @@ class Frame {
|
||||
|
||||
/**
|
||||
* @param {Object} options
|
||||
* @return {!Promise}
|
||||
* @return {!Promise<!ElementHandle>}
|
||||
*/
|
||||
async addScriptTag(options) {
|
||||
if (typeof options.url === 'string')
|
||||
return this.evaluate(addScriptUrl, options.url);
|
||||
return this._context.evaluateHandle(addScriptUrl, options.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);
|
||||
return this._context.evaluateHandle(addScriptContent, contents);
|
||||
}
|
||||
|
||||
if (typeof options.content === 'string')
|
||||
return this.evaluate(addScriptContent, options.content);
|
||||
return this._context.evaluateHandle(addScriptContent, options.content);
|
||||
|
||||
throw new Error('Provide an object with a `url`, `path` or `content` property');
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @return {!Promise<!HTMLElement>}
|
||||
*/
|
||||
function addScriptUrl(url) {
|
||||
async 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;
|
||||
await new Promise(x => script.onload = x);
|
||||
return script;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} content
|
||||
* @return {!HTMLElement}
|
||||
*/
|
||||
function addScriptContent(content) {
|
||||
const script = document.createElement('script');
|
||||
script.type = 'text/javascript';
|
||||
script.text = content;
|
||||
document.head.appendChild(script);
|
||||
return script;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} options
|
||||
* @return {!Promise}
|
||||
* @return {!Promise<!ElementHandle>}
|
||||
*/
|
||||
async addStyleTag(options) {
|
||||
if (typeof options.url === 'string')
|
||||
return this.evaluate(addStyleUrl, options.url);
|
||||
return await this._context.evaluateHandle(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);
|
||||
return await this._context.evaluateHandle(addStyleContent, contents);
|
||||
}
|
||||
|
||||
if (typeof options.content === 'string')
|
||||
return this.evaluate(addStyleContent, options.content);
|
||||
return await this._context.evaluateHandle(addStyleContent, options.content);
|
||||
|
||||
throw new Error('Provide an object with a `url`, `path` or `content` property');
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @return {!Promise<!HTMLElement>}
|
||||
*/
|
||||
function addStyleUrl(url) {
|
||||
async function addStyleUrl(url) {
|
||||
const link = document.createElement('link');
|
||||
link.rel = 'stylesheet';
|
||||
link.href = url;
|
||||
const promise = new Promise(x => link.onload = x);
|
||||
document.head.appendChild(link);
|
||||
return promise;
|
||||
await new Promise(x => link.onload = x);
|
||||
return link;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} content
|
||||
* @return {!HTMLElement}
|
||||
*/
|
||||
function addStyleContent(content) {
|
||||
const style = document.createElement('style');
|
||||
style.type = 'text/css';
|
||||
style.appendChild(document.createTextNode(content));
|
||||
document.head.appendChild(style);
|
||||
return style;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user