Skip to content

Commit

Permalink
Add JSDom example project (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongin authored Aug 12, 2024
1 parent d52ad7c commit 467e030
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/jsdom/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.JavaScript.NodeApi.Runtime;

namespace Microsoft.JavaScript.NodeApi.Examples;

public static class Program
{
public static void Main()
{
string appDir = Path.GetDirectoryName(typeof(Program).Assembly.Location)!;
string libnodePath = Path.Combine(appDir, "libnode.dll");
using NodejsPlatform nodejsPlatform = new(libnodePath);
using NodejsEnvironment nodejs = nodejsPlatform.CreateEnvironment(appDir);
if (Debugger.IsAttached)
{
int pid = Process.GetCurrentProcess().Id;
Uri inspectionUri = nodejs.StartInspector();
Debug.WriteLine($"Node.js ({pid}) inspector listening at {inspectionUri.AbsoluteUri}");
}

string html = "<!DOCTYPE html><p>Hello world!</p>";
string content = nodejs.Run(() => GetContent(nodejs, html));
Console.WriteLine(content);
}

private static string GetContent(NodejsEnvironment nodejs, string html)
{
JSValue jsdomClass = nodejs.Import(module: "jsdom", property: "JSDOM");
JSValue dom = jsdomClass.CallAsConstructor(html);
JSValue document = dom["window"]["document"];
string content = (string)document.CallMethod("querySelector", "p")["textContent"];
return content;
}
}
15 changes: 15 additions & 0 deletions examples/jsdom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## C# JSDom Example
This project is a C# executable application that uses the JSDOM library
(https://github.com/jsdom/jsdom) to parse HTML.

Before building and running this project, download or build `libnode.dll` that
_includes Node API embedding support_.
See https://microsoft.github.io/node-api-dotnet/scenarios/dotnet-js.html

| Command | Explanation
|-------------------------|--------------------------------------------------
| `dotnet pack ../..` | Build Node API .NET packages.
| `npm install` | Install JavaScript packages.
| `dotnet build` | Install .NET nuget packages; build example project.
| `dotnet run --no-build` | Run the example project.

23 changes: 23 additions & 0 deletions examples/jsdom/jsdom.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<RootNamespace>Microsoft.JavaScript.NodeApi.Examples</RootNamespace>
<EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>

<ItemGroup>
<None Include="README.md" />
<Compile Include="*.cs" />
<Content Include="..\..\bin\win-x64\libnode.dll">
<Visible>false</Visible>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="$(NodeApiDotnetPackageVersion)" />
</ItemGroup>
</Project>
37 changes: 37 additions & 0 deletions examples/jsdom/jsdom.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33103.201
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "jsdom", "jsdom.csproj", "{76D22090-236C-4F28-8863-ACF5C66E9559}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|arm64 = Debug|arm64
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|arm64 = Release|arm64
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{76D22090-236C-4F28-8863-ACF5C66E9559}.Debug|arm64.ActiveCfg = Debug|Any CPU
{76D22090-236C-4F28-8863-ACF5C66E9559}.Debug|arm64.Build.0 = Debug|Any CPU
{76D22090-236C-4F28-8863-ACF5C66E9559}.Debug|x64.ActiveCfg = Debug|Any CPU
{76D22090-236C-4F28-8863-ACF5C66E9559}.Debug|x64.Build.0 = Debug|Any CPU
{76D22090-236C-4F28-8863-ACF5C66E9559}.Debug|x86.ActiveCfg = Debug|Any CPU
{76D22090-236C-4F28-8863-ACF5C66E9559}.Debug|x86.Build.0 = Debug|Any CPU
{76D22090-236C-4F28-8863-ACF5C66E9559}.Release|arm64.ActiveCfg = Release|Any CPU
{76D22090-236C-4F28-8863-ACF5C66E9559}.Release|arm64.Build.0 = Release|Any CPU
{76D22090-236C-4F28-8863-ACF5C66E9559}.Release|x64.ActiveCfg = Release|Any CPU
{76D22090-236C-4F28-8863-ACF5C66E9559}.Release|x64.Build.0 = Release|Any CPU
{76D22090-236C-4F28-8863-ACF5C66E9559}.Release|x86.ActiveCfg = Release|Any CPU
{76D22090-236C-4F28-8863-ACF5C66E9559}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E61C40BC-6DC2-4619-8B32-75E681FAAEA1}
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions examples/jsdom/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "node-api-dotnet-examples-jsdom",
"dependencies": {
"jsdom": "^24.1.1",
"node-api-dotnet": "file:../../out/pkg/node-api-dotnet"
}
}

0 comments on commit 467e030

Please sign in to comment.