【问题标题】:Programmatically acess Google chrome history以编程方式访问 Google chrome 历史记录
【发布时间】:2012-01-26 08:23:48
【问题描述】:

我想在谷歌浏览器中索引所有用户操作和网站。我了解谷歌浏览器索引 sqlLite 数据库中的所有数据。我如何在我自己的应用程序中以编程方式访问 chrome 网络历史记录

【问题讨论】:

  • 当您说 VS 2008 时,我假设您的意思是 .net 3.5。VS 是一个 IDE,而不是一个框架。
  • 可能想知道为什么?数据库被锁定
  • @MeSutPişkin 在访问数据库之前关闭 chrome

标签: c# sqlite google-chrome .net-3.5 browser-history


【解决方案1】:

这段代码可能会对你有所帮助:

//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; }

}

【讨论】:

    【解决方案2】:

    在带有 Datagridview 工具的 Windows 窗体应用程序中 - 按钮单击事件

    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)
            {
            }
        }
    

    参考source

    这里我已经将历史文件复制到应用程序启动路径以避免 SQLite 错误“数据库已锁定”。

    【讨论】:

      【解决方案3】:

      使用下面的代码得到“Windows/x86_64”作为结果

         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 ();
          } 
      

      【讨论】:

        【解决方案4】:

        您需要从SqLite downloads page下载相应的程序集

        添加对 SQLite 程序集的引用后,它与标准 ADO.net 非常相似

        所有用户历史都存储在历史数据库中,位于下面连接字符串中的路径

        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());
        }
        

        【讨论】:

        • 抛出数据库被锁定异常
        • 相同的“数据库已锁定”
        • 可能数据库被锁定,因为 Chrome 的一个活动实例正在使用它,尝试将文件复制到另一个位置然后读取它。
        猜你喜欢
        • 2010-09-08
        • 2015-08-31
        • 1970-01-01
        • 2016-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-02
        • 1970-01-01
        相关资源
        最近更新 更多