1

I often write c# or VB.net that access and modify the DOM of an Internet Explorer page using things like:

SHDocVw.InternetExplorer IE = shell.Windows().Item(i);
mshtml.HTMLDocument doc = IE.Document;
mshtml.IHTMLElement o = doc.getElementById(sID);

How do I go about accessing the DOM of a webpage open in Chrome? I think I have exhausted google and stackoverflow and not really found anything at all. Any help would be appreciated.

analyticalpicasso
  • 1,993
  • 8
  • 26
  • 45
Benja
  • 23
  • 1
  • 5
  • http://stackoverflow.com/questions/16305238/get-url-from-all-open-tabs-in-google-chrome-using-vb-net-and-ui-automation – Mederic May 18 '17 at 08:59
  • Hi Mederic. Thanks for the link but they never access the Chrome document, simply extract the URL from the Chrome Window. – Benja May 18 '17 at 09:03
  • and why would you think chrome would allow you to access the DOM – Mederic May 18 '17 at 09:05

2 Answers2

2

I just went through trying to figure this out for similar purposes. Moral of the story is you either need to use "Native Messaging" (which Chrome basically calls a deployed .EXE) or create an Extension that uses JavaScript to talk with your application.

https://developer.chrome.com/extensions https://developer.chrome.com/extensions/nativeMessaging

This is a departure from IE where you can simply assign the underlying DOM to an object. Chrome does not expose an API or the underlying DOM in the same way.

The way I solved this was using a WebSocket Server in C# (SuperWebSocketin NuGet), and Javascript within a Chrome Extension to call said WebSocket Server. For Chrome Extensions, the JavaScript runs in the background so you can connect and establish a session to pass messsages back and forth.

Here is a sample for how to start WebSocket Client: https://github.com/kerryjiang/SuperWebSocket/blob/master/Samples/BasicConsole/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;

namespace SuperWebSocket.Samples.BasicConsole
{
class Program
{
    static void Main(string[] args)
    {
         Console.WriteLine("Press any key to start the WebSocketServer!");

        Console.ReadKey();
        Console.WriteLine();

        var appServer = new WebSocketServer();

        //Setup the appServer
        if (!appServer.Setup(2012)) //Setup with listening port
        {
            Console.WriteLine("Failed to setup!");
            Console.ReadKey();
            return;
        }

        appServer.NewMessageReceived += new SessionHandler<WebSocketSession, string>(appServer_NewMessageReceived);

        Console.WriteLine();

        //Try to start the appServer
        if (!appServer.Start())
        {
            Console.WriteLine("Failed to start!");
            Console.ReadKey();
            return;
        }

        Console.WriteLine("The server started successfully, press key 'q' to stop it!");

        while (Console.ReadKey().KeyChar != 'q')
        {
            Console.WriteLine();
            continue;
        }

        //Stop the appServer
        appServer.Stop();

        Console.WriteLine();
        Console.WriteLine("The server was stopped!");
        Console.ReadKey();
    }

    static void appServer_NewMessageReceived(WebSocketSession session, string message)
    {
        //Send the received message back
        session.Send("Server: " + message);
    }
}
}

Here is sample JavaScript to attempt a new WebSocket connection:

function attemptConnection() {


if (!connectionMade) {
    var exampleSocket = new WebSocket("ws://localhost:8080");

    exampleSocket.onopen = function () {
        connectionMade = true;
    };

    exampleSocket.onmessage = function (event) {
    //msg received from C#          
    }

    exampleSocket.onerror = function () {
        //do nothing
    }

    exampleSocket.onclose = function () {
        connectionMade = false;
    }


}



}

You can use any WebSocket server library you want. The gist of it is the Chrome Extension Opens a line of communication with your application and then the two pass messages back and forth which the Chrome Extension or Application can act on. For example, C# can pass the Chrome Extension JSON containing field elements that you want to update on the web page.

Also, keep in mind that you have to deploy your Chrome Extension to the Chrome Web Store (and set to private if required) or else it will automatically disable itself.

Jason Bayldon
  • 1,296
  • 6
  • 24
  • 41
0

You can use Javascript to access it.


Chrome Extension - Get DOM content
I would bet against being able to do this with .NET. According to documentation, Chrome does not provide .NET APIs. (Unless I miss something)
Community
  • 1
  • 1
Petros Apotsos
  • 615
  • 2
  • 6
  • 13