0

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

6
  • Almost every day a question gets asked about this, and I don't think there's a fool-proof way to do this. What problem are you solving by writing a program that reads a browsers' history?
    – CodeCaster
    Jan 8, 2016 at 11:59
  • I am working on application that keeps the records of employee's browsing activity and storing them to database. This record will help respective manager to track time spent on browser. I can get directly from chrome using sqlite but i can not access when chrome is running. Jan 8, 2016 at 12:05
  • 1
    That's not what you want from application code. Install a proxy server.
    – CodeCaster
    Jan 8, 2016 at 12:07
  • There are several other things that i want to application. This is one of the module. Jan 8, 2016 at 12:10
  • Yeah that's nice. Reading browser history on user's machines is not trivial, may be easily circumvented and can break with any browser update. You have two problems: get user's browsing history and then "do something else". You can reliably get the browsing history by using a proxy server, and then "do something else" based on data you can obtain from the proxy server by querying its reporting database. What you want to do is never going to work well.
    – CodeCaster
    Jan 8, 2016 at 12:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.