如何从 Chrome 和 Firefox 获取打开页面的 URL? [英] How can I get URLs of open pages from Chrome and Firefox?

查看:47
本文介绍了如何从 Chrome 和 Firefox 获取打开页面的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个系统托盘应用程序,需要检查内部基于 Web 的应用程序是否打开.

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

我可以使用以下方法检查 IE:

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

但是,一些使用该系统的用户更喜欢 Chrome 或 Firefox.

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

如何为 Chrome 和 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.)

推荐答案

它特定于每个浏览器.这是主要的:

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

  • Internet Explorer - 您可以使用 SHDocVw(就像您一样)
  • Firefox - 您可以使用 DDE 获取 URL(来源如下)
  • Chrome - 您可以在枚举所有子窗口时获取 URL,直到您使用类Chrome_OmniboxView"到达控件,然后使用 GetWindowText 获取文本莉>
  • Opera - 您可以使用与 Firefox 相同的东西,但使用opera"
  • Safari - 没有已知的方法,因为它使用自定义绘制的控件
  • 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

自 2014 年以来,Chrome 发生了变化,您需要获取具有辅助功能的网址.

使用 DDE 从 Firefox/Opera 获取 URL 的代码(使用了 NDDE - 唯一好的 DDE 包装器对于.NET):

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

这篇关于如何从 Chrome 和 Firefox 获取打开页面的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆