4

How can I get URLs of open pages from Chrome and Firefox?

How can I get URLs of open pages from Chrome and Firefox?

http://hintdesk.com/c-automationelement-left-click-and-send-keys/

How can I check if website is already opened in a webbrowser?

etc...

all fail. Either they return only the top most opened tab or do not work at all.

I need to check and see if firefox is opened at all to a url, if it is then refresh and focus that tab/url, if not open firefox to that page.

This is not a firefox addon or anything really having to do with firefox. That is my browser of choice so that is the one I desire to use. All this is to avoid consecutively opening the same url each time my app is run.

Community
  • 1
  • 1
AbstractDissonance
  • 1
  • 2
  • 16
  • 31

1 Answers1

2

You can print window name using System.Runtime.InteropServices.

[DllImport("user32.dll")]
static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

public static void PrintBrowserTabName()
{
    var browsersList = new List<string>
    {
        "chrome",
        "firefox",
        "iexplore",
        "safari",
        "opera",
        "edge"
    };

    foreach (var singleBrowser in browsersList)
    {
        var process = Process.GetProcessesByName(singleBrowser);
        if (process.Length > 0)
        {
            foreach (Process singleProcess in process)
            {
                IntPtr hWnd = singleProcess.MainWindowHandle;
                int length = GetWindowTextLength(hWnd);

                StringBuilder text = new StringBuilder(length + 1);
                GetWindowText(hWnd, text, text.Capacity);
                Console.WriteLine(text.ToString());
            }
        }
    }
}
clamchoda
  • 4,411
  • 2
  • 36
  • 74
Jayesh Tank
  • 129
  • 1
  • 8
  • On Chrome this returns only the title of the current active tab. Same on Edge (name changed to "msedge") – Markus Oct 06 '20 at 13:09