This code retrieves and processes today's browsing history from a Chrome SQLite database, likely for analysis or reporting.
npm run import -- "get bookmarks from local chrome database"
var sqlite3 = require('better-sqlite3');
var HISTORY_FILE = '/Users/briancullinan/Library/Application Support/Google/Chrome/Default/History copy';
var db = new sqlite3(HISTORY_FILE, {});
var today = Math.floor(Date.now() / 60 / 60 / 48) * 60 * 60 * 48;
// reverse of chromeDtToDate
var todayOffset = (today - new Date(1601, 0, 1).getTime()) * 1000;
function getHistory() {
return Promise.resolve()
// .then(() => db.prepare('PRAGMA table_info(visits)').all())
// .then(() => db.prepare('SELECT * FROM visits WHERE visit_time > ?').all(todayOffset))
// sqlite_temp_master
// .then(() => db.prepare('SELECT name FROM sqlite_master WHERE type = ?').all('table'))
.then(() => db.prepare('SELECT * FROM urls WHERE last_visit_time > ?').all(todayOffset))
}
if(typeof $ !== 'undefined') {
$.async();
getHistory()
.then(r => $.sendResult(r))
.catch(e => $.sendError(e))
}
const sqlite3 = require('better-sqlite3');
const fs = require('fs');
const path = require('path');
const { console } = require('console');
// Constants
const CHROME_HISTORY_FILE = '/Users/briancullinan/Library/Application Support/Google/Chrome/Default/History copy';
const DATE_EPOCH = new Date(1601, 0, 1);
// Function to calculate the epoch time offset
function getEpochTimeOffset(date) {
return (date - DATE_EPOCH.getTime()) * 1000;
}
// Function to get the history from the Chrome database
function getHistory(db) {
return db.prepare('SELECT * FROM urls WHERE last_visit_time >?').all(getEpochTimeOffset(new Date()));
}
// Function to connect to the SQLite database and retrieve the history
async function getConnection() {
try {
// Check if the file exists before attempting to connect to the database
if (fs.existsSync(CHROME_HISTORY_FILE)) {
const db = new sqlite3(CHROME_HISTORY_FILE, {});
return { db, getHistory };
} else {
console.error(`Error: File not found: ${CHROME_HISTORY_FILE}`);
return null;
}
} catch (error) {
console.error(`Error connecting to database: ${error.message}`);
return null;
}
}
// Main execution
(async () => {
const { db, getHistory } = await getConnection();
if (db) {
try {
const history = await getHistory(db);
if (typeof $!== 'undefined') {
$ async() {
$.sendResult(history);
};
}
} catch (error) {
console.error(`Error retrieving history: ${error.message}`);
if (typeof $!== 'undefined') {
$ async() {
$.sendError(error);
};
}
} finally {
db.close();
}
}
})();
This code snippet retrieves and processes browsing history data from a SQLite database file associated with Google Chrome.
Here's a breakdown:
Dependencies:
better-sqlite3
library for interacting with SQLite databases.Database Setup:
HISTORY_FILE
).db
) to the specified file.Time Filtering:
today
.todayOffset
) to filter history entries for today onwards.getHistory
Function:
urls
table where the last_visit_time
is greater than todayOffset
.Async Execution (if applicable):
$
exists (likely indicating an asynchronous environment).$
is defined, it calls $.async()
and uses getHistory()
to retrieve the history data.r
) to $.sendResult()
or the error (e
) to $.sendError()
based on the outcome.Purpose:
This code snippet extracts and processes browsing history data from a Chrome database file, likely for analysis or reporting purposes. It filters the history to include only entries from the current day.