Skip to content

Instantly share code, notes, and snippets.

@rivy
Forked from usayamadx/ExportKindle.js
Last active March 10, 2022 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rivy/54d86297cc65bd21a25476d4d2da7d31 to your computer and use it in GitHub Desktop.
Save rivy/54d86297cc65bd21a25476d4d2da7d31 to your computer and use it in GitHub Desktop.
Amazon Kindle Export
// ref: <https://stackoverflow.com/questions/7191429/get-kindle-library-book-list>
// ref: <https://gist.github.com/jkubecki/d61d3e953ed5c8379075b5ddd8a95f22>
// spell-checker:ignore () booklist
// from <https://gist.github.com/rivy/54d86297cc65bd21a25476d4d2da7d31>
// ToDO: [2022-03-10; rivy] change to use `fetch()` and `async/await`
// ... available since early 2017 ... <https://caniuse.com/?search=fetch> , <https://caniuse.com/async-functions>
// 1 - login to "https://read.amazon.com"
// 2 - use "F12" to open developer window with "Console"
// 3 - from the "Console" prompt, enter ...
// init
let xhr = new XMLHttpRequest()
let domain = 'https://read.amazon.com/'
let items = []
let csvData = ""
// function
function getItemsList(paginationToken = null) {
let url = domain + 'kindle-library/search?query=&libraryType=BOOKS' + ( paginationToken ? '&paginationToken=' + paginationToken : '' ) + '&sortType=recency&querySize=50'
xhr.open('GET', url)
xhr.send()
}
// request result
xhr.onreadystatechange = function() {
switch ( xhr.readyState ) {
case 0:
console.log('uninitialized')
break
case 1:
console.log('loading...')
break
case 4:
if(xhr.status == 200) {
let data = xhr.responseText
data = JSON.parse(data)
if(data.itemsList) {
items.push(...data.itemsList)
}
if(data.paginationToken) {
setInterval(getItemsList(data.paginationToken), 500)
} else {
// ref: <https://ourcodeworld.com/articles/read/682/what-does-the-not-allowed-to-navigate-top-frame-to-data-url-javascript-exception-means-in-google-chrome>
// window.location = 'data:text/json;charset=utf8,' + encodeURIComponent(JSON.stringify(items))
data = 'data:text/json;charset=utf8,' + encodeURIComponent(JSON.stringify(items))
// ref: <https://stackoverflow.com/a/60428121/43774>
// var win = window.open()
// win.document.open()
// win.document.write('<iframe src=' + base64data + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>');
// win.document.close()
const link = document.createElement("a");
link.href = data
link.download = "kindle-booklist.json"
link.click()
}
} else {
console.log('Failed')
}
break
}
}
// action
getItemsList()
//// to csv
//items.forEach(item => {
// csvData += '"' + item.asin + '","' + item.title + '"\n'
//})
//window.location = 'data:text/csv;charset=utf8,' + encodeURIComponent(csvData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment