Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

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

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.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...