From 1c86f389f655d53b0cddc19930a93478f9563070 Mon Sep 17 00:00:00 2001 From: dteviot Date: Sun, 16 Jun 2024 15:50:24 +1200 Subject: [PATCH] Add site https://www.reddit.com/ See: https://github.com/dteviot/WebToEpub/issues/1344 --- plugin/js/parsers/RedditParser.js | 36 +++++++++++++++++++++++++++++++ plugin/popup.html | 1 + 2 files changed, 37 insertions(+) create mode 100644 plugin/js/parsers/RedditParser.js diff --git a/plugin/js/parsers/RedditParser.js b/plugin/js/parsers/RedditParser.js new file mode 100644 index 00000000..2b29d511 --- /dev/null +++ b/plugin/js/parsers/RedditParser.js @@ -0,0 +1,36 @@ +"use strict"; + +parserFactory.register("reddit.com", () => new RedditParser()); + +class RedditParser extends Parser{ + constructor() { + super(); + } + + async getChapterUrls(dom) { + return [...dom.querySelectorAll("div.wiki a")] + .filter(RedditParser.IsChapterLink) + .map(a => util.hyperLinkToChapter(a)); + } + + findContent(dom) { + return RedditParser.getPost(dom)?.querySelector("[slot='text-body']"); + } + + extractTitleImpl(dom) { + return dom.querySelector(".toc a"); + } + + static IsChapterLink(link) { + let pathname = new URL(link.href).pathname; + return pathname.startsWith("/r/HFY/comments/"); + } + + static getPost(dom) { + return dom.querySelector("main shreddit-post"); + } + + findChapterTitle(dom) { + return RedditParser.getPost(dom).querySelector("h1"); + } +} diff --git a/plugin/popup.html b/plugin/popup.html index 9ea66221..a0d74891 100644 --- a/plugin/popup.html +++ b/plugin/popup.html @@ -712,6 +712,7 @@

Instructions

+