Skip to content

Instantly share code, notes, and snippets.

@mattrix007
Forked from jkubecki/ExportKindle.js
Last active January 2, 2019 04:56
Show Gist options
  • Select an option

  • Save mattrix007/a49c5a562cfdb73873728e82e5c3115d to your computer and use it in GitHub Desktop.

Select an option

Save mattrix007/a49c5a562cfdb73873728e82e5c3115d to your computer and use it in GitHub Desktop.
Amazon Kindle Export
// The following data should be run in the console while viewing the page https://read.amazon.com/
// It will export a CSV file called "download" which can (and should) be renamed with a .csv extension
var db = openDatabase('K4W', '2', 'thedatabase', 1024 * 1024);
getAmazonCsv = function() {
// Set header for CSV export line - change this if you change the fields used
var csvData = "ASIN,Title,Authors,PurchaseDate\n";
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM bookdata;', [], function(tx, results) {
var len = results.rows.length;
for (i = 1; i < len; i++) {
// Get the data
var asin = results.rows.item(i).asin;
var title = results.rows.item(i).title;
var authors = JSON.parse(results.rows.item(i).authors);
var purchaseDate = new Date(results.rows.item(i).purchaseDate).toLocaleDateString();
// Remove double quotes from titles to not interfere with CSV double-quotes
title = title.replace(/"/g, '');
// Concatenate the authors list - uncomment the next line to get all authors separated by ";"
// var authorList = authors.join(';');
// OR Take only first author - comment the next line if you uncommented the previous one
var authorList = authors[0];
// Write out the CSV line
csvData += '"' + asin + '","' + title + '","' + authorList + '","' + purchaseDate + '"\n'
}
// "Export" the data
// window.location = 'data:text/csv;charset=utf8,' + encodeURIComponent(csvData);
// from https://ourcodeworld.com/articles/read/682/what-does-the-not-allowed-to-navigate-top-frame-to-data-url-javascript-exception-means-in-google-chrome
var win = window.open();
win.document.write('<iframe src="' + 'data:text/csv;charset=utf8,' + encodeURIComponent(csvData) +
'" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; ' +
'width:100%; height:100%;" allowfullscreen></iframe>');
console.log("Sample Row:");
console.log(results.rows.item(1));
});
});
};
getAmazonCsv();
@mattrix007
Copy link
Author

Doesn't show up until i run Inspect

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