获取所有活动和最小化的chrome选项卡标题和URL c#

时间:2016-01-08 11:57:28

标签: c# winforms google-chrome url

我正在开发一个应用程序,它将读取chrome,firefox和IE浏览历史记录。在this文章的帮助下,我可以使用sqlite获取chrome的历史记录,但是当chrome没有运行时。所以我已经阅读了打开的浏览器选项卡并将它们存储到数据库中。为了实现这一点,我修改了this code,现在我可以获得所有标签标题和活动标签网址以及活动窗口的标题。 这是代码

public class BrowserHistoryBO
{
    public string BrowserName { get; set; }
    public string URL { get; set; }
    public string Title { get; set; }
    public bool IsDelete { get; set; }
    public bool IsCurrent { get; set; }
}
public void GetGoogleHistory()
    {
        List<BrowserHistoryBO> listBH = new List<BrowserHistoryBO>();
        List<string> listTempBH = new List<string>();
        string currentTitle = string.Empty;
        string url = string.Empty;
        Process[] procsChrome = Process.GetProcessesByName("chrome").Where(us => us.MainWindowHandle != IntPtr.Zero).ToArray();
        foreach (Process chrome in procsChrome)
        {
            AutomationElement element = AutomationElement.FromHandle(chrome.MainWindowHandle);
            if (element == null)
            { }
            AutomationElementCollection edits5 = element.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
            AutomationElement edit = edits5[0];
            //Current tab URL
            url = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
            //Current tab title
            currentTitle = chrome.MainWindowTitle.Replace(" - Google Chrome", "");
            if (url != string.Empty && currentTitle != string.Empty && currentTitle != "New Tab")
            {
                //check whether current tab already in list
                var result = listBH.FirstOrDefault(us => us.Title == currentTitle);
                //if yes then add/update url
                if (result != null)
                {
                    result.URL = url;
                    result.IsCurrent = true;
                }
                //If no then add new tab to the list
                else
                {
                    listBH.Add(new BrowserHistoryBO
                    {
                        Title = currentTitle,
                        URL = url,
                        IsDelete = false,
                        IsCurrent = true
                    });
                }
            }
            AutomationElement elm = AutomationElement.FromHandle(chrome.MainWindowHandle);
            AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
            Condition condNewTab = new PropertyCondition(AutomationElement.NameProperty, "New Tab");
            TreeWalker tWalker = TreeWalker.ControlViewWalker;
            AutomationElement elemNewTab = elm.FindFirst(TreeScope.Descendants, condNewTab);
            AutomationElement elemTabStrip = tWalker.GetParent(elemNewTab);
            Condition tabItemCondition = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem);
            if ((elemNewTab == null))
            {
                return;
            }
            //get all the child's title
            foreach (AutomationElement tabItem in elemTabStrip.FindAll(TreeScope.Children, tabItemCondition))
            {
                listTempBH.Add(tabItem.Current.Name);
            }
            var lookup = new List<string>(listBH.Select(x => x.Title));//Get stored Title
            var lookupClosedTab = lookup.Except(listTempBH).ToList();//get closed tab
            var newTab = listTempBH.Except(lookup).ToList();
            //find closed tab
            foreach (var item in lookupClosedTab)
            {
                var ct = listBH.Where(us => us.Title == item).FirstOrDefault();
                ct.IsDelete = true;
            }
            //below code will add new opened tab to list
            foreach (var item in newTab)
            {
                listBH.Add(new BrowserHistoryBO
                {
                    Title = item,
                    URL = "",
                    IsDelete = false,
                    IsCurrent = false
                });
            }
        }
    }

当Tabs打开相同的窗口并且具有隐身模式时,此代码工作正常,但是当打开多个窗口或所有窗口都被最小化时,此代码仅返回单个窗口的标题。 我找到了this回答并修改了使用列表ChromeWindows = WindowsFinder(“Chrome_WidgetWin_1”,“chrome”); 的代码,但它只返回所有窗口的标题。 请帮我查找所有最小化,活动和打开的标签标题和网址。

请原谅我的语法错误

0 个答案:

没有答案