R2D221 / WebView2.DOM

C# DOM bindings to be used with WebView2
MIT License
51 stars 10 forks source link

Please provide some helpful code examples to be used in Windows Forms App (Windows 10) #8

Closed zydjohnHotmail closed 3 years ago

zydjohnHotmail commented 3 years ago

Hello: I really need some good code samples for your repo for Win Forms platform, since I have to use SQL Server to get some data from the database. I have done the following: I download your repo in ZIP format, unzip it, and compile the class library WebView2.DOM, it worked, I generate a DLL to be used in Win Forms app. I created one Windows Forms App for target .NET 5.0, I am using Visual Studio 2019 Version 16.9.3 on Windows 10 (Version 20H2). I added WebView2.DOM class library, and WebView2 (version 1.0.824-prerelease), NodaTime Version 3.0.5. Basically, I use C# code to draw the Form1, adding one WebView2 instance, and set its source to https://www.bing.com/search?q=webview2, that is: I search “webview2” from Bing.com Add one button (button1), when I click button1, I want to find the links (href) from DOM elements. From my testing, when I searched for webview2 in Bing.com, on Developer Tools – Console Tab, I can find some web links from the DOM elements by their class-name, like this: var links = document.getElementsByClassName('b_title');

What I want to do in my C# project is that I want to use C# code to find out all the href attribute from all the DOM elements in the class. The following is my code under Button1_click():

private async void Button1_Click(object sender, EventArgs e) { try { await Task.Delay(2000); Document document = window.document; var links = document.getElementsByClassName("b_title"); Console.Beep(); } catch (JsonException ex) { Debug.Print("[Button1_Click] EX: " + ex.Message); } } I can compile my code, but when I run it, I got run-time error: System.InvalidOperationException HResult=0x80131509 Message=The calling thread cannot access this object because a different thread owns it. Source=WebView2.DOM StackTrace: at WebView2.DOM.window.get_Instance() in C:\WebView2\Helper\WebView2.DOM\WebView2.DOM\Helper\window.cs:line 12 at WebView2.DOM.window.get_document() in C:\WebView2\Helper\WebView2.DOM\WebView2.DOM\Helper\window.cs:line 26 at WebView2DomForm.Form1.d__2.MoveNext() in C:\WebView2\Helper\WebView2DomForm\WebView2DomForm\Form1.cs:line 26 It seems the statement: Document document = window.document; has some illegal cross thread call. If you have some free time, could you please check and let me know if I made any mistake using your repo. By the way, I wonder that if I can use your repo to do something more useful, as install a browser extension, like a new extension called “Privacy Pass” extension for solving hCaptcha. Thanks,

R2D221 commented 3 years ago

Sure. In your project you have this:

private async void Button1_Click(object sender, EventArgs e)
{
    try
    {
        await Task.Delay(2000);
        Document document = window.document;
        var links = document.getElementsByClassName("b_title");
        Console.Beep();
    }
    catch (JsonException ex)
    {
        Debug.Print("[Button1_Click] EX: " + ex.Message);
    }
}

But, the way this project works is that all JS objects (window, document, etc) exist on a separate thread. To use them you would change the code like this:

private async void Button1_Click(object sender, EventArgs e)
{
    try
    {
        await Task.Delay(2000);
        // ========
        // Add this to get access to window and document
        await webView.InvokeInBrowserContextAsync(window =>
        {
            Document document = window.document;
            var links = document.getElementsByClassName("b_title");
        });
        // ========
        Console.Beep();
    }
    catch (JsonException ex)
    {
        Debug.Print("[Button1_Click] EX: " + ex.Message);
    }
}
R2D221 commented 3 years ago

By the way, I wonder that if I can use your repo to do something more useful, as install a browser extension, like a new extension called “Privacy Pass” extension for solving hCaptcha.

The goal of this project is to offer an alternative to stuff like Electron, to develop desktop apps with a HTML frontend.

I don't know if WebView2 supports extensions, but that's beyond the reach of this project