Skip to content

Commit

Permalink
Track site changes: https://kakuyomu.jp/
Browse files Browse the repository at this point in the history
See: #1131
  • Loading branch information
dteviot committed Dec 4, 2023
1 parent 2aeba1a commit 20654d1
Showing 1 changed file with 43 additions and 14 deletions.
57 changes: 43 additions & 14 deletions plugin/js/parsers/KakuyomuParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,53 @@ class KakuyomuParser extends Parser{
super();
}

getChapterUrls(dom) {
let chapters = [...dom.querySelectorAll("a.widget-toc-episode-episodeTitle")]
.map(link => ({
sourceUrl: link.href,
title: link.querySelector(".widget-toc-episode-titleLabel").textContent.trim()
}));
return Promise.resolve(chapters);
async getChapterUrls(dom) {
return this.buildToc(dom);
}

buildToc(dom) {
let script = dom.querySelector("script#__NEXT_DATA__").innerHTML;
let json = JSON.parse(script).props.pageProps.__APOLLO_STATE__;
let work = json["Work:" + this.extractWorkId(dom)];
let chapters = []
for(let tocc of work.tableOfContents) {
this.buildSubToc(chapters, json[tocc.__ref], json, dom.baseURI);
}
return chapters;
}

extractWorkId(dom) {
let url = dom.baseURI;
let index = url.lastIndexOf("/");
return url.substring(index + 1);
}

buildSubToc(chapters, tocc, json, baseURI) {
let chapter = json[tocc.chapter.__ref];
let arcStart = true;
for(let episoderef of tocc.episodes) {
let episode = this.buildEpisode(json[episoderef.__ref], baseURI);
if (arcStart) {
episode.newArc = chapter.title;
arcStart = false;
}
chapters.push(episode);
}
}

buildEpisode(episode, baseURI) {
return ({
sourceUrl: baseURI + "/episodes/" + episode.id,
title: episode.title,
});
}

findContent(dom) {
return dom.querySelector("div.widget-episode");
}

extractTitleImpl(dom) {
return dom.querySelector("#workTitle");
}

extractAuthor(dom) {
let authorLabel = dom.querySelector("#workAuthor-activityName");
return (authorLabel === null) ? super.extractAuthor(dom) : authorLabel.textContent;
return dom.querySelector("a[title]");
}

findChapterTitle(dom) {
Expand All @@ -43,6 +70,8 @@ class KakuyomuParser extends Parser{
}

getInformationEpubItemChildNodes(dom) {
return [...dom.querySelectorAll("p#introduction")];
let info = [...dom.querySelectorAll("div")]
.filter(i => i.className.startsWith("CollapseTextWith"));
return (0 < info.length) ? [info[0]] : [];
}
}

0 comments on commit 20654d1

Please sign in to comment.