Programmatically acess Google chrome history
Asked Answered
L

4

8

I want to index all the user actions and websites in google chrome. i understand that google chrome index all the data in sqlLite database. how can i Programmatically access the chrome web history in my own application

Lindell answered 22/12, 2011 at 18:22 Comment(4)
Duplicate of #2562592.Wholesale
I assume you mean .net 3.5 when you say VS 2008. VS is an IDE, not a framework.Comnenus
may wonder why? database is lockedAchievement
@MeSutPişkin Close chrome before accessing the databaseLindell
W
11

You need to download the appropriate assembly from the SqLite downloads page

Once you add a reference to the SQLite assembly, its very similar to standard ADO.net

All the user history is stored in the History database located at the path in the connection string below

SQLiteConnection conn = new SQLiteConnection
    (@"Data Source=C:\Users\YourUserName\AppData\Local\Google\Chrome\User Data\Default\History");
conn.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
//  cmd.CommandText = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;";
//  Use the above query to get all the table names
cmd.CommandText = "Select * From urls";
SQLiteDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Console.WriteLine(dr[1].ToString());
}
Wiesbaden answered 22/12, 2011 at 19:18 Comment(3)
The database is locked exception is thrownCloven
Same "The Database is locked"Phlegmatic
Maybe the database is locked because an active instance of Chrome is using it, try to copy the file to another location and then read it.Insolation
O
2

In Windows form application with Datagridview tool - button click event

private void button1_Click(object sender, EventArgs e)
    {
        string google = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Google\Chrome\User Data\Default\History";
        string fileName = DateTime.Now.Ticks.ToString();
        File.Copy(google, Application.StartupPath + "\\" + fileName);
        using (SQLiteConnection con = new SQLiteConnection("DataSource = " + Application.StartupPath + "\\" + fileName + ";Versio=3;New=False;Compress=True;"))
        {
            con.Open();
            //SQLiteDataAdapter da = new SQLiteDataAdapter("select url,title,visit_count,last_visit_time from urls order by last_visit_time desc", con);
            SQLiteDataAdapter da = new SQLiteDataAdapter("select * from urls order by last_visit_time desc", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            con.Close();
        }
        try // File already open error is skipped
        {
          if (File.Exists(Application.StartupPath + "\\" + fileName))
             File.Delete(Application.StartupPath + "\\" + fileName);
        }
        catch (Exception)
        {
        }
    }

Reference source

Here i have copied the History file to Application startup path in order to avoid SQLite error "database is locked".

Oatmeal answered 14/4, 2016 at 15:47 Comment(0)
E
0

Used below code getting "Windows/x86_64" as a result

   try 
   {
        Class.forName ("org.sqlite.JDBC");
        connection = DriverManager.getConnection ("jdbc:sqlite:/C:/Users/tarun.kakkar/AppData/Local/Google/Chrome/User Data/Default/History");

        statement = connection.createStatement ();
        resultSet = statement.executeQuery ("SELECT * FROM urls");

        while (resultSet.next ()) 
        {
            System.out.println ("URL [" + resultSet.getString ("url") + "]" + ", visit count [" + resultSet.getString ("visit_count") + "]");
        }
    } 

    catch (Exception e) 
    {
        e.printStackTrace ();
    } 
Extract answered 10/11, 2015 at 7:12 Comment(0)
C
-1

This piece of code might help you:

//Use sqlite for read chrome history
using System;

using System.IO;

using System.Linq;

using System.Data;

using System.Data.SqlClient;

using System.Data.SQLite;

using System.Collections.Generic;

public class Program
{
    public static void Main()
    
{
        
GetChromehistory();

Console.WriteLine();
    }

    public void GetChromehistory()
    {
        //Connection string 
        string path = @"\Google\Chrome\User Data\Default\History";
        string chromehistorypath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + path;
        if (File.Exists(chromehistorypath))
        {
            SQLiteConnection connection = new SQLiteConnection("Data Source=" + chromehistorypath + ";Version=3;New=False;Compress=True;");
            connection.Open();
            DataSet dataSet = new DataSet();
            SQLiteDataAdapter adapter = new SQLiteDataAdapter("select * from urls order by last_visit_time desc", connection);
            connection.Close();
            adapter.Fill(dataSet);
            var allHistoryItems = new List<ChromeHistoryItem>();
            if (dataSet != null && dataSet.Tables.Count > 0 & dataSet.Tables[0] != null)
            {
                DataTable dt = dataSet.Tables[0];
                foreach (DataRow historyRow in dt.Rows)
                {
                    ChromeHistoryItem historyItem = new ChromeHistoryItem()
                    {
                        URL = Convert.ToString(historyRow["url"]),
                        Title = Convert.ToString(historyRow["title"])
                    };

                    // Chrome stores time elapsed since Jan 1, 1601 (UTC format) in microseconds
                    long utcMicroSeconds = Convert.ToInt64(historyRow["last_visit_time"]);

                    // Windows file time UTC is in nanoseconds, so multiplying by 10
                    DateTime gmtTime = DateTime.FromFileTimeUtc(10 * utcMicroSeconds);

                    // Converting to local time
                    DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(gmtTime, TimeZoneInfo.Local);
                    historyItem.VisitedTime = localTime;

                    allHistoryItems.Add(historyItem);
                }
            }
        }

    }

}

public class ChromeHistoryItem

{
    
public string URL { get; set; }
    
public string Title { get; set; }
    
public DateTime VisitedTime { get; set; }

}
Chutzpah answered 15/6, 2021 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.