Skip to content

Instantly share code, notes, and snippets.

@tomoya55
Forked from viatsko/ExportKindle.js
Last active May 10, 2021 11:37
Show Gist options
  • Save tomoya55/e91fc94dd4339165dbddbe2a2f18baf9 to your computer and use it in GitHub Desktop.
Save tomoya55/e91fc94dd4339165dbddbe2a2f18baf9 to your computer and use it in GitHub Desktop.
Amazon Kindle Exporter
// The following data should be run in the console while viewing the page https://read.amazon.com/
// It will export a JSON file called "kindleItems.json"
(function () {
const saveJSON = function (data, filename) {
if (!data) {
console.error("save: No data");
return;
}
if (!filename) filename = "download.json";
if (typeof data === "object") {
data = JSON.stringify(data, undefined, 4);
}
var blob = new Blob([data], { type: "text/json" }),
e = document.createEvent("MouseEvents"),
a = document.createElement("a");
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ["text/json", a.download, a.href].join(":");
e.initMouseEvent(
"click",
true,
false,
window,
0,
0,
0,
0,
0,
false,
false,
false,
false,
0,
null
);
a.dispatchEvent(e);
};
const db = openDatabase("K4W", "5", "thedatabase", 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql(
"SELECT * FROM bookdata ORDER BY purchaseDate;",
[],
function (tx, results) {
const data = [];
for (i = 0; i < results.rows.length; i++) {
const item = results.rows.item(i);
// Get the data
const asin = item.asin;
const title = item.title;
const titlePronunciation = item.titlePronunciation;
const authors = JSON.parse(item.authors);
let authorPronunciations;
if (item.authorPronunciations) {
authorPronunciations = JSON.parse(item.authorPronunciations);
}
const purchaseDate = new Date(item.purchaseDate).toLocaleDateString();
data.push({
asin,
title,
titlePronunciation,
authors,
authorPronunciations,
purchaseDate,
});
}
// "Export" the data
saveJSON(data, "kindleItems.json");
}
);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment