How can I access the DOM using WebKit.NET?

I use WebKit in a C # application to render a CSS style XML document, and I would like to be able to add DOM elements. How to access the DOM for this? The problem is that the WebKitBrowser class does not have a property that provides access to a private webView element. Is it possible? Do I need to modify the class to add this? Am I just doing it wrong?

+3
source share
1 answer

WebKit.Net, since version 0.3.0, it seems that it does not have access to the code form. I did something a little different, I used WebKit.Net, but I added an HTTP listener to the application, and I configured the control to fetch pages from there. This way, I have access to messages and callbacks in one application. Otherwise, I am doing this with JavaScript on my page.

        HttpListener listener;

    public Form1( ) {
        InitializeComponent( );

        listener = new System.Net.HttpListener( );

        listener.Prefixes.Add( "http://*:88/" );
        listener.Start( );
        IAsyncResult result = listener.BeginGetContext( new AsyncCallback(  ListenerCallback ), listener );

    }

    public static void ListenerCallback( IAsyncResult result ) {

    string resp = "<body>test</body";

        HttpListener listener = ( HttpListener )result.AsyncState;

        // Call EndGetContext to complete the asynchronous operation.
        try {
            HttpListenerContext context = listener.EndGetContext( result );
            HttpListenerRequest request = context.Request;

            // Obtain a response object.
            HttpListenerResponse response = context.Response;

            // Construct a response.
            string webpage = request.Url.AbsolutePath.Substring( 1 );
            string responseString = null;
            if ( string.IsNullOrEmpty( webpage ) ) {
                responseString = resp;
            }
            else {
                webpage = webpage.Replace( ".", "_" );
                responseString = webkit_test.Properties.Resources.ResourceManager.GetResourceSet( System.Globalization.CultureInfo.CurrentCulture, true, false ).GetObject( webpage ) as string;
                if ( responseString == null ) {
                    responseString = "<html></html><body> 404 Web Page not Found</body>";
                }
            }
            byte[ ] buffer = System.Text.Encoding.UTF8.GetBytes( responseString );

            // Get a response stream and write the response to it.
            response.ContentType = "text/html; charset=UTF-8";
            response.ContentLength64 = buffer.Length;

            System.IO.Stream output = response.OutputStream;
            output.Write( buffer, 0, buffer.Length );
            output.Flush( );

            // You must close the output stream.
            output.Close( );
            IAsyncResult result1 = listener.BeginGetContext( new AsyncCallback( ListenerCallback ), listener );
        }
        catch { }
    }

Btw. This code will also read the web page from the entered resource.

0
source

Source: https://habr.com/ru/post/1714837/


All Articles