【问题标题】:Get the URLs of opened tabs in browser获取浏览器中打开的标签页的 URL
【发布时间】:2012-10-29 18:22:12
【问题描述】:

我正在做一个项目,我需要获取浏览器中所有打开的标签页的 URL(例如 Google Chrome、IE、Firefox、...)

有没有办法使用 c# 或 vb.net 来做到这一点?

附言它是一个窗体应用程序

【问题讨论】:

  • 这很难做到。如果您解释了为什么要这样做,以防万一有更好的方法,这可能会有所帮助。
  • 如果我创建了一个,我不会让我的浏览器共享这些信息。对我来说似乎不安全......
  • 它特定于浏览器并且可靠性令人怀疑; stackoverflow.com/questions/7814027/… 注意与代理服务器方法相关的 cmets。

标签: c# vb.net winforms winapi desktop-application


【解决方案1】:

即:

    Dim browser As SHDocVw.InternetExplorer
    Dim myLocalLink As String
    Dim myDoc As mshtml.IHTMLDocument2
    Dim shellWindows As SHDocVw.ShellWindows = New SHDocVw.ShellWindows()
    Dim filename As String

    For Each ie As SHDocVw.InternetExplorer In shellWindows
        filename = System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower()

        If filename = "iexplore" Then
            browser = ie
            myDoc = browser.Document
            myLocalLink = myDoc.url
            MessageBox.Show(myLocalLink)
        End If
    Next

c#:

        SHDocVw.InternetExplorer browser;
        string myLocalLink;
        mshtml.IHTMLDocument2 myDoc;
        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
        string filename;
        foreach (SHDocVw.InternetExplorer ie in shellWindows)
        {
            filename = System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
            if ((filename == "iexplore"))
            {
                browser = ie;
                myDoc = browser.Document;
                myLocalLink = myDoc.url;
                MessageBox.Show(myLocalLink);
            }

你需要:

microsoft.mshtml

shdocvw.dll

FireFox c#:

using NDde.Client;


        DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
        dde.Connect();
        string url = dde.Request("URL", int.MaxValue);
        dde.Disconnect();
        MessageBox.Show(url);

下载 NDde.2.01.0563 (NDde.dll)

我也做过 Chrome:

vb.net:

共享功能:

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
 ByVal lpClassName As String, _
 ByVal lpWindowName As String) As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                  ByVal childAfter As IntPtr, _
                  ByVal lclassName As String, _
                  ByVal windowTitle As String) As IntPtr
End Function

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function


    Dim h As IntPtr
    For Each p As Process In Process.GetProcessesByName("chrome")
        h = FindWindow("Chrome_WidgetWin_1", p.MainWindowTitle)
        Exit For
    Next
    Dim urlH As IntPtr
    urlH = FindWindowEx(h, 0, "Chrome_OmniboxView", Nothing)
    Dim urlHH As IntPtr = Marshal.AllocHGlobal(100)
    Dim NumText As Integer = SendMessage(urlH, &HD, 50, urlHH)
    Dim url As String = Marshal.PtrToStringUni(urlHH)
    MsgBox(url)

【讨论】:

    猜你喜欢
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多