Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I'm writing a system tray app that needs to check if an internal web based app is open.

I can check IE using the following:

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();         string filename;         bool sdOpen = false;         foreach (SHDocVw.InternetExplorer ie in shellWindows)         {             filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();             if (filename.Equals("iexplore"))             {                 string[] urlParts = (ie.LocationURL.ToString()).Split('/');                 string website = urlParts[2];                 if (website == "myApp:8080") { sdOpen = true; };             }         }          if (sdOpen) { Console.WriteLine("App is open"); } else { Console.WriteLine("App is not open"); };          Console.ReadKey(true); 

However, some of the users using the system prefer Chrome or Firefox.

How can I do the same as above (i.e. get the urls of any open tabs in the browser) for Chrome and Firefox? (I'm not going to bother with other browsers as these are the only ones in use in our organisation.)

like image 214
Ben Avatar asked Oct 18 '11 21:10

Ben


People also ask

How do I copy the URL of all open tabs in Chrome?

Press Ctrl + a on Windows/Linux or ⌘ + a on Mac to select all bookmarks. Press Ctrl + c on Windows/Linux or ⌘ + c on Mac to copy all urls (and only urls).

How do I copy all open URLs in Firefox?

Click inside the field and press Ctrl + A to select all the text, then Ctrl + C to copy it. Then you can paste it into Notepad or Word to have a list of all URLs. Each one is separated with a pipe (|) symbol. Don't forget to replace your proper Home Page when done!


1 Answers

It's specific for every browser. That's for the major ones:

  • Internet Explorer - You can use SHDocVw (like you did)
  • Firefox - You can get the URL using DDE (source below)
  • Chrome - You can get the URL while enumerating all the child windows untill you get to the control with class "Chrome_OmniboxView" and then get the text using GetWindowText
  • Opera - You can use the same thing as Firefox, but with "opera"
  • Safari - There is no known method since it uses custom drawn controls

EDIT: Since 2014, Chrome has changed and you need to get the URL with Acessibility.

Code to get the URL from Firefox/Opera using DDE (which used NDDE - the only good DDE wrapper for .NET):

// // usage: GetBrowserURL("opera") or GetBrowserURL("firefox") //  private string GetBrowserURL(string browser) {     try {         DdeClient dde = new DdeClient(browser, "WWW_GetWindowInfo");         dde.Connect();         string url = dde.Request("URL", int.MaxValue);         string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);         dde.Disconnect();         return text[0].Substring(1);     } catch {         return null;     } } 
like image 133
blez Avatar answered Sep 28 '22 02:09

blez