BlueGit

timfa hfy-novels

Files

hfy-novels / plugin / hfy-novels.js

hfy-novels.js

hfy-novels.js 48.8 KiB Download Raw
1"use strict";
2
3/**
4 * LNReader-plugin voor r/HFY.
5 *
6 * De catalogus wordt live opgebouwd op de telefoon, met de Reddit-sessie
7 * uit de LNReader-WebView (elk account heeft zijn eigen cookies):
8 * - popularNovels: de wiki-serie-index, plus 'Nieuw' / Latest met recente OC-posts
9 * - searchNovels: r/HFY/search.json (restrict_sr=1), plus hits uit de wiki,
10 * waarbij series en losse Part-1-posts tot één novel worden gegroepeerd
11 * - parseNovel: wiki-inhoudsopgave, anders next-links met titelgelijkenis
12 *
13 * Bij een fout, 403 of login-/blokpagina valt alles terug op Arctic Shift,
14 * en daarna op de statische metadata-index (ook bruikbaar zonder login).
15 * parseChapter probeert eerst Reddit-JSON, daarna Arctic Shift.
16 *
17 * Dit bestand is CommonJS, zoals de officiële gecompileerde plugins:
18 * require(...) uit de LNReader-allowlist, en `exports.default = instance`.
19 */
20
21Object.defineProperty(exports, "__esModule", { value: true });
22
23const { fetchApi } = require("@libs/fetch");
24const { FilterTypes } = require("@libs/filterInputs");
25const { defaultCover } = require("@libs/defaultCover");
26const { NovelStatus } = require("@libs/novelStatus");
27const { storage } = require("@libs/storage");
28
29const DEFAULT_INDEX_BASE =
30 "https://git.timfalken.com/timfa/hfy-novels/-/raw/public";
31const DEFAULT_INDEX_REV = "master";
32const DEFAULT_CHAPTER_SOURCE = "https://arctic-shift.photon-reddit.com";
33const PAGE_FALLBACK_SIZE = 50;
34const WIKI_CACHE_KEY = "wikiSeriesIndex";
35const WIKI_CACHE_MS = 6 * 60 * 60 * 1000;
36const REDDIT_WWW = "https://www.reddit.com";
37const REDDIT_OLD = "https://old.reddit.com";
38const WIKI_INDEX_PATH = "/r/HFY/wiki/series";
39
40function setting(key, fallback) {
41 try {
42 const value = storage.get(key);
43 if (value === undefined || value === null || value === "") {
44 return fallback;
45 }
46 return value;
47 } catch (err) {
48 return fallback;
49 }
50}
51
52function joinUrl(base, path, query) {
53 const trimmedBase = String(base || "").replace(/\/+$/, "");
54 const trimmedPath = String(path || "").replace(/^\/+/, "");
55 let url = trimmedBase + "/" + trimmedPath;
56 const params = [];
57 if (query && typeof query === "object") {
58 for (const [k, v] of Object.entries(query)) {
59 if (v !== undefined && v !== null && v !== "") {
60 params.push(encodeURIComponent(k) + "=" + encodeURIComponent(String(v)));
61 }
62 }
63 }
64 if (params.length) {
65 url += (url.includes("?") ? "&" : "?") + params.join("&");
66 }
67 return url;
68}
69
70function statusFrom(text) {
71 switch (text) {
72 case "Completed":
73 return NovelStatus.Completed;
74 case "Ongoing":
75 return NovelStatus.Ongoing;
76 case "On Hiatus":
77 return NovelStatus.OnHiatus;
78 default:
79 return NovelStatus.Unknown;
80 }
81}
82
83async function readJson(url, label) {
84 const res = await fetchApi(url);
85 if (!res || res.ok === false) {
86 const status = res && res.status != null ? res.status : "?";
87 throw new Error("HFY Novels: kon " + label + " niet laden (HTTP " + status + ")");
88 }
89 if (typeof res.json === "function") {
90 return await res.json();
91 }
92 const text = await res.text();
93 return JSON.parse(text);
94}
95
96function toNovelItem(entry, plugin) {
97 return {
98 name: entry.name,
99 path: entry.path,
100 cover: plugin.coverUrl(entry.cover),
101 };
102}
103
104class HFYNovelsPlugin {
105 constructor() {
106 this.id = "hfyNovels";
107 this.name = "HFY Novels";
108 this.version = "0.3.0";
109 this.site = "https://www.reddit.com/r/HFY/";
110 this.icon = "icon.png";
111 // Geen webStorageUtilized: Reddit-login zit in cookies. LNReader's
112 // fetchApi (native fetch) stuurt de WebView-cookies automatisch mee
113 // voor hetzelfde domein; localStorage is daarvoor niet nodig.
114 this.filters = {
115 catalog: {
116 value: "wiki",
117 label: "Overzicht",
118 options: [
119 { label: "Wiki-series", value: "wiki" },
120 { label: "Nieuw", value: "new" },
121 ],
122 type: FilterTypes.Picker,
123 },
124 kind: {
125 value: "all",
126 label: "Type",
127 options: [
128 { label: "Alles", value: "all" },
129 { label: "Reeksen", value: "series" },
130 { label: "Oneshots", value: "oneshot" },
131 ],
132 type: FilterTypes.Picker,
133 },
134 nsfw: {
135 value: "hide",
136 label: "NSFW",
137 options: [
138 { label: "Verbergen", value: "hide" },
139 { label: "Tonen", value: "show" },
140 ],
141 type: FilterTypes.Picker,
142 },
143 };
144 this.pluginSettings = {
145 indexBaseUrl: {
146 value: DEFAULT_INDEX_BASE,
147 label: "Basis-URL van de statische index (laatste fallback)",
148 type: "Text",
149 },
150 indexRev: {
151 value: DEFAULT_INDEX_REV,
152 label: "Git-revisie van de index (rev=)",
153 type: "Text",
154 },
155 chapterSource: {
156 value: "auto",
157 label: "Bron voor catalogus en hoofdstukken",
158 type: "Select",
159 options: [
160 { label: "Automatisch (Reddit, anders Arctic Shift, anders index)", value: "auto" },
161 { label: "Alleen Reddit", value: "reddit" },
162 { label: "Alleen Arctic Shift (daarna index)", value: "arctic" },
163 ],
164 },
165 chapterSourceUrl: {
166 value: DEFAULT_CHAPTER_SOURCE,
167 label: "Arctic Shift-basis-URL (fallback)",
168 type: "Text",
169 },
170 };
171 }
172
173 indexQuery() {
174 const rev = setting("indexRev", DEFAULT_INDEX_REV);
175 return { rev: rev };
176 }
177
178 indexUrl(path) {
179 const base = setting("indexBaseUrl", DEFAULT_INDEX_BASE);
180 return joinUrl(base, path, this.indexQuery());
181 }
182
183 coverUrl(cover) {
184 if (!cover) {
185 return defaultCover;
186 }
187 if (/^https?:\/\//i.test(cover)) {
188 return cover;
189 }
190 let url = this.indexUrl(cover);
191 // BlueGit serveert SVG pas als afbeelding met ?as=img.
192 if (/\.svg$/i.test(cover) && url.indexOf("as=img") === -1) {
193 url += (url.indexOf("?") === -1 ? "?" : "&") + "as=img";
194 }
195 return url;
196 }
197
198 chapterSource() {
199 return String(setting("chapterSourceUrl", DEFAULT_CHAPTER_SOURCE)).replace(/\/+$/, "");
200 }
201
202 chapterMode() {
203 const mode = String(setting("chapterSource", "auto") || "auto").toLowerCase();
204 if (mode === "reddit" || mode === "arctic") {
205 return mode;
206 }
207 return "auto";
208 }
209
210 /**
211 * Maakt "Open in WebView" op een reeks of hoofdstuk tot een Reddit-URL,
212 * zodat je daar kunt inloggen. Zonder deze functie zou LNReader
213 * site+path plakken (bijv. /r/HFY/series/id), wat geen geldige Reddit-pagina is.
214 * Het reeks-id is de oudste post (eerste deel), of een wiki-slug.
215 */
216 resolveUrl(path, isNovel) {
217 const raw = String(path || "").trim();
218 if (/^https?:\/\//i.test(raw)) {
219 return raw;
220 }
221 const wikiSlug = wikiSlugFromPath(raw);
222 if (wikiSlug) {
223 return REDDIT_WWW + "/r/HFY/wiki/series/" + wikiSlug + "/";
224 }
225 const id = chapterIdFromPath(raw) || chapterIdFromPath(raw.replace(/^series\//, "chapter/"));
226 if (id) {
227 return REDDIT_WWW + "/r/HFY/comments/" + id + "/";
228 }
229 return this.site;
230 }
231
232 matchesFilters(entry, filters) {
233 const kind = filters && filters.kind ? filters.kind.value : "all";
234 const nsfw = filters && filters.nsfw ? filters.nsfw.value : "hide";
235 if (kind === "oneshot" && !entry.oneshot) {
236 return false;
237 }
238 if (kind === "series" && entry.oneshot) {
239 return false;
240 }
241 if (nsfw === "hide" && entry.nsfw) {
242 return false;
243 }
244 return true;
245 }
246
247 async loadMeta() {
248 return await readJson(this.indexUrl("catalog-meta.json"), "catalogus-meta");
249 }
250
251 async popularNovels(pageNo, options) {
252 const opts = options || {};
253 const filters = opts.filters || {};
254 const showLatest = !!opts.showLatestNovels || (filters.catalog && filters.catalog.value === "new");
255 const page = Math.max(1, parseInt(pageNo, 10) || 1);
256 const mode = this.chapterMode();
257
258 if (showLatest) {
259 const live = await this.liveLatestNovels(page, filters, mode);
260 if (live && live.ok) {
261 return live.items;
262 }
263 if (mode === "reddit") {
264 throw new Error(
265 "HFY Novels: Reddit gaf geen nieuwe posts (" +
266 ((live && live.reason) || "onbekende fout") +
267 "). Log in via Open in WebView op reddit.com en probeer het opnieuw.",
268 );
269 }
270 return await this.staticPopular(page, opts);
271 }
272
273 const live = await this.liveWikiNovels(page, filters, mode);
274 if (live && live.ok) {
275 return live.items;
276 }
277 if (mode === "reddit") {
278 throw new Error(
279 "HFY Novels: Reddit gaf de wiki-index niet (" +
280 ((live && live.reason) || "onbekende fout") +
281 "). Log in via Open in WebView op reddit.com en probeer het opnieuw.",
282 );
283 }
284 return await this.staticPopular(page, opts);
285 }
286
287 async searchNovels(searchTerm, pageNo) {
288 const term = String(searchTerm || "").trim();
289 if (!term) {
290 return [];
291 }
292 const page = Math.max(1, parseInt(pageNo, 10) || 1);
293 const mode = this.chapterMode();
294 const live = await this.liveSearchNovels(term, page, mode);
295 if (live && live.ok) {
296 return live.items;
297 }
298 if (mode === "reddit") {
299 throw new Error(
300 "HFY Novels: Reddit-zoeken mislukte (" +
301 ((live && live.reason) || "onbekende fout") +
302 "). Log in via Open in WebView op reddit.com en probeer het opnieuw.",
303 );
304 }
305 return await this.staticSearch(term, page);
306 }
307
308 seriesFileFor(novelPath) {
309 const id = String(novelPath || "")
310 .replace(/^series\//, "")
311 .replace(/\/+$/, "");
312 if (!id) {
313 throw new Error("HFY Novels: leeg reeks-pad");
314 }
315 const prefix = id.charAt(0).toLowerCase() || "x";
316 return "series/" + prefix + "/" + id + ".json";
317 }
318
319 async parseNovel(novelPath) {
320 const mode = this.chapterMode();
321 const live = await this.liveParseNovel(novelPath, mode);
322 if (mode === "reddit" && (!live || !live.ok)) {
323 throw new Error(
324 "HFY Novels: Reddit gaf de hoofdstuklijst niet (" +
325 ((live && live.reason) || "onbekende fout") +
326 "). Log in via Open in WebView op reddit.com en probeer het opnieuw.",
327 );
328 }
329 let novel = live && live.ok ? live.novel : null;
330 if (!wikiSlugFromPath(novelPath)) {
331 try {
332 const fallback = await this.staticParseNovel(novelPath);
333 if (!novel) {
334 return fallback;
335 }
336 // De statische index kan een langere inhoudsopgave hebben dan een
337 // enkele live-post zonder werkende next-link; neem dan die lijst.
338 if ((fallback.chapters || []).length > (novel.chapters || []).length) {
339 novel.chapters = fallback.chapters;
340 if (fallback.cover) {
341 novel.cover = fallback.cover;
342 }
343 if (!novel.summary && fallback.summary) {
344 novel.summary = fallback.summary;
345 }
346 }
347 return novel;
348 } catch (err) {
349 if (novel) {
350 return novel;
351 }
352 throw err;
353 }
354 }
355 if (novel) {
356 return novel;
357 }
358 throw new Error(
359 "HFY Novels: kon de reeks " +
360 novelPath +
361 " niet live bepalen (" +
362 ((live && live.reason) || "geen bron") +
363 ").",
364 );
365 }
366
367 async parseChapter(chapterPath) {
368 const id = chapterIdFromPath(chapterPath);
369 if (!id) {
370 throw new Error("HFY Novels: ongeldig hoofdstuk-id");
371 }
372 const mode = this.chapterMode();
373 let redditError = "";
374 if (mode !== "arctic") {
375 const reddit = await tryRedditChapter(id);
376 if (reddit.ok) {
377 return renderChapter(reddit.post, "Reddit");
378 }
379 redditError = reddit.reason || "onbekende fout";
380 if (mode === "reddit") {
381 throw new Error(
382 "HFY Novels: Reddit gaf geen hoofdstuk (" +
383 redditError +
384 "). Log in via Open in WebView op reddit.com en probeer het opnieuw.",
385 );
386 }
387 }
388 const arctic = await tryArcticChapter(this.chapterSource(), id);
389 if (arctic.ok) {
390 const suffix = mode === "arctic" ? "Arctic Shift" : "Arctic Shift (fallback)";
391 return renderChapter(arctic.post, suffix);
392 }
393 if (redditError) {
394 throw new Error(
395 "HFY Novels: Reddit (" +
396 redditError +
397 ") en Arctic Shift (" +
398 (arctic.reason || "geen tekst") +
399 ") faalden allebei.",
400 );
401 }
402 throw new Error("HFY Novels: kon hoofdstuk " + id + " niet laden (" + (arctic.reason || "geen tekst") + ")");
403 }
404
405 async staticPopular(pageNo, options) {
406 const opts = options || {};
407 const filters = opts.filters || {};
408 const showLatest = !!opts.showLatestNovels || (filters.catalog && filters.catalog.value === "new");
409 const page = Math.max(1, parseInt(pageNo, 10) || 1);
410 const usePagedCatalog =
411 !showLatest &&
412 (!filters.kind || filters.kind.value === "all") &&
413 (!filters.nsfw || filters.nsfw.value === "hide");
414
415 if (usePagedCatalog) {
416 const meta = await this.loadMeta();
417 const pages = meta.pages || [];
418 const spec = pages[page - 1];
419 if (!spec) {
420 return [];
421 }
422 const payload = await readJson(this.indexUrl(spec.path), "cataloguspagina " + page);
423 const novels = (payload.novels || []).filter((n) => !n.nsfw);
424 return novels.map((n) => toNovelItem(n, this));
425 }
426
427 const search = await readJson(this.indexUrl("search-index.json"), "zoekindex");
428 const items = (search.novels || []).filter((n) => this.matchesFilters(n, filters));
429 const size = (await this.loadMeta()).pageSize || PAGE_FALLBACK_SIZE;
430 const start = (page - 1) * size;
431 return items.slice(start, start + size).map((n) => toNovelItem(n, this));
432 }
433
434 async staticSearch(searchTerm, pageNo) {
435 const term = String(searchTerm || "").trim().toLowerCase();
436 if (!term) {
437 return [];
438 }
439 const page = Math.max(1, parseInt(pageNo, 10) || 1);
440 const search = await readJson(this.indexUrl("search-index.json"), "zoekindex");
441 const hits = (search.novels || []).filter((n) => {
442 const name = String(n.name || "").toLowerCase();
443 const author = String(n.author || "").toLowerCase();
444 return name.includes(term) || author.includes(term);
445 });
446 const start = (page - 1) * PAGE_FALLBACK_SIZE;
447 return hits.slice(start, start + PAGE_FALLBACK_SIZE).map((n) => toNovelItem(n, this));
448 }
449
450 async staticParseNovel(novelPath) {
451 const data = await readJson(this.indexUrl(this.seriesFileFor(novelPath)), "reeks " + novelPath);
452 return {
453 path: data.path || novelPath,
454 name: data.name || "Untitled",
455 author: data.author || "Anonymous",
456 artist: undefined,
457 cover: this.coverUrl(data.cover),
458 genres: data.genres || "HFY",
459 status: statusFrom(data.status),
460 summary: data.summary || "",
461 chapters: (data.chapterList || []).map((ch) => ({
462 name: ch.name,
463 path: ch.path,
464 releaseTime: ch.releaseTime || null,
465 chapterNumber: ch.chapterNumber,
466 })),
467 };
468 }
469
470 async liveWikiNovels(page, filters, mode) {
471 const fetched = await this.loadWikiIndex(mode);
472 if (!fetched.ok) {
473 return fetched;
474 }
475 let items = fetched.entries.map((entry) => wikiEntryToItem(entry, this));
476 items = items.filter((entry) => this.matchesFilters(entry, filters));
477 const start = (page - 1) * PAGE_FALLBACK_SIZE;
478 return { ok: true, items: items.slice(start, start + PAGE_FALLBACK_SIZE), source: fetched.source };
479 }
480
481 async liveLatestNovels(page, filters, mode) {
482 const fetched = await this.loadRecentOcPosts(page, mode);
483 if (!fetched.ok) {
484 return fetched;
485 }
486 const grouped = groupPostsIntoNovels(fetched.posts, fetched.wikiEntries || []);
487 const items = grouped
488 .filter((entry) => this.matchesFilters(entry, filters))
489 .map((entry) => toNovelItem(entry, this));
490 return { ok: true, items: items, source: fetched.source };
491 }
492
493 async liveSearchNovels(term, page, mode) {
494 const wiki = await this.loadWikiIndex(mode === "reddit" ? "reddit" : "auto");
495 const wikiHits = (wiki.ok ? wiki.entries : []).filter((entry) => wikiEntryMatches(entry, term));
496 const postsRes = await this.loadSearchPosts(term, page, mode);
497 if (!postsRes.ok && !wiki.ok) {
498 return postsRes.ok === false ? postsRes : wiki;
499 }
500 const grouped = groupPostsIntoNovels(postsRes.ok ? postsRes.posts : [], wiki.ok ? wiki.entries : []);
501 const byKey = {};
502 const items = [];
503 function addItem(entry) {
504 const key = (entry.path || "") + "|" + normalizeTitle(entry.name);
505 if (byKey[key]) {
506 return;
507 }
508 byKey[key] = true;
509 items.push(toNovelItem(entry, this));
510 }
511 const add = addItem.bind(this);
512 wikiHits.forEach((entry) => add(wikiEntryToItem(entry, this)));
513 grouped.forEach((entry) => add(entry));
514 if (!items.length && !postsRes.ok) {
515 return { ok: false, reason: postsRes.reason || (wiki.ok ? "geen treffers" : wiki.reason) };
516 }
517 const start = (page - 1) * PAGE_FALLBACK_SIZE;
518 return { ok: true, items: items.slice(start, start + PAGE_FALLBACK_SIZE) };
519 }
520
521 async liveParseNovel(novelPath, mode) {
522 const wikiSlug = wikiSlugFromPath(novelPath);
523 const startId = chapterIdFromPath(novelPath) || chapterIdFromPath(String(novelPath).replace(/^series\//, "chapter/"));
524 let entry = null;
525 let reason = "";
526
527 if (wikiSlug) {
528 const page = await this.loadWikiPage(wikiSlug, mode);
529 if (page.ok) {
530 entry = page.entry;
531 } else {
532 reason = page.reason || "wiki-subpagina ontbreekt";
533 if (mode === "reddit") {
534 return { ok: false, reason: reason };
535 }
536 }
537 }
538
539 const seedId = (entry && (entry.firstId || (entry.chapterIds && entry.chapterIds[0]))) || startId;
540 const wanted = uniqueIds([].concat((entry && entry.chapterIds) || [], seedId ? [seedId] : []));
541 const fetched = await this.loadPostsByIds(wanted, mode);
542 if (!fetched.ok && !fetched.posts.length) {
543 return { ok: false, reason: reason || fetched.reason || "geen posts" };
544 }
545 const byId = {};
546 fetched.posts.forEach((post) => {
547 const id = postId(post);
548 if (id) {
549 byId[id] = post;
550 }
551 });
552 const toc = wanted.map((id) => byId[id]).filter(Boolean);
553 let members = toc.slice();
554 const startPost = toc.length ? toc[toc.length - 1] : byId[seedId];
555 if (startPost) {
556 const tail = await this.followChain(startPost, byId, mode);
557 const seen = {};
558 members.forEach((p) => {
559 seen[postId(p)] = true;
560 });
561 tail.forEach((p) => {
562 const id = postId(p);
563 if (id && !seen[id]) {
564 seen[id] = true;
565 members.push(p);
566 }
567 });
568 }
569 if (!members.length) {
570 return { ok: false, reason: reason || fetched.reason || "geen hoofdstukken" };
571 }
572 members = members.slice().sort(comparePosts);
573 const first = members[0];
574 const name = (entry && entry.title) || guessTitle(members.map((p) => p.title)) || first.title || "Untitled";
575 const author = (entry && entry.author) || first.author || "Anonymous";
576 const path = startId ? "series/" + postId(first) : wikiSlug ? "wiki/" + wikiSlug : "series/" + postId(first);
577 const nsfw = members.some((p) => p.over_18 || p.over18);
578 const oneshot = members.length <= 1;
579 return {
580 ok: true,
581 novel: {
582 path: path,
583 name: name,
584 author: author,
585 artist: undefined,
586 cover: this.coverUrl(null),
587 genres: nsfw ? "HFY, NSFW" : "HFY",
588 status: oneshot ? NovelStatus.Completed : NovelStatus.Unknown,
589 summary: author ? "HFY-serie van u/" + author + "." : "",
590 chapters: members.map((p, i) => ({
591 name: p.title || "Chapter " + (i + 1),
592 path: "chapter/" + postId(p),
593 releaseTime: releaseTimeOf(p),
594 chapterNumber: i + 1,
595 })),
596 },
597 };
598 }
599
600 async loadWikiIndex(mode) {
601 const cached = readWikiCache();
602 if (cached && cached.entries && cached.entries.length) {
603 return { ok: true, entries: cached.entries, source: "cache" };
604 }
605 if (mode !== "arctic") {
606 const reddit = await fetchRedditWikiMarkdown(WIKI_INDEX_PATH);
607 if (reddit.ok) {
608 const entries = parseSeriesIndex(reddit.markdown);
609 if (entries.length) {
610 writeWikiCache(entries);
611 return { ok: true, entries: entries, source: "reddit" };
612 }
613 }
614 if (mode === "reddit") {
615 return { ok: false, reason: (reddit && reddit.reason) || "lege wiki-index" };
616 }
617 }
618 const arctic = await fetchArcticWikiPage(this.chapterSource(), WIKI_INDEX_PATH);
619 if (arctic.ok) {
620 const entries = parseSeriesIndex(arctic.markdown);
621 if (entries.length) {
622 writeWikiCache(entries);
623 return { ok: true, entries: entries, source: "arctic" };
624 }
625 }
626 return { ok: false, reason: (arctic && arctic.reason) || "wiki-index niet beschikbaar" };
627 }
628
629 async loadWikiPage(slug, mode) {
630 const path = WIKI_INDEX_PATH + "/" + slug;
631 if (mode !== "arctic") {
632 const reddit = await fetchRedditWikiMarkdown(path);
633 if (reddit.ok) {
634 return { ok: true, entry: parseSeriesPage(reddit.markdown, path), source: "reddit" };
635 }
636 if (mode === "reddit") {
637 return reddit;
638 }
639 }
640 const arctic = await fetchArcticWikiPage(this.chapterSource(), path);
641 if (arctic.ok) {
642 return { ok: true, entry: parseSeriesPage(arctic.markdown, path), source: "arctic" };
643 }
644 return { ok: false, reason: (arctic && arctic.reason) || "wiki-subpagina niet beschikbaar" };
645 }
646
647 async loadSearchPosts(term, page, mode) {
648 if (mode !== "arctic") {
649 const reddit = await fetchRedditSearch(term, page);
650 if (reddit.ok) {
651 return reddit;
652 }
653 if (mode === "reddit") {
654 return reddit;
655 }
656 }
657 return await fetchArcticSearch(this.chapterSource(), term);
658 }
659
660 async loadRecentOcPosts(page, mode) {
661 const wiki = await this.loadWikiIndex(mode === "reddit" ? "reddit" : "auto");
662 if (mode !== "arctic") {
663 const reddit = await fetchRedditListing("/r/HFY/new.json", page);
664 if (reddit.ok) {
665 return {
666 ok: true,
667 posts: reddit.posts.filter(isStoryPost),
668 wikiEntries: wiki.ok ? wiki.entries : [],
669 source: "reddit",
670 };
671 }
672 if (mode === "reddit") {
673 return reddit;
674 }
675 }
676 const arctic = await fetchArcticRecent(this.chapterSource());
677 if (arctic.ok) {
678 return {
679 ok: true,
680 posts: (arctic.posts || []).filter(isStoryPost),
681 wikiEntries: wiki.ok ? wiki.entries : [],
682 source: "arctic",
683 };
684 }
685 return arctic;
686 }
687
688 async loadPostsByIds(ids, mode) {
689 const wanted = uniqueIds(ids);
690 if (!wanted.length) {
691 return { ok: true, posts: [], source: "none" };
692 }
693 if (mode !== "arctic") {
694 const reddit = await fetchRedditPostsByIds(wanted);
695 if (reddit.ok) {
696 return reddit;
697 }
698 if (mode === "reddit") {
699 return reddit;
700 }
701 }
702 return await fetchArcticPostsByIds(this.chapterSource(), wanted);
703 }
704
705 async followChain(seed, byId, mode) {
706 const chain = [];
707 const visited = {};
708 let current = seed;
709 let guard = 0;
710 while (current && guard < 400) {
711 const id = postId(current);
712 if (!id || visited[id]) {
713 break;
714 }
715 visited[id] = true;
716 chain.push(current);
717 const nxt = pickNextLink(current);
718 if (!nxt || visited[nxt]) {
719 break;
720 }
721 if (!byId[nxt]) {
722 const fetched = await this.loadPostsByIds([nxt], mode);
723 (fetched.posts || []).forEach((p) => {
724 const pid = postId(p);
725 if (pid) {
726 byId[pid] = p;
727 }
728 });
729 }
730 const candidate = byId[nxt];
731 if (!candidate) {
732 break;
733 }
734 if (!titlesBelongTogether(current.title, candidate.title, current.author, candidate.author, nxt === pickNextLink(current))) {
735 // Expliciete next van dezelfde auteur mag iets lager scoren; anders stoppen.
736 if (!sameAuthor(current, candidate) || titleSimilarity(current.title, candidate.title) < 0.4) {
737 break;
738 }
739 }
740 current = candidate;
741 guard += 1;
742 }
743 return chain;
744 }
745}
746
747function wikiSlugFromPath(novelPath) {
748 const raw = String(novelPath || "").trim();
749 const m = raw.match(/^(?:wiki\/|series\/wiki\/)?(?:r\/hfy\/wiki\/series\/)?([a-z0-9][a-z0-9_-]{1,80})$/i);
750 if (!m) {
751 return "";
752 }
753 const slug = m[1];
754 if (/^[0-9a-z]{4,10}$/i.test(slug) && raw.indexOf("wiki/") === -1) {
755 return "";
756 }
757 if (raw.indexOf("wiki/") === 0 || raw.indexOf("/wiki/series/") !== -1) {
758 return slug;
759 }
760 return "";
761}
762
763function wikiEntryToItem(entry, plugin) {
764 const first = entry.firstId || (entry.chapterIds && entry.chapterIds[0]) || "";
765 const path = first ? "series/" + first : "wiki/" + entry.slug;
766 return {
767 name: entry.title,
768 path: path,
769 cover: plugin.coverUrl(null),
770 author: entry.author || "",
771 oneshot: false,
772 nsfw: false,
773 };
774}
775
776function wikiEntryMatches(entry, term) {
777 const q = String(term || "").trim().toLowerCase();
778 if (!q) {
779 return false;
780 }
781 const title = String(entry.title || "").toLowerCase();
782 const author = String(entry.author || "").toLowerCase();
783 const slug = String(entry.slug || "").toLowerCase().replace(/_/g, " ");
784 return title.indexOf(q) !== -1 || author.indexOf(q) !== -1 || slug.indexOf(q) !== -1;
785}
786
787function readWikiCache() {
788 try {
789 const value = storage.get(WIKI_CACHE_KEY);
790 if (!value) {
791 return null;
792 }
793 if (Array.isArray(value)) {
794 return { entries: value };
795 }
796 if (value.entries && Array.isArray(value.entries)) {
797 return value;
798 }
799 } catch (err) {
800 return null;
801 }
802 return null;
803}
804
805function writeWikiCache(entries) {
806 try {
807 const expires = Date.now() + WIKI_CACHE_MS;
808 storage.set(WIKI_CACHE_KEY, { fetchedAt: Date.now(), entries: entries }, expires);
809 } catch (err) {
810 // cache is optioneel
811 }
812}
813
814function parseSeriesIndex(markdown) {
815 const text = String(markdown || "");
816 const out = [];
817 const seen = {};
818 const re = /^\s*[\*\-]\s*\[([^\]]+)\]\(([^)]+)\)(.*)$/gm;
819 let m;
820 while ((m = re.exec(text))) {
821 const title = cleanHeading(m[1]);
822 const path = normalizeWikiPath(m[2]);
823 if (!path || path.toLowerCase().indexOf("/wiki/series/") === -1) {
824 continue;
825 }
826 const key = path.toLowerCase();
827 if (seen[key]) {
828 continue;
829 }
830 seen[key] = true;
831 const slug = path.split("/").filter(Boolean).pop();
832 out.push({
833 title: title || slug.replace(/_/g, " "),
834 wikiPath: path,
835 slug: slug,
836 author: authorFromRest(m[3] || ""),
837 chapterIds: [],
838 firstId: "",
839 });
840 }
841 return out;
842}
843
844function parseSeriesPage(markdown, wikiPath) {
845 const text = String(markdown || "");
846 const path = normalizeWikiPath(wikiPath) || String(wikiPath || "");
847 const slug = path.split("/").filter(Boolean).pop() || "";
848 const heading = text.match(/^#{1,4}\s*(.+)$/m);
849 let title = heading ? cleanHeading(heading[1]) : slug.replace(/_/g, " ");
850 if (title && /^(all series|series|one shots|oneshots)$/i.test(title)) {
851 title = slug.replace(/_/g, " ");
852 }
853 const ids = [];
854 const seen = {};
855 extractLabeledLinks(text).forEach((link) => {
856 if (!seen[link.id]) {
857 seen[link.id] = true;
858 ids.push(link.id);
859 }
860 });
861 extractIds(text).forEach((id) => {
862 if (!seen[id]) {
863 seen[id] = true;
864 ids.push(id);
865 }
866 });
867 return {
868 title: title,
869 wikiPath: path,
870 slug: slug,
871 author: authorFromPage(text),
872 chapterIds: ids,
873 firstId: ids[0] || "",
874 };
875}
876
877function normalizeWikiPath(href) {
878 if (!href) {
879 return "";
880 }
881 let raw = String(href).trim().split(")")[0].split(/\s+/)[0];
882 raw = raw.replace(/\/wiki\/settings\/series\//i, "/wiki/series/");
883 const m = raw.match(/(?:https?:\/\/(?:www\.)?reddit\.com)?(\/r\/[^/]+\/wiki\/(?:series)\/[^)#\s]+)/i);
884 if (!m) {
885 return "";
886 }
887 const parts = m[1].split("/");
888 if (parts.length >= 3 && parts[1].toLowerCase() === "r") {
889 parts[2] = "hfy";
890 }
891 return parts.join("/").replace(/\/+$/, "").replace(/\/r\/HFY\//i, "/r/hfy/");
892}
893
894function cleanHeading(raw) {
895 let text = String(raw || "").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1");
896 text = text.replace(/[*_`]+/g, "");
897 return text.replace(/\s+/g, " ").trim();
898}
899
900function authorFromRest(rest) {
901 const m = String(rest || "").match(/\[\s*\*([^*]+)\*\s*\]/);
902 if (!m) {
903 return "";
904 }
905 return m[1].replace(/^u\//, "").trim();
906}
907
908function authorFromPage(markdown) {
909 const wiki = String(markdown || "").match(
910 /\[(?:\*\*)?([^\]*]+)(?:\*\*)?\]\((?:https?:\/\/(?:www\.)?reddit\.com)?\/r\/[^/]+\/wiki\/authors\/([^)\s/]+)/i,
911 );
912 if (wiki) {
913 return (wiki[1] || wiki[2] || "").trim();
914 }
915 const by = String(markdown || "").match(/(?:a\s+story\s+by|author)\s*[:.\-–—]?\s*\[(?:\*\*)?([^\]*]+)(?:\*\*)?\]/i);
916 if (by) {
917 return by[1].trim();
918 }
919 const user = String(markdown || "").slice(0, 2000).match(/(?:https?:\/\/(?:www\.)?reddit\.com)?\/u(?:ser)?\/([A-Za-z0-9_-]+)|(?<![/\w])u\/([A-Za-z0-9_-]+)/i);
920 if (user) {
921 return (user[1] || user[2] || "").trim();
922 }
923 return "";
924}
925
926function extractIds(text) {
927 const ids = [];
928 const re = /(?:redd\.it\/|\/(?:r\/[^/]+\/)?comments\/)([0-9a-z]{4,10})\b/gi;
929 let m;
930 while ((m = re.exec(String(text || "")))) {
931 ids.push(m[1].toLowerCase());
932 }
933 return ids;
934}
935
936function extractLabeledLinks(text) {
937 const links = [];
938 const re = /\[([^\]]+)\]\(([^)]+)\)/g;
939 let m;
940 while ((m = re.exec(String(text || "")))) {
941 const id = extractIds(m[2])[0];
942 if (!id) {
943 continue;
944 }
945 const label = String(m[1] || "").toLowerCase();
946 let kind = "other";
947 if (/\b(next|nxt|continue|volgende)\b/.test(label)) {
948 kind = "next";
949 } else if (/\b(prev|previous|vorige)\b/.test(label)) {
950 kind = "prev";
951 } else if (/\b(first|index|eerste)\b/.test(label)) {
952 kind = "first";
953 }
954 links.push({ id: id, kind: kind, label: m[1] });
955 }
956 return links;
957}
958
959function postId(post) {
960 if (!post) {
961 return "";
962 }
963 let id = String(post.id || "").replace(/^t3_/, "").toLowerCase();
964 if (/^[0-9a-z]{4,10}$/.test(id)) {
965 return id;
966 }
967 const fromUrl = extractIds(post.permalink || post.url || "")[0];
968 return fromUrl || "";
969}
970
971function comparePosts(a, b) {
972 const ua = Number(a.created_utc || a.createdUtc || 0);
973 const ub = Number(b.created_utc || b.createdUtc || 0);
974 if (ua !== ub) {
975 return ua - ub;
976 }
977 return postId(a) < postId(b) ? -1 : 1;
978}
979
980function releaseTimeOf(post) {
981 const utc = Number(post && (post.created_utc || post.createdUtc) || 0);
982 if (!utc) {
983 return null;
984 }
985 return new Date(utc * 1000).toISOString();
986}
987
988function sameAuthor(a, b) {
989 return String((a && a.author) || "").toLowerCase() === String((b && b.author) || "").toLowerCase();
990}
991
992function pickNextLink(post) {
993 const text = String((post && (post.selftext || post.selftext_html || post.body)) || "");
994 const links = extractLabeledLinks(text);
995 const next = links.find((l) => l.kind === "next");
996 if (next) {
997 return next.id;
998 }
999 const other = links.find((l) => l.kind === "other");
1000 return other ? other.id : "";
1001}
1002
1003function isStoryPost(post) {
1004 if (!post) {
1005 return false;
1006 }
1007 const isSelf = post.is_self !== false && post.isSelf !== false;
1008 if (!isSelf && !(post.selftext || post.selftext_html)) {
1009 return false;
1010 }
1011 const flair = String(post.link_flair_text || post.flair || "")
1012 .trim()
1013 .toLowerCase()
1014 .replace(/\s+/g, "");
1015 if (["meta", "mod", "media", "art", "video", "misc"].indexOf(flair) !== -1) {
1016 return false;
1017 }
1018 const title = String(post.title || "").toLowerCase();
1019 if (title.indexOf("[meta]") === 0 || title.indexOf("[mod]") === 0) {
1020 return false;
1021 }
1022 return true;
1023}
1024
1025function isSeedPost(post) {
1026 const flair = String(post.link_flair_text || post.flair || "")
1027 .trim()
1028 .toLowerCase()
1029 .replace(/\s+/g, "");
1030 if (flair.indexOf("firstofseries") !== -1) {
1031 return true;
1032 }
1033 if (flair.indexOf("oneshot") !== -1) {
1034 return true;
1035 }
1036 return isStartTitle(post.title);
1037}
1038
1039function isStartTitle(title) {
1040 return /(?:^|[\s:\-–—|\[\(])(?:(?:part|chapter|ch|pt|episode|ep)\.?\s*(?:1|i|one|first)\b|#\s*1\b|\(\s*1\s*(?:\/\s*(?:\d+|\?))?\s*\)|prologue)\b/i.test(
1041 String(title || ""),
1042 );
1043}
1044
1045function normalizeTitle(title) {
1046 let text = String(title || "");
1047 try {
1048 text = text.normalize("NFKD").replace(/[\u0300-\u036f]/g, "");
1049 } catch (err) {
1050 // oudere JS zonder normalize
1051 }
1052 text = text.replace(
1053 /\[\s*(?:oc|pi|hfy|ff|fic|meta|chapter|ch|part|pt|oneshot|one[-\s]?shot)(?:[^\]]*)\]/gi,
1054 " ",
1055 );
1056 text = text.replace(/^\s*(?:oc|pi|hfy|ff)\s*[:|\-–—]\s*/i, " ");
1057 text = text.replace(
1058 /(?:^|[\s,;:\-–—|\[\(])(?:part|chapter|chapters|ch|pt|episode|ep|update|book|vol|volume)\.?\s*(?:[0-9]+(?:\s*[./]\s*[0-9]+)?)|#\s*[0-9]+[\]\)]?/gi,
1059 " ",
1060 );
1061 text = text.replace(/[\s:\-–—|]*\(\s*[0-9]{1,4}\s*(?:\/\s*(?:[0-9]{1,4}|\?)\s*)?\)/g, " ");
1062 text = text.replace(/[\s:\-–—|]+(?:[0-9]{1,4})(?:\s*[./]\s*[0-9]{1,4})?\s*$/g, " ");
1063 return text.toLowerCase().replace(/["“”‘’`]+/g, "").replace(/[^a-z0-9]+/g, " ").replace(/\s+/g, " ").trim();
1064}
1065
1066function titleSimilarity(a, b) {
1067 const ta = normalizeTitle(a).split(" ").filter(Boolean);
1068 const tb = normalizeTitle(b).split(" ").filter(Boolean);
1069 if (!ta.length || !tb.length) {
1070 return 0;
1071 }
1072 const setB = {};
1073 tb.forEach((w) => {
1074 setB[w] = true;
1075 });
1076 let inter = 0;
1077 ta.forEach((w) => {
1078 if (setB[w]) {
1079 inter += 1;
1080 }
1081 });
1082 return inter / Math.sqrt(ta.length * tb.length);
1083}
1084
1085function titlesBelongTogether(aTitle, bTitle, aAuthor, bAuthor, explicitNext) {
1086 if (aAuthor && bAuthor && String(aAuthor).toLowerCase() !== String(bAuthor).toLowerCase()) {
1087 return explicitNext && titleSimilarity(aTitle, bTitle) >= 0.4;
1088 }
1089 const score = titleSimilarity(aTitle, bTitle);
1090 return explicitNext ? score >= 0.3 : score >= 0.5;
1091}
1092
1093function guessTitle(titles) {
1094 if (!titles || !titles.length) {
1095 return "";
1096 }
1097 const norms = titles.map(normalizeTitle).filter(Boolean);
1098 if (!norms.length) {
1099 return String(titles[0] || "").replace(/\s+/g, " ").trim();
1100 }
1101 const first = norms[0].split(" ");
1102 let end = first.length;
1103 for (let i = 1; i < norms.length; i++) {
1104 const parts = norms[i].split(" ");
1105 let j = 0;
1106 while (j < end && j < parts.length && parts[j] === first[j]) {
1107 j += 1;
1108 }
1109 end = j;
1110 }
1111 const shared = first.slice(0, end).join(" ");
1112 if (shared.length >= 4) {
1113 return shared.replace(/\b\w/g, (c) => c.toUpperCase());
1114 }
1115 return String(titles[0] || "").replace(/\s+/g, " ").trim();
1116}
1117
1118function groupPostsIntoNovels(posts, wikiEntries) {
1119 const stories = (posts || []).filter(isStoryPost);
1120 const groups = [];
1121 const claimed = {};
1122 const wiki = wikiEntries || [];
1123
1124 function matchWiki(post) {
1125 const key = normalizeTitle(post.title);
1126 const author = String(post.author || "").toLowerCase();
1127 let best = null;
1128 let bestScore = 0;
1129 wiki.forEach((entry) => {
1130 if (entry.author && author && entry.author.toLowerCase() !== author) {
1131 return;
1132 }
1133 const score = titleSimilarity(entry.title, post.title);
1134 if (score > bestScore) {
1135 bestScore = score;
1136 best = entry;
1137 }
1138 });
1139 if (best && bestScore >= 0.55) {
1140 return best;
1141 }
1142 if (best && key && normalizeTitle(best.title) === key) {
1143 return best;
1144 }
1145 return null;
1146 }
1147
1148 const byWiki = {};
1149 const byKey = {};
1150 stories.forEach((post) => {
1151 const wikiHit = matchWiki(post);
1152 if (wikiHit) {
1153 const k = "wiki:" + (wikiHit.slug || wikiHit.wikiPath);
1154 if (!byWiki[k]) {
1155 byWiki[k] = { entry: wikiHit, posts: [] };
1156 }
1157 byWiki[k].posts.push(post);
1158 claimed[postId(post)] = true;
1159 return;
1160 }
1161 const key = String(post.author || "").toLowerCase() + "|" + normalizeTitle(post.title);
1162 if (!byKey[key]) {
1163 byKey[key] = [];
1164 }
1165 byKey[key].push(post);
1166 });
1167
1168 Object.keys(byWiki).forEach((k) => {
1169 const g = byWiki[k];
1170 const members = g.posts.slice().sort(comparePosts);
1171 const first = members[0];
1172 groups.push({
1173 name: g.entry.title,
1174 path: postId(first) ? "series/" + postId(first) : "wiki/" + g.entry.slug,
1175 author: g.entry.author || first.author || "",
1176 oneshot: members.length <= 1,
1177 nsfw: members.some((p) => p.over_18 || p.over18),
1178 cover: null,
1179 });
1180 });
1181
1182 Object.keys(byKey).forEach((k) => {
1183 const members = byKey[k].filter((p) => !claimed[postId(p)]).sort(comparePosts);
1184 if (!members.length) {
1185 return;
1186 }
1187 // Alleen een nieuwe reeks starten als er een zaad is, of precies één post.
1188 const seeds = members.filter(isSeedPost);
1189 const use = seeds.length ? [seeds[0]].concat(members.filter((p) => p !== seeds[0])) : members;
1190 const ordered = use.slice().sort(comparePosts);
1191 const first = ordered[0];
1192 groups.push({
1193 name: guessTitle(ordered.map((p) => p.title)) || first.title,
1194 path: "series/" + postId(first),
1195 author: first.author || "",
1196 oneshot: ordered.length <= 1,
1197 nsfw: ordered.some((p) => p.over_18 || p.over18),
1198 cover: null,
1199 });
1200 });
1201
1202 return groups;
1203}
1204
1205function uniqueIds(ids) {
1206 const seen = {};
1207 const out = [];
1208 (ids || []).forEach((id) => {
1209 const key = String(id || "")
1210 .replace(/^t3_/, "")
1211 .toLowerCase();
1212 if (!/^[0-9a-z]{4,10}$/.test(key) || seen[key]) {
1213 return;
1214 }
1215 seen[key] = true;
1216 out.push(key);
1217 });
1218 return out;
1219}
1220
1221async function fetchRedditBody(url) {
1222 const res = await fetchApi(url, { headers: { Accept: "application/json" } });
1223 const body = res && typeof res.text === "function" ? await res.text() : "";
1224 const status = res && res.status != null ? res.status : "?";
1225 if (!res || res.ok === false || isLoginOrBlockPage(res, body)) {
1226 return {
1227 ok: false,
1228 blocked: !res || res.ok === false || isLoginOrBlockPage(res, body),
1229 reason: "HTTP " + status + (isLoginOrBlockPage(res, body) ? " (login of blokkade)" : ""),
1230 body: body,
1231 res: res,
1232 };
1233 }
1234 if (!body || looksLikeHtml(body)) {
1235 return { ok: false, reason: "geen JSON", body: body, res: res };
1236 }
1237 return { ok: true, body: body, res: res };
1238}
1239
1240async function fetchRedditJson(pathAndQuery) {
1241 const path = pathAndQuery.indexOf("/") === 0 ? pathAndQuery : "/" + pathAndQuery;
1242 const urls = [REDDIT_WWW + path, REDDIT_OLD + path];
1243 let last = { ok: false, reason: "geen antwoord" };
1244 for (let i = 0; i < urls.length; i++) {
1245 try {
1246 const got = await fetchRedditBody(urls[i]);
1247 if (!got.ok) {
1248 last = got;
1249 continue;
1250 }
1251 try {
1252 return { ok: true, data: JSON.parse(got.body), source: "reddit" };
1253 } catch (err) {
1254 last = { ok: false, reason: "ongeldige JSON" };
1255 }
1256 } catch (err) {
1257 last = { ok: false, reason: err && err.message ? err.message : String(err) };
1258 }
1259 }
1260 return last;
1261}
1262
1263async function fetchRedditWikiMarkdown(wikiPath) {
1264 const path = String(wikiPath || "").replace(/\/+$/, "");
1265 const result = await fetchRedditJson(path + ".json?raw_json=1");
1266 if (!result.ok) {
1267 return result;
1268 }
1269 const data = result.data && result.data.data ? result.data.data : result.data;
1270 const markdown = data && (data.content_md || data.content_html || data.content);
1271 if (!markdown || !String(markdown).trim()) {
1272 return { ok: false, reason: "lege wikipagina" };
1273 }
1274 return { ok: true, markdown: String(data.content_md || markdown), source: "reddit" };
1275}
1276
1277async function fetchRedditSearch(term, page) {
1278 const params =
1279 "q=" +
1280 encodeURIComponent(term) +
1281 "&restrict_sr=1&sort=relevance&limit=25&raw_json=1&type=link&count=" +
1282 ((Math.max(1, page) - 1) * 25);
1283 const result = await fetchRedditJson("/r/HFY/search.json?" + params);
1284 if (!result.ok) {
1285 return result;
1286 }
1287 return { ok: true, posts: listingPosts(result.data), source: "reddit" };
1288}
1289
1290async function fetchRedditListing(path, page) {
1291 const count = (Math.max(1, page) - 1) * 25;
1292 const qs = "limit=25&raw_json=1" + (count ? "&count=" + count : "");
1293 const result = await fetchRedditJson(path + (path.indexOf("?") === -1 ? "?" : "&") + qs);
1294 if (!result.ok) {
1295 return result;
1296 }
1297 return { ok: true, posts: listingPosts(result.data), source: "reddit" };
1298}
1299
1300async function fetchRedditPostsByIds(ids) {
1301 const full = uniqueIds(ids).map((id) => "t3_" + id);
1302 const posts = [];
1303 for (let i = 0; i < full.length; i += 100) {
1304 const chunk = full.slice(i, i + 100);
1305 const result = await fetchRedditJson("/api/info.json?id=" + encodeURIComponent(chunk.join(",")) + "&raw_json=1");
1306 if (!result.ok) {
1307 if (!posts.length) {
1308 return result;
1309 }
1310 break;
1311 }
1312 listingPosts(result.data).forEach((p) => posts.push(p));
1313 }
1314 return { ok: true, posts: posts, source: "reddit" };
1315}
1316
1317function listingPosts(payload) {
1318 const listing = Array.isArray(payload) ? payload[0] : payload;
1319 const children = listing && listing.data && Array.isArray(listing.data.children) ? listing.data.children : [];
1320 const out = [];
1321 children.forEach((child) => {
1322 if (child && child.data && (!child.kind || child.kind === "t3")) {
1323 out.push(child.data);
1324 }
1325 });
1326 return out;
1327}
1328
1329async function fetchArcticWikiPage(base, wikiPath) {
1330 const url =
1331 String(base || "").replace(/\/+$/, "") +
1332 "/api/subreddits/wikis?paths=" +
1333 encodeURIComponent(wikiPath);
1334 try {
1335 const payload = await readJson(url, "wiki " + wikiPath);
1336 const pages = payload.data || payload;
1337 const page = Array.isArray(pages) ? pages[0] : pages;
1338 const markdown = page && (page.content || page.content_md);
1339 if (!markdown || !String(markdown).trim()) {
1340 return { ok: false, reason: "lege Arctic Shift-wikipagina" };
1341 }
1342 return { ok: true, markdown: String(markdown), source: "arctic" };
1343 } catch (err) {
1344 return { ok: false, reason: err && err.message ? err.message : String(err) };
1345 }
1346}
1347
1348async function fetchArcticSearch(base, term) {
1349 const root = String(base || "").replace(/\/+$/, "");
1350 const tryUrls = [
1351 root + "/api/posts/search?subreddit=HFY&query=" + encodeURIComponent(term) + "&limit=25&sort=desc",
1352 root + "/api/posts/search?subreddit=HFY&title=" + encodeURIComponent(term) + "&limit=25&sort=desc",
1353 ];
1354 let last = { ok: false, reason: "Arctic Shift-zoeken mislukt" };
1355 for (let i = 0; i < tryUrls.length; i++) {
1356 try {
1357 const payload = await readJson(tryUrls[i], "zoeken");
1358 const posts = payload.data || payload;
1359 if (Array.isArray(posts)) {
1360 return { ok: true, posts: posts, source: "arctic" };
1361 }
1362 } catch (err) {
1363 last = { ok: false, reason: err && err.message ? err.message : String(err) };
1364 }
1365 }
1366 return last;
1367}
1368
1369async function fetchArcticRecent(base) {
1370 const url = String(base || "").replace(/\/+$/, "") + "/api/posts/search?subreddit=HFY&limit=50&sort=desc";
1371 try {
1372 const payload = await readJson(url, "recente posts");
1373 const posts = payload.data || payload;
1374 return { ok: true, posts: Array.isArray(posts) ? posts : [], source: "arctic" };
1375 } catch (err) {
1376 return { ok: false, reason: err && err.message ? err.message : String(err) };
1377 }
1378}
1379
1380async function fetchArcticPostsByIds(base, ids) {
1381 const url =
1382 String(base || "").replace(/\/+$/, "") +
1383 "/api/posts/ids?ids=" +
1384 encodeURIComponent(uniqueIds(ids).join(","));
1385 try {
1386 const payload = await readJson(url, "posts");
1387 const posts = payload.data || payload;
1388 return { ok: true, posts: Array.isArray(posts) ? posts : [], source: "arctic" };
1389 } catch (err) {
1390 return { ok: false, reason: err && err.message ? err.message : String(err) };
1391 }
1392}
1393
1394function chapterIdFromPath(chapterPath) {
1395 const id = String(chapterPath || "")
1396 .replace(/^chapter\//, "")
1397 .replace(/^series\//, "")
1398 .trim()
1399 .toLowerCase();
1400 return /^[0-9a-z]{4,10}$/.test(id) ? id : "";
1401}
1402
1403function redditJsonUrls(id) {
1404 const path = "/r/HFY/comments/" + id + "/.json?raw_json=1";
1405 return [REDDIT_WWW + path, REDDIT_OLD + path];
1406}
1407
1408function looksLikeHtml(text) {
1409 const body = String(text || "").trim();
1410 if (!body) {
1411 return false;
1412 }
1413 return /^\s*<(!DOCTYPE|html|body|head)\b/i.test(body);
1414}
1415
1416function isLoginOrBlockPage(res, body) {
1417 const status = res && res.status != null ? Number(res.status) : 0;
1418 if (status === 401 || status === 403 || status === 429) {
1419 return true;
1420 }
1421 const finalUrl = String((res && res.url) || "");
1422 if (/[?&]reason=lor2\b/i.test(finalUrl) || /\/login\b/i.test(finalUrl)) {
1423 return true;
1424 }
1425 const type = String(
1426 (res && res.headers && typeof res.headers.get === "function" && res.headers.get("content-type")) || "",
1427 ).toLowerCase();
1428 if (type.indexOf("text/html") !== -1 && !/json/i.test(type)) {
1429 return true;
1430 }
1431 const text = String(body || "");
1432 if (looksLikeHtml(text)) {
1433 return true;
1434 }
1435 const lower = text.toLowerCase();
1436 if (
1437 lower.indexOf("reason=lor2") !== -1 ||
1438 lower.indexOf("www.reddit.com/login") !== -1 ||
1439 lower.indexOf("old.reddit.com/login") !== -1 ||
1440 lower.indexOf("log in to reddit") !== -1 ||
1441 (lower.indexOf("shreddit") !== -1 && lower.indexOf("<!doctype html") !== -1)
1442 ) {
1443 return true;
1444 }
1445 return false;
1446}
1447
1448function postFromRedditPayload(payload) {
1449 if (!payload) {
1450 return null;
1451 }
1452 const listing = Array.isArray(payload) ? payload[0] : payload;
1453 const children =
1454 listing && listing.data && Array.isArray(listing.data.children) ? listing.data.children : [];
1455 const child = children.find((c) => !c.kind || c.kind === "t3") || children[0];
1456 if (!child || !child.data) {
1457 return null;
1458 }
1459 if (child.kind && child.kind !== "t3") {
1460 return null;
1461 }
1462 return child.data;
1463}
1464
1465function decodeRedditHtml(html) {
1466 let text = String(html || "");
1467 if (text.indexOf("&lt;") !== -1 || text.indexOf("&amp;") !== -1) {
1468 text = text
1469 .replace(/&lt;/g, "<")
1470 .replace(/&gt;/g, ">")
1471 .replace(/&quot;/g, '"')
1472 .replace(/&#39;/g, "'")
1473 .replace(/&amp;/g, "&");
1474 }
1475 return text;
1476}
1477
1478async function tryRedditChapter(id) {
1479 let lastReason = "geen antwoord";
1480 const urls = redditJsonUrls(id);
1481 for (let i = 0; i < urls.length; i++) {
1482 const url = urls[i];
1483 try {
1484 const res = await fetchApi(url, { headers: { Accept: "application/json" } });
1485 const body = typeof res.text === "function" ? await res.text() : "";
1486 if (!res || res.ok === false || isLoginOrBlockPage(res, body)) {
1487 const status = res && res.status != null ? res.status : "?";
1488 lastReason = "HTTP " + status + (isLoginOrBlockPage(res, body) ? " (login of blokkade)" : "");
1489 continue;
1490 }
1491 if (!body || looksLikeHtml(body)) {
1492 lastReason = "geen JSON";
1493 continue;
1494 }
1495 let payload;
1496 try {
1497 payload = JSON.parse(body);
1498 } catch (err) {
1499 lastReason = "ongeldige JSON";
1500 continue;
1501 }
1502 const post = postFromRedditPayload(payload);
1503 if (!post) {
1504 lastReason = "geen post in de listing";
1505 continue;
1506 }
1507 return { ok: true, post: post };
1508 } catch (err) {
1509 lastReason = err && err.message ? err.message : String(err);
1510 }
1511 }
1512 return { ok: false, reason: lastReason };
1513}
1514
1515async function tryArcticChapter(base, id) {
1516 const url = String(base || "").replace(/\/+$/, "") + "/api/posts/ids?ids=" + encodeURIComponent(id) + "&md2html=true";
1517 try {
1518 const payload = await readJson(url, "hoofdstuk " + id);
1519 const posts = payload.data || payload;
1520 const post = Array.isArray(posts) ? posts[0] : posts;
1521 if (!post) {
1522 return { ok: false, reason: "leeg antwoord" };
1523 }
1524 return { ok: true, post: post };
1525 } catch (err) {
1526 return { ok: false, reason: err && err.message ? err.message : String(err) };
1527 }
1528}
1529
1530function sourceFooter(label) {
1531 return '<p class="hfy-source"><em>Bron: ' + escapeHtml(label) + "</em></p>";
1532}
1533
1534function renderChapter(post, sourceLabel) {
1535 if (!post) {
1536 return "<p><em>Dit hoofdstuk is niet (meer) beschikbaar.</em></p>" + sourceFooter(sourceLabel);
1537 }
1538 const title = post.title ? "<h1>" + escapeHtml(post.title) + "</h1>" : "";
1539 const by = post.author ? "<p><em>by u/" + escapeHtml(post.author) + "</em></p>" : "";
1540 const rawHtml = post.selftext_html ? decodeRedditHtml(post.selftext_html) : "";
1541 const html = rawHtml ? sanitizeHtml(rawHtml) : markdownToHtml(post.selftext || "");
1542 if (!html) {
1543 return (
1544 title +
1545 by +
1546 "<p><em>Deze post heeft geen tekst (verwijderd of een link-post).</em></p>" +
1547 sourceFooter(sourceLabel)
1548 );
1549 }
1550 return title + by + html + sourceFooter(sourceLabel);
1551}
1552
1553function escapeHtml(text) {
1554 return String(text)
1555 .replace(/&/g, "&amp;")
1556 .replace(/</g, "&lt;")
1557 .replace(/>/g, "&gt;")
1558 .replace(/"/g, "&quot;");
1559}
1560
1561function sanitizeHtml(html) {
1562 // Reddit en Arctic Shift leveren HTML; we strippen scripts en on-event-attributen.
1563 return String(html)
1564 .replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, "")
1565 .replace(/\son[a-z]+\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)/gi, "");
1566}
1567
1568function markdownToHtml(md) {
1569 if (!md) {
1570 return "";
1571 }
1572 const escaped = escapeHtml(md);
1573 const withLinks = escaped.replace(
1574 /\[([^\]]+)\]\((https?:\/\/[^)]+)\)/g,
1575 '<a href="$2">$1</a>',
1576 );
1577 const paragraphs = withLinks
1578 .split(/\n{2,}/)
1579 .map((block) => "<p>" + block.replace(/\n/g, "<br>") + "</p>");
1580 return paragraphs.join("");
1581}
1582
1583const instance = new HFYNovelsPlugin();
1584exports.default = instance;