I am working on an application what will read chrome, firefox and IE browsing history. With the help of this article, I can able to get chrome's history using sqlite but when chrome is not running. So I have read the opened tabs of browser and store them into database. To achieve this I modified this code and now i am able to get all the tab title and active tab url as well title of active window. Here is the 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
});
}
}
}
This code works fine when Tabs are opened same window and with incognito mode also, But when there are more than one window is open or all the windows are minimized, this code returns only title of single window. I found this answer and modified code that uses List ChromeWindows = WindowsFinder("Chrome_WidgetWin_1", "chrome"); but it returns only title of all window. Please help me to find all minimized, active and opened tabs Title and URL.
Pardon me for grammatical mistakes