【问题标题】:Interaction between two web browsers两个 Web 浏览器之间的交互
【发布时间】:2017-11-03 08:14:14
【问题描述】:

您好,我有一个“你为什么要打扰”的问题,但这对我正在尝试构建的应用程序很重要(长话短说)。

我的应用由两个并排的网络浏览器组成.net 语言?

【问题讨论】:

  • 我认为最好的办法是从第一个浏览器捕获点击事件,然后以编程方式在第二个浏览器中触发某些事件。见thisthis

标签: html vb.net web triggers webbrowser-control


【解决方案1】:

这在一定程度上是可能的,前提是您控制了他们托管的内容

好消息是,使用 ObjectForScripting 属性,您可以将数据从 javascript 到 .net 来回传递,然后再次传递,以便一个网络浏览器可以对另一个作出反应。

另一个好消息是它们将使用相同的 WinInet 缓存。因此,如果您为站点创建 cookie,如果它是会话 cookie 等,两个浏览器都可能读取它。

虽然我必须更多地了解你的想法,才能给你一个更好、更详细的答案。

这里是一些从 MSDN 关于 objectforscipting 的示例代码,展示了如何与单个 Web 浏览器控件进行交互

    Imports System
    Imports System.Windows.Forms
    Imports System.Security.Permissions

    <PermissionSet(SecurityAction.Demand, Name:="FullTrust")>
    <System.Runtime.InteropServices.ComVisibleAttribute(True)>
    Public Class Form1
        Inherits Form

        Private webBrowser1 As New WebBrowser()
        Private WithEvents button1 As New Button()

        <STAThread()>
        Public Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub

        Public Sub New()
            button1.Text = "call script code from client code"
            button1.Dock = DockStyle.Top
            webBrowser1.Dock = DockStyle.Fill
            Controls.Add(webBrowser1)
            Controls.Add(button1)
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
            Handles Me.Load

            webBrowser1.AllowWebBrowserDrop = False
            webBrowser1.IsWebBrowserContextMenuEnabled = False
            webBrowser1.WebBrowserShortcutsEnabled = False
            webBrowser1.ObjectForScripting = Me
            ' Uncomment the following line when you are finished debugging.
            'webBrowser1.ScriptErrorsSuppressed = True

            webBrowser1.DocumentText =
                "<html><head><script>" &
                "function test(message) { alert(message); }" &
                "</script></head><body><button " &
                "onclick=""window.external.Test('called from script code')"" > " &
                "call client code from script code</button>" &
                "</body></html>"
        End Sub

        Public Sub Test(ByVal message As String)
            MessageBox.Show(message, "client code")
        End Sub

        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
            Handles button1.Click

            webBrowser1.Document.InvokeScript("test",
                New String() {"called from client code"})

        End Sub

    End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-16
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多