How to access Google Chrome browser history programmatically on local machine
Asked Answered
D

6

34

I want to write a simple program which shows my internet activity over a period of time (which site I visited, how many times and so on). I mostly use Google Chrome browser. I found out Chrome stores browser history at this location (please correct me if I'm wrong)

C:\Documents and Settings\<username>\Local Settings\Application Data\Google\Chrome\User Data\Default

How can I open the history files? They don't have any file extension. I could not open using notepad, SQLite browser. How do I access this data programmatically? I want to know which file format it is and how to read it using a programming language like C#.

Dalrymple answered 1/4, 2010 at 16:50 Comment(0)
S
17

It is just a SQlite 3 database, I was able to open it successfully (though of course you can't open a locked database of a running browser).

Subspecies answered 1/4, 2010 at 17:0 Comment(6)
What did you use to open it successfully?Lagunas
@Arjang An sqlite3 client, obviouslySubspecies
where is the path of that SQLite file? how can I find it on my local computer?Scum
@MartinAJ the question contains a path. It can be different for modern Chrome versions and for different OSes though.Subspecies
@Subspecies See my answer on how to avoid LOCKED DATABASE - https://mcmap.net/q/451983/-database-file-locked-error-while-reading-chrome-history-c (happy 7 year old answer)Varuna
Now the sqlite file location has been change to C:\Users\{username}\AppData\Local\Google\Chrome\User Data\Profile 7\HistoryChaldean
I
8

Here is a class I created to read browsing data from Google chrome. Most of the code I got from here but I tweaked it abit to add support for Google Chrome. You probably also want to download the SQLite for .Net from here and add the references to System.Data.Sqlite.

class GoogleChrome
{
    public List<URL> URLs = new List<URL>();
    public IEnumerable<URL> GetHistory()
    {
        // Get Current Users App Data
        string documentsFolder = Environment.GetFolderPath
        (Environment.SpecialFolder.ApplicationData);
        string[] tempstr = documentsFolder.Split('\\');
        string tempstr1 = "";
        documentsFolder += "\\Google\\Chrome\\User Data\\Default";
        if (tempstr[tempstr.Length - 1] != "Local")
        {
            for (int i = 0; i < tempstr.Length - 1; i++)
            {
                tempstr1 += tempstr[i] + "\\";
            }
            documentsFolder =  tempstr1 + "Local\\Google\\Chrome\\User Data\\Default";
        }


        // Check if directory exists
        if (Directory.Exists(documentsFolder))
        {
            return ExtractUserHistory(documentsFolder);

        }
        return null;
    }


    IEnumerable<URL> ExtractUserHistory(string folder)
    {
        // Get User history info
        DataTable historyDT = ExtractFromTable("urls", folder);

        // Get visit Time/Data info
        DataTable visitsDT = ExtractFromTable("visits",
        folder);

        // Loop each history entry
        foreach (DataRow row in historyDT.Rows)
        {

                // Obtain URL and Title strings
                string url = row["url"].ToString();
                string title = row["title"].ToString();

                // Create new Entry
                URL u = new URL(url.Replace('\'', ' '),
                title.Replace('\'', ' '),
                "Google Chrome");

                // Add entry to list
                URLs.Add(u);
        }
        // Clear URL History
        DeleteFromTable("urls", folder);
        DeleteFromTable("visits", folder);

        return URLs;
    }
    void DeleteFromTable(string table, string folder)
    {
        SQLiteConnection sql_con;
        SQLiteCommand sql_cmd;

        // FireFox database file
        string dbPath = folder + "\\History";

        // If file exists
        if (File.Exists(dbPath))
        {
            // Data connection
            sql_con = new SQLiteConnection("Data Source=" + dbPath +
            ";Version=3;New=False;Compress=True;");

            // Open the Conn
            sql_con.Open();

            // Delete Query
            string CommandText = "delete from " + table;

            // Create command
            sql_cmd = new SQLiteCommand(CommandText, sql_con);

            sql_cmd.ExecuteNonQuery();

            // Clean up
            sql_con.Close();
        }
    }

    DataTable ExtractFromTable(string table, string folder)
    {
        SQLiteConnection sql_con;
        SQLiteCommand sql_cmd;
        SQLiteDataAdapter DB;
        DataTable DT = new DataTable();

        // FireFox database file
        string dbPath = folder + "\\History";

        // If file exists
        if (File.Exists(dbPath))
        {
            // Data connection
            sql_con = new SQLiteConnection("Data Source=" + dbPath +
            ";Version=3;New=False;Compress=True;");

            // Open the Connection
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();

            // Select Query
            string CommandText = "select * from " + table;

            // Populate Data Table
            DB = new SQLiteDataAdapter(CommandText, sql_con);
            DB.Fill(DT);

            // Clean up
            sql_con.Close();
        }
        return DT;
    }
}

The class for the URL:

class URL
{
    string url;
    string title;
    string browser;
    public URL(string url, string title, string browser)
    {
        this.url = url;
        this.title = title;
        this.browser = browser;
    }

    public string getData()
    {
        return browser + " - " + title + " - " + url;
    }
}

It worked like a charm for me. Hope it helps

Intercollegiate answered 6/4, 2012 at 18:22 Comment(0)
L
5

For viewing sqlite databases (which is what the Chromium history index is), I've preferred sqlitebrowser. It's free and works on Windows, Mac, and Linux. An all-time fave for me.

Laceylach answered 13/1, 2011 at 20:29 Comment(0)
R
2

Since the original poster asked for a simple program, here it is. The program was adapted from the java workspace website (as credited in the code). You will need to change the argument for getConnection () to point to where the history files reside on your machine. The program compiles and runs on my Linux 2.6.39 environment:

/**
 Adapted from http://www.javaworkspace.com/connectdatabase/connectSQLite.do
 Date: 09/25/2012

 Download sqlite-jdbc-<>.jar from http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC, and
 compile: javac GetChromiumHistory.java
 run:     java -classpath ".:sqlite-jdbc-3.7.2.jar" GetChromiumHistory
*/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author www.javaworkspace.com
 * 
 */
public class GetChromiumHistory
{
    public static void main (String[] args) 
    {

    Connection connection = null;
    ResultSet resultSet = null;
    Statement statement = null;

    try 
        {
        Class.forName ("org.sqlite.JDBC");
        connection = DriverManager
            .getConnection ("jdbc:sqlite:/home/username/.config/chromium/Default/History");
        statement = connection.createStatement ();
        resultSet = statement
            .executeQuery ("SELECT * FROM urls where visit_count > 100");

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

    catch (Exception e) 
        {
        e.printStackTrace ();
        } 

    finally 
        {
        try 
            {
            resultSet.close ();
            statement.close ();
            connection.close ();
            } 

        catch (Exception e) 
            {
            e.printStackTrace ();
            }
        }
    }
}
Runkle answered 26/9, 2012 at 10:51 Comment(0)
I
2

There is an open source program called Hindsight (https://github.com/obsidianforensics/hindsight) that analyzes browsing history in Chrome. While the program is rather large and complicated, it access the various Chrome SQLite files using SQL queries, which can pull out and use independently, either in a SQLite browser or a different program.

An example of one for the Chrome v30+ History database is:

SELECT urls.id, urls.url, urls.title, urls.visit_count, urls.typed_count, urls.last_visit_time, urls.hidden, urls.favicon_id, visits.visit_time, visits.from_visit, visits.visit_duration, visits.transition, visit_source.source
FROM urls JOIN visits ON urls.id = visits.url
LEFT JOIN visit_source ON visits.id = visit_source.id

There are many more SQL queries for different Chrome databases, as well as different versions of Chrome.

Indiscerptible answered 19/11, 2015 at 16:43 Comment(0)
T
0

I know Safari uses Binary PLists for its history (and SQLite for its cache). Is it possible that Chrome is doing the same?

Tadashi answered 1/4, 2010 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.