How to access Google Chrome browser history programmatically on local machine

后端 未结 6 676
無奈伤痛
無奈伤痛 2020-12-07 15:44

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.

相关标签:
6条回答
  • 2020-12-07 16:21

    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.

    0 讨论(0)
  • 2020-12-07 16:27

    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).

    0 讨论(0)
  • 2020-12-07 16:35

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

    0 讨论(0)
  • 2020-12-07 16:39

    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

    0 讨论(0)
  • 2020-12-07 16:39

    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 ();
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-07 16:43

    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.

    0 讨论(0)
提交回复
热议问题