C# 从VisualStudio访问Chrome选项卡的DOM
我经常编写C# 从VisualStudio访问Chrome选项卡的DOM,c#,vb.net,visual-studio,google-chrome,dom,C#,Vb.net,Visual Studio,Google Chrome,Dom,我经常编写c#或VB.net来访问和修改internetexplorer页面的DOM,方法如下: SHDocVw.InternetExplorer IE = shell.Windows().Item(i); mshtml.HTMLDocument doc = IE.Document; mshtml.IHTMLElement o = doc.getElementById(sID); 如何访问在Chrome中打开的网页的DOM?我想我已经用尽了google和stackoverflow,根本没有找到
c#
或VB.net
来访问和修改internetexplorer
页面的DOM
,方法如下:
SHDocVw.InternetExplorer IE = shell.Windows().Item(i);
mshtml.HTMLDocument doc = IE.Document;
mshtml.IHTMLElement o = doc.getElementById(sID);
如何访问在
Chrome
中打开的网页的DOM
?我想我已经用尽了google和stackoverflow,根本没有找到任何东西。任何帮助都将不胜感激。您可以使用Javascript访问它。我打赌不能用.NET实现这一点。根据文档,Chrome不提供.NETAPI。(除非我漏掉了什么)为了类似的目的,我一直在努力想办法解决这个问题。这个故事的寓意是,您要么需要使用“本机消息传递”(Chrome基本上称之为deployed.EXE),要么创建一个使用JavaScript与应用程序对话的扩展 这与IE不同,IE可以简单地将底层DOM分配给对象。Chrome不会以同样的方式公开API或底层DOM 我解决这个问题的方法是使用C#中的WebSocket服务器(NuGet中的
SuperWebSocket
),以及Chrome扩展中的Javascript调用上述WebSocket服务器。对于Chrome扩展,JavaScript在后台运行,因此您可以连接并建立会话来来回传递消息
以下是如何启动WebSocket客户端的示例:
您可以使用任何想要的WebSocket服务器库。其要点是Chrome扩展打开了与应用程序的通信线路,然后这两条线路来回传递消息,Chrome扩展或应用程序可以根据这些消息进行操作。例如,C#可以传递包含要在网页上更新的字段元素的Chrome扩展JSON
此外,请记住,您必须将您的Chrome扩展部署到Chrome Web应用商店(如果需要,请设置为private),否则它将自动禁用。Hi Mederic。谢谢你的链接,但是他们从来没有访问过Chrome文档,只需从Chrome窗口中提取URL。你为什么认为Chrome会允许你访问DOM谢谢,我将在下周尝试!
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);
}
}
}
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;
}
}
}