fix: support full text preview (#4057)

This commit is contained in:
Jeason Lau 2020-02-25 00:10:12 +08:00 committed by GitHub
parent 6c93b86e4f
commit 5429e46157
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 6 deletions

View File

@ -1,12 +1,12 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const baseUrl = 'https://www.daocaorenshuwu.com/book';
const baseUrl = 'https://www.daocaorenshuwu.com';
module.exports = async (ctx) => {
const name = ctx.params.name;
const count = ctx.params.count || 3;
const novelUrl = `${baseUrl}/${name}`;
const novelUrl = `${baseUrl}/book/${name}`;
const response = await got({
method: 'get',
url: novelUrl,
@ -29,10 +29,26 @@ module.exports = async (ctx) => {
const chapterTitle = $('a').text();
const chapterUrl = 'https:' + $('a').attr('href');
const chapterContent = await ctx.cache.tryGet(chapterUrl, async () => {
const chapter = await got.get(chapterUrl);
const $ = cheerio.load(chapter.data);
$('div.cont-text > script').remove();
return $('div.cont-text').html();
let result = '';
let temp = chapterUrl;
while (temp !== '') {
// eslint-disable-next-line no-await-in-loop
const chapter = await got.get(temp);
const $ = cheerio.load(chapter.data);
$('div.cont-text > script').remove();
result += $('div.cont-text').html();
// 如果有下一页则访问下一页
if ($('a:contains("下一页")').length > 0) {
temp =
baseUrl +
$('a:contains("下一页")')
.last()
.attr('href');
} else {
temp = '';
}
}
return result;
});
return Promise.resolve({
title: chapterTitle,