-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |