从Visual Studio访问Chrome选项卡的DOM [英] Accessing the DOM of a Chrome Tab from Visual Studio

查看:128
本文介绍了从Visual Studio访问Chrome选项卡的DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常写c#VB.net来访问和修改Internet Explorer页面的DOM,例如:

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);

如何访问在Chrome中打开的网页的DOM?我想我已经用尽了谷歌和stackoverflow并没有真正找到任何东西.任何帮助将不胜感激.

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.

推荐答案

我只是出于类似目的试图解决这个问题.故事的寓意是您要么需要使用本地消息传递"(Chrome基本上将其称为已部署的.EXE),要么需要创建一个使用JavaScript与您的应用程序对话的扩展程序.

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

这与IE有所不同,您可以在其中简单地将基础DOM分配给对象. Chrome不会以相同的方式公开API或基础DOM.

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.

我解决此问题的方法是在C#中使用WebSocket服务器(在NuGet中为SuperWebSocket),并在Chrome扩展程序中使用Javascript来调用所述WebSocket服务器.对于Chrome扩展程序,JavaScript在后台运行,因此您可以连接并建立会话来回传递消息.

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.

以下是如何启动WebSocket客户端的示例: https://github.com/kerryjiang/SuperWebSocket/blob/master/Samples/BasicConsole/Program.cs

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);
    }
}
}

以下是示例JavaScript尝试建立新的WebSocket连接:

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;
    }


}



}

您可以使用任何所需的WebSocket服务器库.要点是Chrome扩展程序会打开与您的应用程序的通信线路,然后再来回传递两条消息,Chrome扩展程序或应用程序可以对其进行操作.例如,C#可以传递包含要在网页上更新的字段元素的Chrome扩展JSON.

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.

此外,请记住,您必须将Chrome扩展程序部署到Chrome网上应用店(并在需要时设置为私有),否则它将自动禁用自身.

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.

这篇关于从Visual Studio访问Chrome选项卡的DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆