Skip to content

Instantly share code, notes, and snippets.

@tiansh
Created July 30, 2019 06:06
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 tiansh/01a2e201a748e2bb724bab652dd375a3 to your computer and use it in GitHub Desktop.
Save tiansh/01a2e201a748e2bb724bab652dd375a3 to your computer and use it in GitHub Desktop.
/*
* 从 jjwxc.net 下载文章
* 使用方法:
* 1. 打开网址 http://www.jjwxc.net/onebook.php?novelid=0000000 (替换000为需要的数字)
* 2. 打开网页控制台
* 3. 把这段粘贴进去
* 4. 等一段时间,浏览器提示你要下载时,确认下载
*/
const decoder = new TextDecoder('gbk');
const parser = new DOMParser();
async function getContent(url) {
const ab = await fetch(url).then(r=> r.arrayBuffer());
const html = decoder.decode(ab);
const dom = parser.parseFromString(html, 'text/html');
const noveltextNode = dom.querySelector('.noveltext');
const titleNode = noveltextNode.querySelector('h2');
const title = titleNode.parentNode.removeChild(titleNode).textContent;
const noteNode = noveltextNode.querySelector('.readsmall');
const note = noteNode && noteNode.parentNode.removeChild(noteNode).textContent.trim();
const text = [...noveltextNode.childNodes].map(x => {
if (x.nodeType === Node.TEXT_NODE) {
return x.textContent.replace(/ /g,'<SP!>>').trim().replace(/<SP!>>/g, ' ').trimRight();
}
if (x.tagName.toUpperCase() === 'BR') return '\n';
return null;
}).filter(x => x).join('');
return `## ${title}\n\n${text}\n\n${note?`--------\n${note}\n\n`:''}`;
}
async function getListContent(list) {
const result = [], length = list.length;
for (let index = 0; index < length; index++) {
console.log('%d / %d', index, length);
try {
result.push(await getContent(list[index]));
} catch (e) {
console.error(e);
}
}
return result;
}
async function downloadAsText() {
const content = await getListContent([...document.querySelectorAll('a[itemprop="url"]')])
const text = '\ufeff' + content.join('\n\n========\n\n').replace(/\n/g, '\r\n');
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
const title = document.querySelector('[itemprop="articleSection"]');
link.download = title.textContent + '.txt';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
downloadAsText()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment