[ehentai] support showing comments in details page

This commit is contained in:
2024-11-20 18:07:27 +08:00
parent 0613a6f66a
commit 3856f6996b
3 changed files with 34 additions and 25 deletions

View File

@@ -699,7 +699,7 @@ class HtmlElement {
doc: this.doc, doc: this.doc,
}) })
if(k == null) return null; if(k == null) return null;
return new HtmlElement(k); return new HtmlElement(k, this.doc);
} }
/** /**
@@ -850,6 +850,7 @@ let console = {
* @param id {string} * @param id {string}
* @param title {string} * @param title {string}
* @param subtitle {string} * @param subtitle {string}
* @param subTitle {string} - equal to subtitle
* @param cover {string} * @param cover {string}
* @param tags {string[]} * @param tags {string[]}
* @param description {string} * @param description {string}
@@ -859,10 +860,11 @@ let console = {
* @param stars {number?} - 0-5, double * @param stars {number?} - 0-5, double
* @constructor * @constructor
*/ */
function Comic({id, title, subtitle, cover, tags, description, maxPage, language, favoriteId, stars}) { function Comic({id, title, subtitle, subTitle, cover, tags, description, maxPage, language, favoriteId, stars}) {
this.id = id; this.id = id;
this.title = title; this.title = title;
this.subtitle = subtitle; this.subtitle = subtitle;
this.subTitle = subTitle;
this.cover = cover; this.cover = cover;
this.tags = tags; this.tags = tags;
this.description = description; this.description = description;
@@ -878,8 +880,8 @@ function Comic({id, title, subtitle, cover, tags, description, maxPage, language
* @param cover {string} * @param cover {string}
* @param description {string?} * @param description {string?}
* @param tags {Map<string, string[]> | {} | null | undefined} * @param tags {Map<string, string[]> | {} | null | undefined}
* @param chapters {Map<string, string> | {} | null | undefined}} - key: chapter id, value: chapter title * @param chapters {Map<string, string> | {} | null | undefined} - key: chapter id, value: chapter title
* @param isFavorite {boolean | null | undefined}} - favorite status. If the comic source supports multiple folders, this field should be null * @param isFavorite {boolean | null | undefined} - favorite status. If the comic source supports multiple folders, this field should be null
* @param subId {string?} - a param which is passed to comments api * @param subId {string?} - a param which is passed to comments api
* @param thumbnails {string[]?} - for multiple page thumbnails, set this to null, and use `loadThumbnails` api to load thumbnails * @param thumbnails {string[]?} - for multiple page thumbnails, set this to null, and use `loadThumbnails` api to load thumbnails
* @param recommend {Comic[]?} - related comics * @param recommend {Comic[]?} - related comics
@@ -892,9 +894,10 @@ function Comic({id, title, subtitle, cover, tags, description, maxPage, language
* @param url {string?} * @param url {string?}
* @param stars {number?} - 0-5, double * @param stars {number?} - 0-5, double
* @param maxPage {number?} * @param maxPage {number?}
* @param comments {Comment[]?}- `since 1.0.7` App will display comments in the details page.
* @constructor * @constructor
*/ */
function ComicDetails({title, cover, description, tags, chapters, isFavorite, subId, thumbnails, recommend, commentCount, likesCount, isLiked, uploader, updateTime, uploadTime, url, stars, maxPage}) { function ComicDetails({title, cover, description, tags, chapters, isFavorite, subId, thumbnails, recommend, commentCount, likesCount, isLiked, uploader, updateTime, uploadTime, url, stars, maxPage, comments}) {
this.title = title; this.title = title;
this.cover = cover; this.cover = cover;
this.description = description; this.description = description;
@@ -913,6 +916,7 @@ function ComicDetails({title, cover, description, tags, chapters, isFavorite, su
this.url = url; this.url = url;
this.stars = stars; this.stars = stars;
this.maxPage = maxPage; this.maxPage = maxPage;
this.comments = comments;
} }
/** /**

View File

@@ -7,7 +7,7 @@ class Ehentai extends ComicSource {
// unique id of the source // unique id of the source
key = "ehentai" key = "ehentai"
version = "1.0.9" version = "1.0.10"
minAppVersion = "1.0.0" minAppVersion = "1.0.0"
@@ -681,6 +681,7 @@ class Ehentai extends ComicSource {
if(subtitle != null && subtitle.trim() === "") { if(subtitle != null && subtitle.trim() === "") {
subtitle = null; subtitle = null;
} }
let comments = this.comic.parseComments(document)
let comic = new ComicDetails({ let comic = new ComicDetails({
id: id, id: id,
@@ -694,6 +695,7 @@ class Ehentai extends ComicSource {
uploader: uploader, uploader: uploader,
uploadTime: time, uploadTime: time,
url: id, url: id,
comments: comments.comments,
}) })
comic.folder = folder comic.folder = folder
@@ -957,22 +959,7 @@ class Ehentai extends ComicSource {
} }
} }
}, },
/** parseComments: (document) => {
* [Optional] load comments
* @param comicId {string}
* @param subId {string?} - ComicDetails.subId
* @param page {number}
* @param replyTo {string?} - commentId to reply, not null when reply to a comment
* @returns {Promise<{comments: Comment[], maxPage: number?}>}
*/
loadComments: async (comicId, subId, page, replyTo) => {
let res = await Network.get(`${comicId}?hc=1`, {
'cookie': 'nw=1'
});
if(res.status !== 200) {
throw `Invalid status code: ${res.status}`
}
let document = new HtmlDocument(res.body)
let comments = [] let comments = []
for(let c of document.querySelectorAll('div.c1')) { for(let c of document.querySelectorAll('div.c1')) {
let name = c.querySelector('div.c3 > a').text let name = c.querySelector('div.c3 > a').text
@@ -1002,13 +989,31 @@ class Ehentai extends ComicSource {
})) }))
} }
document.dispose()
return { return {
comments: comments, comments: comments,
maxPage: 1 maxPage: 1
} }
}, },
/**
* [Optional] load comments
* @param comicId {string}
* @param subId {string?} - ComicDetails.subId
* @param page {number}
* @param replyTo {string?} - commentId to reply, not null when reply to a comment
* @returns {Promise<{comments: Comment[], maxPage: number?}>}
*/
loadComments: async (comicId, subId, page, replyTo) => {
let res = await Network.get(`${comicId}?hc=1`, {
'cookie': 'nw=1'
});
if(res.status !== 200) {
throw `Invalid status code: ${res.status}`
}
let document = new HtmlDocument(res.body)
let result = this.comic.parseComments(document)
document.dispose()
return result
},
/** /**
* [Optional] send a comment, return any value to indicate success * [Optional] send a comment, return any value to indicate success
* @param comicId {string} * @param comicId {string}

View File

@@ -45,7 +45,7 @@
"name": "ehentai", "name": "ehentai",
"fileName": "ehentai.js", "fileName": "ehentai.js",
"key": "ehentai", "key": "ehentai",
"version": "1.0.9" "version": "1.0.10"
}, },
{ {
"name": "禁漫天堂", "name": "禁漫天堂",