sappho192 / AmaurotTranslator

Lightweight GUI Ja↔Ko translator
1 stars 0 forks source link

Upgrade .NET and replace the browser engine #4

Closed sappho192 closed 2 years ago

sappho192 commented 2 years ago

Since .NET 5 will be deprecated, upgrade to .NET 6.0. And replace the browser engine from Selenium.Chromium to PuppeteerSharp.

Check the following code:

// .NET 6.0 code
using HtmlAgilityPack;
using PuppeteerSharp;

await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
Browser browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = true
});
var page = await browser.NewPageAsync();
page.DefaultTimeout = 5000;

async Task<string> Translate(Page page, string url)
{
    await page.GoToAsync(url, WaitUntilNavigation.Networkidle2);
    var content = await page.GetContentAsync();

    //Console.WriteLine(content);

    var doc = new HtmlDocument();
    doc.LoadHtml(content);
    string translated = string.Empty;
    try
    {
        var pathElement = doc.GetElementbyId("txtTarget");
        translated = pathElement.InnerText.Trim();
    }
    catch (Exception e)
    {
        Console.Error.WriteLine(e);
    }
    return translated;
}

var result = await Translate(page, "https://papago.naver.com/?sk=ja&tk=ko&hn=0&st=%E3%81%8A%E3%81%AF%E3%82%88%E3%81%86");
Console.WriteLine(result);

result = await Translate(page, "https://papago.naver.com/?sk=ja&tk=ko&hn=0&st=%E3%81%84%E3%82%89%E3%81%A3%E3%81%97%E3%82%83%E3%81%84%E3%81%BE%E3%81%9B~");
Console.WriteLine(result);
sappho192 commented 2 years ago

Resolved in #5