From 5428afae4e532e19a3c742db8281fe005b36ce8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Mon, 19 Aug 2024 11:36:02 -0300 Subject: [PATCH 1/4] Prepare docs for v19 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9db7dc3e2..1ef3127dc 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Puppeteer Sharp is a .NET port of the official [Node.JS Puppeteer API](https://g ## Recent news -* [Puppeteer-Sharp 2023 recap](https://www.hardkoded.com/blog/puppeteer-sharp-2023-recap). +PuppeteerSharp now supports AOT compilation! Check the [PuppeteerSharp 19 release notes!](https://github.com/hardkoded/puppeteer-sharp/releases/tag/v19.9.0). ## Useful links @@ -28,7 +28,7 @@ Puppeteer Sharp is a .NET port of the official [Node.JS Puppeteer API](https://g ## Prerequisites -* As Puppeteer-Sharp is a NetStandard 2.0 library, the minimum platform versions are .NET Framework 4.6.1 and .NET Core 2.0. [Read more](https://docs.microsoft.com/en-us/dotnet/standard/net-standard). +* Puppeteer-Sharp comes in two flavors: a NetStandard 2.0 library for .NET Framework 4.6.1 and .NET Core 2.0 or greater. and a .NET 8 version. * If you have issues running Chrome on Linux, the Puppeteer repo has a [great troubleshooting guide](https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md). * X-server is required on Linux. From 3d3f415f61ed7c292711a860d2c6f4e796a08585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Mon, 19 Aug 2024 11:56:18 -0300 Subject: [PATCH 2/4] add help doc --- docfx_project/examples/AOT.md | 70 ++++++++++++++++++++++++++++++++++ docfx_project/examples/toc.yml | 2 + 2 files changed, 72 insertions(+) create mode 100644 docfx_project/examples/AOT.md diff --git a/docfx_project/examples/AOT.md b/docfx_project/examples/AOT.md new file mode 100644 index 000000000..31dc6b91d --- /dev/null +++ b/docfx_project/examples/AOT.md @@ -0,0 +1,70 @@ +# How to test a Chrome Extension +_Contributors: [Dario Kondratiuk](https://github.com/kblok)_ + +## Problem + +You need to use Puppeteer Sharp in an application set up for AOT compilation. + +## Solution + +You shouldn't need to do anything special to use Puppeteer Sharp in an AOT environment. The library is already prepared for it.\ +The only challenge you might face is if you use any custom class to pass into or get from an Evaluate function. In that case you will need to provide a serialization context to PuppeteerSharp.\ +Let's say you have a class like this: + +```csharp +public class TestClass +{ + public string Name { get; set; } +} +``` + +You need to create a serialization context like this: + +```csharp +[JsonSerializable(typeof(TestClass))] +public partial class DemoJsonSerializationContext : JsonSerializerContext +{} + +``` + +_For more information about `JsonSerializerContext` see [How to use source generation in System.Text.Json](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation?WT.mc_id=DT-MVP-5003814)._ + +Once you have your own context you have to pass it to PuppeteerSharp before launching the browser: + +```csharp +Puppeteer.ExtraJsonSerializerContext = DemoJsonSerializationContext.Default; +``` + +`ExtraJsonSerializerContext` will be used the first time PuppeteerSharp serializes or deserializes any object. So it's important to set it before launching the browser. Once set, you can't change it. + +## Example + +```csharp +class MainClass +{ + public static async Task Main(string[] args) + { + Puppeteer.ExtraJsonSerializerContext = DemoJsonSerializationContext.Default; + var options = new LaunchOptions { Headless = true }; + + var browserFetcher = new BrowserFetcher(); + await browserFetcher.DownloadAsync(); + + await using var browser = await Puppeteer.LaunchAsync(options); + await using var page = await browser.NewPageAsync(); + + await page.GoToAsync("https://www.google.com"); + + var result = await page.EvaluateFunctionAsync("test => test", new TestClass { Name = "Dario"}); + } +} + +public class TestClass +{ + public string Name { get; set; } +} + +[JsonSerializable(typeof(TestClass))] +public partial class DemoJsonSerializationContext : JsonSerializerContext +{} +``` diff --git a/docfx_project/examples/toc.yml b/docfx_project/examples/toc.yml index a037e7d2d..1c16e0945 100644 --- a/docfx_project/examples/toc.yml +++ b/docfx_project/examples/toc.yml @@ -18,6 +18,8 @@ href: Page.Request.md - name: How to download an specific browser href: DownloadFetcher.Download.md + - name: Using PuppeteerSharp with AOT compilation + href: AOT.md - name: Advanced items: - name: How to log CDP communication From 9b6cebc46b434581216b4b5e288f62f90510c7b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Tue, 20 Aug 2024 09:33:48 -0300 Subject: [PATCH 3/4] Update README.md Co-authored-by: Jonas Nyrup --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ef3127dc..673ed08cc 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Puppeteer Sharp is a .NET port of the official [Node.JS Puppeteer API](https://g ## Recent news -PuppeteerSharp now supports AOT compilation! Check the [PuppeteerSharp 19 release notes!](https://github.com/hardkoded/puppeteer-sharp/releases/tag/v19.9.0). +PuppeteerSharp now supports AOT compilation! Check the [PuppeteerSharp 19 release notes!](https://github.com/hardkoded/puppeteer-sharp/releases/tag/v19.0.0). ## Useful links From ec36c08ec512deba2e58d8e5ae4eaf64fbb1e594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Tue, 20 Aug 2024 09:33:55 -0300 Subject: [PATCH 4/4] Update README.md Co-authored-by: Jonas Nyrup --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 673ed08cc..b9fbfffd5 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ PuppeteerSharp now supports AOT compilation! Check the [PuppeteerSharp 19 releas ## Prerequisites -* Puppeteer-Sharp comes in two flavors: a NetStandard 2.0 library for .NET Framework 4.6.1 and .NET Core 2.0 or greater. and a .NET 8 version. +* Puppeteer-Sharp comes in two flavors: a NetStandard 2.0 library for .NET Framework 4.6.1 and .NET Core 2.0 or greater and a .NET 8 version. * If you have issues running Chrome on Linux, the Puppeteer repo has a [great troubleshooting guide](https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md). * X-server is required on Linux.