forked from microsoft/node-api-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
41 lines (37 loc) · 1.54 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 NodeEmbeddingPlatform nodejsPlatform = new(libnodePath, null);
using NodeEmbeddingThreadRuntime nodejs = nodejsPlatform.CreateThreadRuntime(appDir,
new NodeEmbeddingRuntimeSettings
{
MainScript =
"globalThis.require = require('module').createRequire(process.execPath);\n"
});
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(NodeEmbeddingThreadRuntime 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;
}
}