Skip to content

Instantly share code, notes, and snippets.

@matthewpoe
Forked from usayamadx/ExportKindle.js
Created December 31, 2021 18:12
Show Gist options
  • Save matthewpoe/a089e2954b5956be53c2673d22165e59 to your computer and use it in GitHub Desktop.
Save matthewpoe/a089e2954b5956be53c2673d22165e59 to your computer and use it in GitHub Desktop.
Amazon Kindle Export
// 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, false)
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) {
getItemsList(data.paginationToken)
}
} 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