Skip to content

Instantly share code, notes, and snippets.

@sandrinodimattia
Last active February 5, 2022 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandrinodimattia/92dfbe0196aefd65eede201d85c34afc to your computer and use it in GitHub Desktop.
Save sandrinodimattia/92dfbe0196aefd65eede201d85c34afc to your computer and use it in GitHub Desktop.
Export Google Chrome History to a CSV file
import fs from 'fs';
import path from 'path';
import { v4 } from 'uuid';
import each from 'p-each-series';
import Database from 'sqlite-async';
/**
* Find all of the Chrome History files.
*/
function getHistoryFiles(startPath, targetFile) {
let results = [];
const files = fs.readdirSync(startPath);
for (let i = 0; i < files.length; i++) {
const filename = path.join(startPath, files[i]);
if (fs.lstatSync(filename).isDirectory()) {
results = results.concat(getHistoryFiles(filename, targetFile));
} else if (filename.endsWith(targetFile) === true) {
results.push(filename);
}
}
return results;
}
/**
* Query the Chrome History DB.
*/
async function queryHistory(dbPath, sql) {
const db = await Database.open(dbPath);
const rows = await db.all(sql);
await db.close();
return rows.map((row) => {
return {
utc_time: row.last_visit_time,
title: row.title,
url: row.url,
};
});
}
// Get the Chrome History files
const chromePath = path.join(
process.env.HOME,
'Library',
'Application Support',
'Google',
'Chrome'
);
const paths = getHistoryFiles(chromePath, '/History');
// Make sure the data folder exists.
if (!fs.existsSync('./data')) {
fs.mkdirSync('./data');
}
// Iterate over each DB.
await each(paths, async (historyFile) => {
// Copy the file.
let dbPath = path.join('./data', v4() + '.sqlite');
fs.copyFileSync(historyFile, dbPath);
// Get the history;
let sql = `SELECT title, datetime(last_visit_time/1000000 + (strftime('%s', '1601-01-01')),'unixepoch') last_visit_time, url from urls WHERE DATETIME (last_visit_time/1000000 + (strftime('%s', '1601-01-01')), 'unixepoch') >= DATETIME('now', '-3652 days') group by title, last_visit_time order by last_visit_time`;
const history = await queryHistory(dbPath, sql);
// Output as CSV.
for (const record of history) {
console.log(`${record.utc_time}\t${record.title}\t${record.url}`);
}
});
{
"name": "export-chrome-history",
"type": "module",
"main": "index.js",
"scripts": {
"start": "node index > history.csv"
},
"dependencies": {
"p-each-series": "^3.0.0",
"sqlite-async": "^1.1.2",
"uuid": "^8.3.2"
}
}
@sandrinodimattia
Copy link
Author

Exports all of your Google Chrome Bookmarks on OSX to a CSV file. To run this tool:

npm install
npm run start

This will create a TAB delimited history.csv file with all of your Chrome history.

@btiernay
Copy link

btiernay commented Feb 5, 2022

You're hired!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment