Skip to content

Commit 5323e8f

Browse files
Meir017kblok
authored andcommitted
Add type=module to addScriptTag (#372)
1 parent b1fb17f commit 5323e8f

File tree

10 files changed

+73
-11
lines changed

10 files changed

+73
-11
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"parserOptions": {
3+
"sourceType": "module"
4+
}
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import num from './es6module.js';
2+
window.__es6injected = num;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 42;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import num from './es6/es6module.js';
2+
window.__es6injected = num;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import num from './es6module.js';
2+
window.__es6injected = num;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 42;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import num from './es6/es6module.js';
2+
window.__es6injected = num;

lib/PuppeteerSharp.Tests/PageTests/AddScriptTagTests.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,40 @@ public async Task ShouldWorkWithAUrl()
3030
Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("__injected"));
3131
}
3232

33+
[Fact]
34+
public async Task ShouldWorkWithAUrlAndTypeModule()
35+
{
36+
await Page.GoToAsync(TestConstants.EmptyPage);
37+
await Page.AddScriptTagAsync(new AddTagOptions { Url = "/es6/es6import.js", Type = "module" });
38+
Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("__es6injected"));
39+
}
40+
41+
[Fact]
42+
public async Task ShouldWorkWithAPathAndTypeModule()
43+
{
44+
await Page.GoToAsync(TestConstants.EmptyPage);
45+
await Page.AddScriptTagAsync(new AddTagOptions
46+
{
47+
Path = Path.Combine(Directory.GetCurrentDirectory(), Path.Combine("assets", "es6", "es6pathimport.js")),
48+
Type = "module"
49+
});
50+
await Page.WaitForFunctionAsync("() => window.__es6injected");
51+
Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("__es6injected"));
52+
}
53+
54+
[Fact]
55+
public async Task ShouldWorkWithAContentAndTypeModule()
56+
{
57+
await Page.GoToAsync(TestConstants.EmptyPage);
58+
await Page.AddScriptTagAsync(new AddTagOptions
59+
{
60+
Content = "import num from '/es6/es6module.js'; window.__es6injected = num;",
61+
Type = "module"
62+
});
63+
await Page.WaitForFunctionAsync("() => window.__es6injected");
64+
Assert.Equal(42, await Page.EvaluateExpressionAsync<int>("__es6injected"));
65+
}
66+
3367
[Fact]
3468
public async Task ShouldThrowAnErrorIfLoadingFromUrlFail()
3569
{
@@ -72,4 +106,4 @@ public async Task ShouldWorkWithContent()
72106
Assert.Equal(35, await Page.EvaluateExpressionAsync<int>("__injected"));
73107
}
74108
}
75-
}
109+
}

lib/PuppeteerSharp/AddTagOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@ public class AddTagOptions
1919
/// Raw JavaScript content to be injected into frame
2020
/// </summary>
2121
public string Content { get; set; }
22+
23+
/// <summary>
24+
/// Script type. Use <c>module</c> in order to load a Javascript ES6 module
25+
/// </summary>
26+
/// <seealso href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script"/>
27+
public string Type { get; set; }
2228
}
2329
}

lib/PuppeteerSharp/Frame.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public Task<ElementHandle> WaitForSelectorAsync(string selector, WaitForSelector
252252
/// <seealso cref="Page.WaitForXPathAsync(string, WaitForSelectorOptions)"/>
253253
public Task<ElementHandle> WaitForXPathAsync(string xpath, WaitForSelectorOptions options = null)
254254
=> WaitForSelectorOrXPathAsync(xpath, true, options);
255-
255+
256256
/// <summary>
257257
/// Waits for a timeout
258258
/// </summary>
@@ -406,31 +406,40 @@ public async Task<ElementHandle> AddStyleTag(AddTagOptions options)
406406
/// <seealso cref="Page.AddScriptTagAsync(string)"/>
407407
public async Task<ElementHandle> AddScriptTag(AddTagOptions options)
408408
{
409-
const string addScriptUrl = @"async function addScriptUrl(url) {
409+
const string addScriptUrl = @"async function addScriptUrl(url, type) {
410410
const script = document.createElement('script');
411411
script.src = url;
412+
if(type)
413+
script.type = type;
412414
document.head.appendChild(script);
413415
await new Promise((res, rej) => {
414416
script.onload = res;
415417
script.onerror = rej;
416418
});
417419
return script;
418420
}";
419-
const string addScriptContent = @"function addScriptContent(content) {
421+
const string addScriptContent = @"function addScriptContent(content, type = 'text/javascript') {
420422
const script = document.createElement('script');
421-
script.type = 'text/javascript';
423+
script.type = type;
422424
script.text = content;
423425
document.head.appendChild(script);
424426
return script;
425427
}";
426428

429+
async Task<ElementHandle> AddScriptTagPrivate(string script, string urlOrContent, string type)
430+
{
431+
var context = await GetExecutionContextAsync();
432+
return (string.IsNullOrEmpty(type)
433+
? await context.EvaluateFunctionHandleAsync(script, urlOrContent)
434+
: await context.EvaluateFunctionHandleAsync(script, urlOrContent, type)) as ElementHandle;
435+
}
436+
427437
if (!string.IsNullOrEmpty(options.Url))
428438
{
429439
var url = options.Url;
430440
try
431441
{
432-
var context = await GetExecutionContextAsync();
433-
return (await context.EvaluateFunctionHandleAsync(addScriptUrl, url)) as ElementHandle;
442+
return await AddScriptTagPrivate(addScriptUrl, url, options.Type);
434443
}
435444
catch (PuppeteerException)
436445
{
@@ -442,14 +451,12 @@ public async Task<ElementHandle> AddScriptTag(AddTagOptions options)
442451
{
443452
var contents = File.ReadAllText(options.Path, Encoding.UTF8);
444453
contents += "//# sourceURL=" + options.Path.Replace("\n", string.Empty);
445-
var context = await GetExecutionContextAsync();
446-
return (await context.EvaluateFunctionHandleAsync(addScriptContent, contents)) as ElementHandle;
454+
return await AddScriptTagPrivate(addScriptContent, contents, options.Type);
447455
}
448456

449457
if (!string.IsNullOrEmpty(options.Content))
450458
{
451-
var context = await GetExecutionContextAsync();
452-
return (await context.EvaluateFunctionHandleAsync(addScriptContent, options.Content)) as ElementHandle;
459+
return await AddScriptTagPrivate(addScriptContent, options.Content, options.Type);
453460
}
454461

455462
throw new ArgumentException("Provide options with a `Url`, `Path` or `Content` property");

0 commit comments

Comments
 (0)