mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(FrameManager): add type=module to addScriptTag (#2090)
This patch adds a new "type" option to the `addScriptTag` method that allows adding "module" tags to the page. Fixes #2078
This commit is contained in:
committed by
Andrey Lushnikov
parent
552be1ae87
commit
625c7ebdda
@@ -440,7 +440,7 @@ class Frame {
|
||||
const url = options.url;
|
||||
try {
|
||||
const context = await this._contextPromise;
|
||||
return (await context.evaluateHandle(addScriptUrl, url)).asElement();
|
||||
return (await context.evaluateHandle(addScriptUrl, url, options.type)).asElement();
|
||||
} catch (error) {
|
||||
throw new Error(`Loading script from ${url} failed`);
|
||||
}
|
||||
@@ -450,23 +450,26 @@ class Frame {
|
||||
let contents = await readFileAsync(options.path, 'utf8');
|
||||
contents += '//# sourceURL=' + options.path.replace(/\n/g, '');
|
||||
const context = await this._contextPromise;
|
||||
return (await context.evaluateHandle(addScriptContent, contents)).asElement();
|
||||
return (await context.evaluateHandle(addScriptContent, contents, options.type)).asElement();
|
||||
}
|
||||
|
||||
if (typeof options.content === 'string') {
|
||||
const context = await this._contextPromise;
|
||||
return (await context.evaluateHandle(addScriptContent, options.content)).asElement();
|
||||
return (await context.evaluateHandle(addScriptContent, options.content, options.type)).asElement();
|
||||
}
|
||||
|
||||
throw new Error('Provide an object with a `url`, `path` or `content` property');
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @param {string} type
|
||||
* @return {!Promise<!HTMLElement>}
|
||||
*/
|
||||
async function addScriptUrl(url) {
|
||||
async function addScriptUrl(url, type) {
|
||||
const script = document.createElement('script');
|
||||
script.src = url;
|
||||
if (type)
|
||||
script.type = type;
|
||||
document.head.appendChild(script);
|
||||
await new Promise((res, rej) => {
|
||||
script.onload = res;
|
||||
@@ -477,11 +480,12 @@ class Frame {
|
||||
|
||||
/**
|
||||
* @param {string} content
|
||||
* @param {string} type
|
||||
* @return {!HTMLElement}
|
||||
*/
|
||||
function addScriptContent(content) {
|
||||
function addScriptContent(content, type = 'text/javascript') {
|
||||
const script = document.createElement('script');
|
||||
script.type = 'text/javascript';
|
||||
script.type = type;
|
||||
script.text = content;
|
||||
document.head.appendChild(script);
|
||||
return script;
|
||||
|
||||
Reference in New Issue
Block a user