diff --git a/plugin/js/parsers/RaeitranslationsParser.js b/plugin/js/parsers/RaeitranslationsParser.js new file mode 100644 index 00000000..1ef64ecd --- /dev/null +++ b/plugin/js/parsers/RaeitranslationsParser.js @@ -0,0 +1,62 @@ +"use strict"; + +parserFactory.register("raeitranslations.com", () => new RaeitranslationsParser()); + +class RaeitranslationsParser extends Parser{ + constructor() { + super(); + } + + async getChapterUrls(dom) { + return [...dom.querySelectorAll("div.chapter-list a")] + .map(this.linkToChapter); + } + + linkToChapter(link) { + return { + sourceUrl: link.href, + title: link.querySelector(".chapter-title").innerText.trim(), + }; + } + + findContent(dom) { + return Parser.findConstrutedContent(dom); + } + + extractTitleImpl(dom) { + return dom.querySelector("h2.title"); + } + + findCoverImageUrl(dom) { + let div = dom.querySelector("div.img.wrapper [style*=background-image]"); + return "https://raeitranslations.com" + util.extractUrlFromBackgroundImage(div); + } + + async fetchChapter(url) { + let restUrl = this.makeRestUrl(url); + let json = (await HttpClient.fetchJson(restUrl)).json; + let content = this.buildHtml(json.currentChapter); + let newDoc = Parser.makeEmptyDocForContent(url); + newDoc.content.appendChild(content); + return newDoc.dom; + } + + makeRestUrl(chapterUrl) { + let path = new URL(chapterUrl).pathname.split("/"); + let restUrl = new URL("https://api.raeitranslations.com/api/chapters/"); + restUrl.searchParams.set("id", path[1]); + restUrl.searchParams.set("num", path[2]); + return restUrl; + } + + buildHtml(json) { + let paragraphs = json.body.replace(/\n/g, "
"); + let html = `
${paragraphs}