从 Mono WebBrowser 控件访问 HTML DOM?

发布于 2024-09-26 00:02:07 字数 6868 浏览 0 评论 0原文

使用 .NET 的 Windows 窗体 Webbrowser 控件,访问 DOM 非常容易。考虑这种情况

this.webBrowser1.Navigate
            (Path.Combine(Application.StartupPath,"TestPage.html"));

现在,假设您的 html 页面中有一个测试 javascript 方法,该方法接受字符串参数。因此,一旦加载完成,您就可以

webBrowser1.Document.InvokeScript("test", new object[] { "hi" });

现在,我正在尝试在 Mono 中运行它。在运行时,Mono 应该使用 Mono 的 Web 浏览器控件 - http://www.mono-project.com/WebBrowser 。我发现一些非常奇怪的行为(请参阅更新)

问题是,如何通过 Mono 的网络浏览器控件访问 HTML DOM,可能调用脚本?

更新

刚刚注意到,仅当我连接到网络时,webBrowser1.Document 才为空。否则,如果我处于离线状态,脚本实际上会被调用,但之后 Mono 运行时崩溃,并出现此错误

Stacktrace:

  at (wrapper managed-to-native) Mono.Mozilla.Base.gluezilla_evalScript (intptr,string) <0x00004>    
  at (wrapper managed-to-native) Mono.Mozilla.Base.gluezilla_evalScript (intptr,string) <0x00004>    
  at Mono.Mozilla.Base.EvalScript (Mono.WebBrowser.IWebBrowser,string) <IL 0x0001a, 0x00038>    
  at Mono.Mozilla.DOM.Document.InvokeScript (string) <IL 0x00007, 0x00017>    
  at System.Windows.Forms.HtmlDocument.InvokeScript (string,object[]) [0x00054] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\HtmlDocument.cs:143    
  at WindowsFormsHost.Form1.ShowMsg (string) <IL 0x0002b, 0x000ac>    
  at WindowsFormsHost.Form1.testToolStripMenuItem_Click (object,System.EventArgs) <IL 0x00008, 0x0001c>    
  at System.Windows.Forms.ToolStripItem.OnClick (System.EventArgs) [0x0001c] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripItem.cs:1002    
  at System.Windows.Forms.ToolStripMenuItem.OnClick (System.EventArgs) [0x000a5] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripMenuItem.cs:267    
  at System.Windows.Forms.ToolStripMenuItem.HandleClick (System.EventArgs) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripMenuItem.cs:539    
  at System.Windows.Forms.ToolStripItem.FireEvent (System.EventArgs,System.Windows.Forms.ToolStripItemEventType) [0x00042] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripItem.cs:1776    
  at (wrapper remoting-invoke-with-check) System.Windows.Forms.ToolStripItem.FireEvent (System.EventArgs,System.Windows.Forms.ToolStripItemEventType) <IL 0x00034, 0x00061>    
  at System.Windows.Forms.ToolStrip.OnMouseUp (System.Windows.Forms.MouseEventArgs) [0x00047] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStrip.cs:960    
  at System.Windows.Forms.ToolStripDropDown.OnMouseUp (System.Windows.Forms.MouseEventArgs) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripDropDown.cs:630    
  at System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message&) [0x0007e] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Control.cs:5592    
  at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message&) [0x0017b] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Control.cs:5329    
  at System.Windows.Forms.ScrollableControl.WndProc (System.Windows.Forms.Message&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ScrollableControl.cs:807    
  at System.Windows.Forms.ToolStrip.WndProc (System.Windows.Forms.Message&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStrip.cs:1213    
  at System.Windows.Forms.ToolStripDropDown.WndProc (System.Windows.Forms.Message&) [0x0001a] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripDropDown.cs:722    
  at System.Windows.Forms.Control/ControlWindowTarget.OnMessage (System.Windows.Forms.Message&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Control.cs:234    
  at System.Windows.Forms.Control/ControlNativeWindow.WndProc (System.Windows.Forms.Message&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Control.cs:215    
  at System.Windows.Forms.NativeWindow.WndProc (intptr,System.Windows.Forms.Msg,intptr,intptr) [0x00085] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\NativeWindow.cs:242    
  at System.Windows.Forms.XplatUIWin32.InternalWndProc (intptr,System.Windows.Forms.Msg,intptr,intptr) [0x00031] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\XplatUIWin32.cs:1946    
  at (wrapper native-to-managed) System.Windows.Forms.XplatUIWin32.InternalWndProc (intptr,System.Windows.Forms.Msg,intptr,intptr) <IL 0x00021, 0x00073>    
  at (wrapper managed-to-native) System.Windows.Forms.XplatUIWin32.Win32DispatchMessage (System.Windows.Forms.MSG&) <0x00004>    
  at (wrapper managed-to-native) System.Windows.Forms.XplatUIWin32.Win32DispatchMessage (System.Windows.Forms.MSG&) <0x00004>    
  at System.Windows.Forms.XplatUIWin32.DispatchMessage (System.Windows.Forms.MSG&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\XplatUIWin32.cs:2146    
  at System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\XplatUI.cs:605    
  at System.Windows.Forms.Application.RunLoop (bool,System.Windows.Forms.ApplicationContext) [0x0036a] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Application.cs:984
  at System.Windows.Forms.Application.Run (System.Windows.Forms.ApplicationContext) [0x00014] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Application.cs:763
  at System.Windows.Forms.Application.Run (System.Windows.Forms.Form) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Application.cs:742
  at WindowsFormsHost.Program.Main () <IL 0x00013, 0x00044>
  at (wrapper runtime-invoke) object.runtime_invoke_void (object,intptr,intptr,intptr) <IL 0x0001b, 0x0003a>

此应用程序已请求运行时以异常方式终止它。 请联系应用程序的支持团队以获取更多信息。

Using .NET's Webbrowser control for Windows Forms , it is pretty easy to access the DOM. Consider this scenario

this.webBrowser1.Navigate
            (Path.Combine(Application.StartupPath,"TestPage.html"));

Now, Assume that you've a test javascript method in your html page, which accepts a string param. So, once loading is complete, you may

webBrowser1.Document.InvokeScript("test", new object[] { "hi" });

Now, I'm trying running this in Mono. At Runtime, Mono should be using Mono's web browser control - http://www.mono-project.com/WebBrowser. I am finding some very weird behavior (see the update)

Question is, how to access the HTML DOM, probably to invoke a script, via Mono's webbrowser control?

Update

Just noted that webBrowser1.Document is null only when I'm connected to the network. Otherwise, if I'm offline, the script is actually getting invoked, but then the Mono runtime crashes after that, with this error

Stacktrace:

  at (wrapper managed-to-native) Mono.Mozilla.Base.gluezilla_evalScript (intptr,string) <0x00004>    
  at (wrapper managed-to-native) Mono.Mozilla.Base.gluezilla_evalScript (intptr,string) <0x00004>    
  at Mono.Mozilla.Base.EvalScript (Mono.WebBrowser.IWebBrowser,string) <IL 0x0001a, 0x00038>    
  at Mono.Mozilla.DOM.Document.InvokeScript (string) <IL 0x00007, 0x00017>    
  at System.Windows.Forms.HtmlDocument.InvokeScript (string,object[]) [0x00054] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\HtmlDocument.cs:143    
  at WindowsFormsHost.Form1.ShowMsg (string) <IL 0x0002b, 0x000ac>    
  at WindowsFormsHost.Form1.testToolStripMenuItem_Click (object,System.EventArgs) <IL 0x00008, 0x0001c>    
  at System.Windows.Forms.ToolStripItem.OnClick (System.EventArgs) [0x0001c] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripItem.cs:1002    
  at System.Windows.Forms.ToolStripMenuItem.OnClick (System.EventArgs) [0x000a5] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripMenuItem.cs:267    
  at System.Windows.Forms.ToolStripMenuItem.HandleClick (System.EventArgs) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripMenuItem.cs:539    
  at System.Windows.Forms.ToolStripItem.FireEvent (System.EventArgs,System.Windows.Forms.ToolStripItemEventType) [0x00042] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripItem.cs:1776    
  at (wrapper remoting-invoke-with-check) System.Windows.Forms.ToolStripItem.FireEvent (System.EventArgs,System.Windows.Forms.ToolStripItemEventType) <IL 0x00034, 0x00061>    
  at System.Windows.Forms.ToolStrip.OnMouseUp (System.Windows.Forms.MouseEventArgs) [0x00047] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStrip.cs:960    
  at System.Windows.Forms.ToolStripDropDown.OnMouseUp (System.Windows.Forms.MouseEventArgs) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripDropDown.cs:630    
  at System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message&) [0x0007e] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Control.cs:5592    
  at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message&) [0x0017b] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Control.cs:5329    
  at System.Windows.Forms.ScrollableControl.WndProc (System.Windows.Forms.Message&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ScrollableControl.cs:807    
  at System.Windows.Forms.ToolStrip.WndProc (System.Windows.Forms.Message&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStrip.cs:1213    
  at System.Windows.Forms.ToolStripDropDown.WndProc (System.Windows.Forms.Message&) [0x0001a] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\ToolStripDropDown.cs:722    
  at System.Windows.Forms.Control/ControlWindowTarget.OnMessage (System.Windows.Forms.Message&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Control.cs:234    
  at System.Windows.Forms.Control/ControlNativeWindow.WndProc (System.Windows.Forms.Message&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Control.cs:215    
  at System.Windows.Forms.NativeWindow.WndProc (intptr,System.Windows.Forms.Msg,intptr,intptr) [0x00085] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\NativeWindow.cs:242    
  at System.Windows.Forms.XplatUIWin32.InternalWndProc (intptr,System.Windows.Forms.Msg,intptr,intptr) [0x00031] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\XplatUIWin32.cs:1946    
  at (wrapper native-to-managed) System.Windows.Forms.XplatUIWin32.InternalWndProc (intptr,System.Windows.Forms.Msg,intptr,intptr) <IL 0x00021, 0x00073>    
  at (wrapper managed-to-native) System.Windows.Forms.XplatUIWin32.Win32DispatchMessage (System.Windows.Forms.MSG&) <0x00004>    
  at (wrapper managed-to-native) System.Windows.Forms.XplatUIWin32.Win32DispatchMessage (System.Windows.Forms.MSG&) <0x00004>    
  at System.Windows.Forms.XplatUIWin32.DispatchMessage (System.Windows.Forms.MSG&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\XplatUIWin32.cs:2146    
  at System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG&) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\XplatUI.cs:605    
  at System.Windows.Forms.Application.RunLoop (bool,System.Windows.Forms.ApplicationContext) [0x0036a] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Application.cs:984
  at System.Windows.Forms.Application.Run (System.Windows.Forms.ApplicationContext) [0x00014] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Application.cs:763
  at System.Windows.Forms.Application.Run (System.Windows.Forms.Form) [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.6.7\mcs\class\Managed.Windows.Forms\System.Windows.Forms\Application.cs:742
  at WindowsFormsHost.Program.Main () <IL 0x00013, 0x00044>
  at (wrapper runtime-invoke) object.runtime_invoke_void (object,intptr,intptr,intptr) <IL 0x0001b, 0x0003a>

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天冷不及心凉 2024-10-03 00:02:07

它应该可以工作,但不幸的是,每次 Mozilla 发布更新时,事情都会比我希望的要严重得多。你是在windows还是linux上做这个的?您安装的 xulrunner 版本是什么?

It should work, but unfortunately things break much more than I would like every time mozilla throws up an update. Are you doing this on windows or linux? What xulrunner version do you have installed?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文