Last active
February 5, 2022 02:29
-
-
Save sandrinodimattia/92dfbe0196aefd65eede201d85c34afc to your computer and use it in GitHub Desktop.
Export Google Chrome History to a CSV file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're hired!