Skip to content

Instantly share code, notes, and snippets.

@Shaked
Last active June 27, 2022 22:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Shaked/65e198dcda8784c557075b9b89265a16 to your computer and use it in GitHub Desktop.
Save Shaked/65e198dcda8784c557075b9b89265a16 to your computer and use it in GitHub Desktop.
Kindle Keywords Exporter
// ==UserScript==
// @name KindleKeywordsExporter
// @namespace https://kindle.amazon.com/your_highlights
// @version 0.0.8
// @downloadURL https://gist.github.com/raw/65e198dcda8784c557075b9b89265a16/kindle.user.js
// @updateURL https://gist.github.com/raw/65e198dcda8784c557075b9b89265a16/kindle.meta.js
// @description Helps exporting highlighted keywords from Kindle
// @include https://kindle.amazon.com/your_highlights
// @match https://kindle.amazon.com/your_highlights
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
// ==UserScript==
// @name KindleKeywordsExporter
// @namespace https://kindle.amazon.com/your_highlights
// @version 0.0.8
// @downloadURL https://gist.github.com/raw/65e198dcda8784c557075b9b89265a16/kindle.user.js
// @updateURL https://gist.github.com/raw/65e198dcda8784c557075b9b89265a16/kindle.meta.js
// @description Helps exporting highlighted keywords from Kindle
// @include https://kindle.amazon.com/your_highlights
// @match https://kindle.amazon.com/your_highlights
var collectKeywords = function() {
var kindleBooksHighlightedData = {};
$('#allHighlightedBooks').children().each(function(index, element) {
if (element.getAttribute('id')) {
bookId = element.getAttribute('id');
kindleBooksHighlightedData[bookId] = {
meta: {
title: $(element).find('a').text(),
href: $(element).find('a').attr('href'),
hightlightsCount: $(element).find('.highlightCount' + bookId).text(),
notesCount: $(element).find('.noteCount' + bookId).text(),
lastHighlighted: $(element).find('.lastHighlighted').text(),
},
highlights: []
};
return;
}
console.log(bookId);
kindleBooksHighlightedData[bookId].highlights.push({
text: $(element).find('.highlight').text(),
href: $(element).find('a.readMore').attr('href'),
note: $(element).find('.noteContent').text()
});
});
return kindleBooksHighlightedData;
};
function copyToClipboard(element) {
var $temp = $("<textarea></textarea>");
$("body").append($temp);
$temp.val(element).select();
document.execCommand("copy");
$temp.remove();
}
var copyJson = function(kindleBooksHighlightedData) {
var kindleBooksHighlightedDataJson = JSON.stringify(kindleBooksHighlightedData);
console.log(kindleBooksHighlightedDataJson);
copyToClipboard(kindleBooksHighlightedDataJson);
};
var copyNotes = function(kindleBooksHighlightedData) {
var notes = '';
for (var bookId in kindleBooksHighlightedData) {
var bookInfo = kindleBooksHighlightedData[bookId];
notes += bookInfo.meta.title + "\n";
notes += "\t\t(" + bookInfo.meta.href + ")\n";
notes += "\t" + bookInfo.meta.hightlightsCount + "\n";
notes += "\t" + bookInfo.meta.notesCount + "\n";
notes += "\t" + bookInfo.meta.lastHighlighted + "\n";
notes += "\n" + "Highlights" + "\n";
for (var index in bookInfo.highlights) {
var bookHighlights = bookInfo.highlights[index];
notes += "\t" + bookHighlights.text + "\n";
notes += "\t\t(" + bookHighlights.href + ")\n";
if (bookHighlights.note) {
notes += "\t\t[" + bookHighlights.note + "]\n";
}
}
notes += "\n\n";
}
console.log(notes);
copyToClipboard(notes);
};
var copyBasic = function(kindleBooksHighlightedData) {
var notes = '';
for (var bookId in kindleBooksHighlightedData) {
var bookInfo = kindleBooksHighlightedData[bookId];
for (var index in bookInfo.highlights) {
var bookHighlights = bookInfo.highlights[index];
notes += bookHighlights.text + "\n\n";
}
}
notes += notes.substr(0, notes.length, -1);
copyToClipboard(notes);
};
var html = `
<style>
#KindleKeywordsExporter {
position: fixed !important;
width: 15% !important;
background: #fff !important;
border: 3px solid black !important;
color: black !important;
text-align: center !important;
z-index: 10000 !important;
padding: 0 !important;
border-radius: 0 !important;
box-shadow: 5px 5px 15px #888888 !important;
z-index: 10000 !important;
height: 90% !important;
}
button.kindle-keywords-exporter-btn {
margin-top: 25%;
height: 50px;
color: #ffffff;
background: #2196F3;
border: 0;
cursor: pointer;
position:relative;
}
</style>
<div id="KindleKeywordsExporter">
<button method='json' class='kindle-keywords-exporter-btn' id='KindleKeywordsExporterJsonBtn'>JSON to clipboard</button>
<button method='notes' class='kindle-keywords-exporter-btn' id='KindleKeywordsExporterNotesBtn'>Notes to clipboard</button>
<button method='basic' class='kindle-keywords-exporter-btn' id='KindleKeywordsExporterBasicBtn'>Basic to clipboard</button>
</div>
`;
$('body').prepend(html);
function scrollToBottomOfPage() {
$(window).scrollTop($(document).height());
setTimeout(function(){
if ($("#stillLoadingBooks").length > 0) {
scrollToBottomOfPage();
} else {
$(window).scrollTop(0);
$("KindleKeywordsExporter").append("loaded data, now copy!");
}
}, 4000);
}
$('.kindle-keywords-exporter-btn').click(function() {
var method = $(this).attr('method');
switch (method) {
case "json":
copyJson(collectKeywords());
break;
case "notes":
copyNotes(collectKeywords());
break;
case "basic":
copyBasic(collectKeywords());
break;
}
});
// ==/UserScript==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment