Skip to content

Instantly share code, notes, and snippets.

@Gowee
Created May 27, 2021
Embed
What would you like to do?
Download PDF from read.nlc.cn
async function downloadBook(aid, bid) {
const [title, myreader, kime, fime] = await getBookKey(aid, bid)
const r = await fetch(`http://read.nlc.cn/menhu/OutOpenBook/getReader?aid=${aid}&bid=${bid}&kime=${kime}&fime=${fime}`, { method: "POST", headers: { myreader: myreader }})
const d = await r.arrayBuffer()
saveByteArray(title + ".pdf", d)
}
async function getBookKey(aid, bid) {
const r = await fetch(`http://read.nlc.cn/OutOpenBook/OpenObjectBook?aid=${aid}&bid=${bid}`)
const d = await r.text()
const title = /var title = \'(.+?)\'/.exec(d)[1]
const myreader = /tokenKey=\"(\w+)\"/.exec(d)[1]
const kime = /timeKey=\"(\w+)\"/.exec(d)[1]
const fime = /timeFlag=\"(\w+)\"/.exec(d)[1]
return [title, myreader, kime, fime]
}
// https://stackoverflow.com/a/37340749/5488616
function saveByteArray(reportName, byte) {
var blob = new Blob([byte], {type: "application/pdf"});
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
var fileName = reportName;
link.download = fileName;
link.click();
};
downloadBook("403", "8179.0")
// To be executed at http://read.nlc.cn/ in the browser console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment