Странное поведение примера из официальной документации
Начил читать официальную дукументацию по anglesharp,последний пример работает как то странно Events in JavaScript and C#.По логике должен получить текст из событый load и hello из скрипта а получаю только из с# кода.
Код
using AngleSharp;
using System;
namespace ConsoleApp1
{
class MyClass
{
public async static void EventScriptingExample()
{
//We require a custom configuration
var config = Configuration.Default.WithJs();
//Create a new context for evaluating webpages with the given config
var context = BrowsingContext.New(config);
//This is our sample source, we will trigger the load event
var source = @"<!doctype html>
<html>
<head><title>Event sample</title></head>
<body>
<script>
console.log('Before setting the handler!');
document.addEventListener('load', function() {
console.log('Document loaded!');
});
document.addEventListener('hello', function() {
console.log('hello world from JavaScript!');
});
console.log('After setting the handler!');
</script>
</body>";
var document = await context.OpenAsync(req => req.Content(source));
//HTML should be output in the end
Console.WriteLine(document.DocumentElement.OuterHtml);
//Register Hello event listener from C# (we also have one in JS)
document.AddEventListener("hello", (s, ev) =>
{
Console.WriteLine("hello world from C#!");
});
var e = document.CreateEvent("event");
e.Init("hello", false, false);
document.Dispatch(e);
}
static void Main()
{
EventScriptingExample();
Console.ReadKey();
}
}
}
Как должно быть:
Что получаю:
Note: То есть событие выполняется только из с# кода.
Ответы (1 шт):
Автор решения: aepot
→ Ссылка
Наверное пример просто не полный. У меня заработало вот так:
using AngleSharp;
using AngleSharp.Js;
class ConsoleLogger : IConsoleLogger
{
public void Log(object[] values)
{
foreach (object value in values)
Console.WriteLine(value);
}
}
class MyClass
{
public async static Task EventScriptingExample()
{
//We require a custom configuration
var config = Configuration.Default.WithJs().WithConsoleLogger(c => new ConsoleLogger());
//Create a new context for evaluating webpages with the given config
var context = BrowsingContext.New(config);
//This is our sample source, we will trigger the load event
var source = @"<!doctype html>
<html>
<head><title>Event sample</title></head>
<body>
<script>
console.log('Before setting the handler!');
document.addEventListener('load', function() {
console.log('Document loaded!');
});
document.addEventListener('hello', function() {
console.log('hello world from JavaScript!');
});
console.log('After setting the handler!');
</script>
</body>";
var document = await context.OpenAsync(req => req.Content(source));
//HTML should be output in the end
Console.WriteLine(document.DocumentElement.OuterHtml);
//Register Hello event listener from C# (we also have one in JS)
document.AddEventListener("hello", (s, ev) =>
{
Console.WriteLine("hello world from C#!");
});
var e = document.CreateEvent("event");
e.Init("hello", false, false);
document.Dispatch(e);
}
static async Task Main()
{
await EventScriptingExample();
Console.ReadKey();
}
}
Я поправил еще асинхронность, чтобы все красиво было.
Before setting the handler!
After setting the handler!
Document loaded!
<html><head><title>Event sample</title></head>
<body>
<script>
console.log('Before setting the handler!');
document.addEventListener('load', function() {
console.log('Document loaded!');
});
document.addEventListener('hello', function() {
console.log('hello world from JavaScript!');
});
console.log('After setting the handler!');
</script>
</body></html>
hello world from JavaScript!
hello world from C#!

