Laat de plugin live zoeken en ontdekken via de WebView-sessie
De catalogus (wiki-index, recente OC-posts, zoeken en hoofdstuklijsten) wordt op de telefoon opgebouwd met Reddit-JSON en de cookies van de gebruiker. Bij een 403 of fout volgt Arctic Shift, daarna de statische index. Versie 0.3.0.
@@ -1,14 +1,18 @@
1
1
"use strict";
2
2
3
3
/**
4
* LNReader-plugin voor de zelfgehoste HFY-Novels-index.
4
* LNReader-plugin voor r/HFY.
5
5
*
6
* De catalogus (titels, auteurs, hoofdstuklijsten) komt uit een statische
7
* JSON-index. De verhaaltekst zelf staat niet in die index: parseChapter
8
* probeert eerst de echte Reddit-JSON (www.reddit.com/.../.json, daarna
9
* old.reddit.com) via fetchApi, zodat cookies uit de LNReader-WebView
10
* meegaan na inloggen op reddit.com. Bij een fout, 403 of login-/blokpagina
11
* valt de plugin terug op Arctic Shift.
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.
12
16
*
13
17
* Dit bestand is CommonJS, zoals de officiële gecompileerde plugins:
14
18
* require(...) uit de LNReader-allowlist, en `exports.default = instance`.
@@ -27,6 +31,11 @@ const DEFAULT_INDEX_BASE =
27
31
const DEFAULT_INDEX_REV = "master";
28
32
const DEFAULT_CHAPTER_SOURCE = "https://arctic-shift.photon-reddit.com";
29
33
const PAGE_FALLBACK_SIZE = 50;
34
const WIKI_CACHE_KEY = "wikiSeriesIndex";
35
const WIKI_CACHE_MS = 6 * 60 * 60 * 1000;
36
const REDDIT_WWW = "https://www.reddit.com";
37
const REDDIT_OLD = "https://old.reddit.com";
38
const WIKI_INDEX_PATH = "/r/HFY/wiki/series";
30
39
31
40
function setting(key, fallback) {
32
41
try {
@@ -96,13 +105,22 @@ class HFYNovelsPlugin {
96
105
constructor() {
97
106
this.id = "hfyNovels";
98
107
this.name = "HFY Novels";
99
this.version = "0.2.0";
108
this.version = "0.3.0";
100
109
this.site = "https://www.reddit.com/r/HFY/";
101
110
this.icon = "icon.png";
102
111
// Geen webStorageUtilized: Reddit-login zit in cookies. LNReader's
103
112
// fetchApi (native fetch) stuurt de WebView-cookies automatisch mee
104
113
// voor hetzelfde domein; localStorage is daarvoor niet nodig.
105
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
},
106
124
kind: {
107
125
value: "all",
108
126
label: "Type",
@@ -126,7 +144,7 @@ class HFYNovelsPlugin {
126
144
this.pluginSettings = {
127
145
indexBaseUrl: {
128
146
value: DEFAULT_INDEX_BASE,
129
label: "Basis-URL van de index",
147
label: "Basis-URL van de statische index (laatste fallback)",
130
148
type: "Text",
131
149
},
132
150
indexRev: {
@@ -136,12 +154,12 @@ class HFYNovelsPlugin {
136
154
},
137
155
chapterSource: {
138
156
value: "auto",
139
label: "Bron voor hoofdstuktekst",
157
label: "Bron voor catalogus en hoofdstukken",
140
158
type: "Select",
141
159
options: [
142
{ label: "Automatisch (Reddit, anders Arctic Shift)", value: "auto" },
160
{ label: "Automatisch (Reddit, anders Arctic Shift, anders index)", value: "auto" },
143
161
{ label: "Alleen Reddit", value: "reddit" },
144
{ label: "Alleen Arctic Shift", value: "arctic" },
162
{ label: "Alleen Arctic Shift (daarna index)", value: "arctic" },
145
163
],
146
164
},
147
165
chapterSourceUrl: {
@@ -193,16 +211,20 @@ class HFYNovelsPlugin {
193
211
* Maakt "Open in WebView" op een reeks of hoofdstuk tot een Reddit-URL,
194
212
* zodat je daar kunt inloggen. Zonder deze functie zou LNReader
195
213
* site+path plakken (bijv. /r/HFY/series/id), wat geen geldige Reddit-pagina is.
196
* Het reeks-id is de oudste post (eerste deel).
214
* Het reeks-id is de oudste post (eerste deel), of een wiki-slug.
197
215
*/
198
216
resolveUrl(path, isNovel) {
199
217
const raw = String(path || "").trim();
200
218
if (/^https?:\/\//i.test(raw)) {
201
219
return raw;
202
220
}
221
const wikiSlug = wikiSlugFromPath(raw);
222
if (wikiSlug) {
223
return REDDIT_WWW + "/r/HFY/wiki/series/" + wikiSlug + "/";
224
}
203
225
const id = chapterIdFromPath(raw) || chapterIdFromPath(raw.replace(/^series\//, "chapter/"));
204
226
if (id) {
205
return "https://www.reddit.com/r/HFY/comments/" + id + "/";
227
return REDDIT_WWW + "/r/HFY/comments/" + id + "/";
206
228
}
207
229
return this.site;
208
230
}
@@ -229,10 +251,162 @@ class HFYNovelsPlugin {
229
251
async popularNovels(pageNo, options) {
230
252
const opts = options || {};
231
253
const filters = opts.filters || {};
232
const showLatest = !!opts.showLatestNovels;
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
}
233
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
}
234
404
235
// Zonder extra filters is de catalogus al op update-datum gesorteerd.
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);
236
410
const usePagedCatalog =
237
411
!showLatest &&
238
412
(!filters.kind || filters.kind.value === "all") &&
@@ -251,16 +425,14 @@ class HFYNovelsPlugin {
251
425
}
252
426
253
427
const search = await readJson(this.indexUrl("search-index.json"), "zoekindex");
254
let items = (search.novels || []).filter((n) => this.matchesFilters(n, filters));
428
const items = (search.novels || []).filter((n) => this.matchesFilters(n, filters));
255
429
const size = (await this.loadMeta()).pageSize || PAGE_FALLBACK_SIZE;
256
430
const start = (page - 1) * size;
257
431
return items.slice(start, start + size).map((n) => toNovelItem(n, this));
258
432
}
259
433
260
async searchNovels(searchTerm, pageNo) {
261
const term = String(searchTerm || "")
262
.trim()
263
.toLowerCase();
434
async staticSearch(searchTerm, pageNo) {
435
const term = String(searchTerm || "").trim().toLowerCase();
264
436
if (!term) {
265
437
return [];
266
438
}
@@ -271,23 +443,11 @@ class HFYNovelsPlugin {
271
443
const author = String(n.author || "").toLowerCase();
272
444
return name.includes(term) || author.includes(term);
273
445
});
274
const size = PAGE_FALLBACK_SIZE;
275
const start = (page - 1) * size;
276
return hits.slice(start, start + size).map((n) => toNovelItem(n, this));
277
}
278
279
seriesFileFor(novelPath) {
280
const id = String(novelPath || "")
281
.replace(/^series\//, "")
282
.replace(/\/+$/, "");
283
if (!id) {
284
throw new Error("HFY Novels: leeg reeks-pad");
285
}
286
const prefix = id.charAt(0).toLowerCase() || "x";
287
return "series/" + prefix + "/" + id + ".json";
446
const start = (page - 1) * PAGE_FALLBACK_SIZE;
447
return hits.slice(start, start + PAGE_FALLBACK_SIZE).map((n) => toNovelItem(n, this));
288
448
}
289
449
290
async parseNovel(novelPath) {
450
async staticParseNovel(novelPath) {
291
451
const data = await readJson(this.indexUrl(this.seriesFileFor(novelPath)), "reeks " + novelPath);
292
452
return {
293
453
path: data.path || novelPath,
@@ -307,43 +467,927 @@ class HFYNovelsPlugin {
307
467
};
308
468
}
309
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
}
310
480
311
async parseChapter(chapterPath) {
312
const id = chapterIdFromPath(chapterPath);
313
if (!id) {
314
throw new Error("HFY Novels: ongeldig hoofdstuk-id");
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" };
315
604
}
316
const mode = this.chapterMode();
317
let redditError = "";
318
605
if (mode !== "arctic") {
319
const reddit = await tryRedditChapter(id);
606
const reddit = await fetchRedditWikiMarkdown(WIKI_INDEX_PATH);
320
607
if (reddit.ok) {
321
return renderChapter(reddit.post, "Reddit");
608
const entries = parseSeriesIndex(reddit.markdown);
609
if (entries.length) {
610
writeWikiCache(entries);
611
return { ok: true, entries: entries, source: "reddit" };
612
}
322
613
}
323
redditError = reddit.reason || "onbekende fout";
324
614
if (mode === "reddit") {
325
throw new Error(
326
"HFY Novels: Reddit gaf geen hoofdstuk (" +
327
redditError +
328
"). Log in via Open in WebView op reddit.com en probeer het opnieuw.",
329
);
615
return { ok: false, reason: (reddit && reddit.reason) || "lege wiki-index" };
330
616
}
331
617
}
332
const arctic = await tryArcticChapter(this.chapterSource(), id);
618
const arctic = await fetchArcticWikiPage(this.chapterSource(), WIKI_INDEX_PATH);
333
619
if (arctic.ok) {
334
const suffix = mode === "arctic" ? "Arctic Shift" : "Arctic Shift (fallback)";
335
return renderChapter(arctic.post, suffix);
620
const entries = parseSeriesIndex(arctic.markdown);
621
if (entries.length) {
622
writeWikiCache(entries);
623
return { ok: true, entries: entries, source: "arctic" };
624
}
336
625
}
337
if (redditError) {
338
throw new Error(
339
"HFY Novels: Reddit (" +
340
redditError +
341
") en Arctic Shift (" +
342
(arctic.reason || "geen tekst") +
343
") faalden allebei.",
344
);
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
}
345
639
}
346
throw new Error("HFY Novels: kon hoofdstuk " + id + " niet laden (" + (arctic.reason || "geen tekst") + ")");
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
747
function 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
763
function 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
776
function 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
787
function 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
805
function 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
814
function 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
844
function 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
877
function 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
894
function cleanHeading(raw) {
895
let text = String(raw || "").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1");
896
text = text.replace(/[*_`]+/g, "");
897
return text.replace(/\s+/g, " ").trim();
898
}
899
900
function 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
908
function 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
926
function 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
936
function 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
959
function 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
971
function 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
980
function 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
988
function sameAuthor(a, b) {
989
return String((a && a.author) || "").toLowerCase() === String((b && b.author) || "").toLowerCase();
990
}
991
992
function 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
1003
function 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
1025
function 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
1039
function 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
1045
function 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
1066
function 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
1085
function 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
1093
function 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
1118
function 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
1205
function 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
1221
async 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
1240
async 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
1263
async 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
1277
async 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
1290
async 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
1300
async 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
1317
function 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
1329
async 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
1348
async 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
1369
async 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
1380
async 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) };
347
1391
}
348
1392
}
349
1393
@@ -358,10 +1402,7 @@ function chapterIdFromPath(chapterPath) {
358
1402
359
1403
function redditJsonUrls(id) {
360
1404
const path = "/r/HFY/comments/" + id + "/.json?raw_json=1";
361
return [
362
"https://www.reddit.com" + path,
363
"https://old.reddit.com" + path,
364
];
1405
return [REDDIT_WWW + path, REDDIT_OLD + path];
365
1406
}
366
1407
367
1408
function looksLikeHtml(text) {
@@ -1,14 +1,18 @@1
"use strict";1
"use strict";2
2
3
/**3
/**4
* LNReader-plugin voor de zelfgehoste HFY-Novels-index.4
* LNReader-plugin voor r/HFY.5
*5
*6
* De catalogus (titels, auteurs, hoofdstuklijsten) komt uit een statische6
* De catalogus wordt live opgebouwd op de telefoon, met de Reddit-sessie7
* JSON-index. De verhaaltekst zelf staat niet in die index: parseChapter7
* uit de LNReader-WebView (elk account heeft zijn eigen cookies):8
* probeert eerst de echte Reddit-JSON (www.reddit.com/.../.json, daarna8
* - popularNovels: de wiki-serie-index, plus 'Nieuw' / Latest met recente OC-posts9
* old.reddit.com) via fetchApi, zodat cookies uit de LNReader-WebView9
* - searchNovels: r/HFY/search.json (restrict_sr=1), plus hits uit de wiki,10
* meegaan na inloggen op reddit.com. Bij een fout, 403 of login-/blokpagina10
* waarbij series en losse Part-1-posts tot één novel worden gegroepeerd11
* valt de plugin terug op Arctic Shift.11
* - parseNovel: wiki-inhoudsopgave, anders next-links met titelgelijkenis12
*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.12
*16
*13
* Dit bestand is CommonJS, zoals de officiële gecompileerde plugins:17
* Dit bestand is CommonJS, zoals de officiële gecompileerde plugins:14
* require(...) uit de LNReader-allowlist, en `exports.default = instance`.18
* require(...) uit de LNReader-allowlist, en `exports.default = instance`.@@ -27,6 +31,11 @@ const DEFAULT_INDEX_BASE =27
const DEFAULT_INDEX_REV = "master";31
const DEFAULT_INDEX_REV = "master";28
const DEFAULT_CHAPTER_SOURCE = "https://arctic-shift.photon-reddit.com";32
const DEFAULT_CHAPTER_SOURCE = "https://arctic-shift.photon-reddit.com";29
const PAGE_FALLBACK_SIZE = 50;33
const PAGE_FALLBACK_SIZE = 50;34
const WIKI_CACHE_KEY = "wikiSeriesIndex";35
const WIKI_CACHE_MS = 6 * 60 * 60 * 1000;36
const REDDIT_WWW = "https://www.reddit.com";37
const REDDIT_OLD = "https://old.reddit.com";38
const WIKI_INDEX_PATH = "/r/HFY/wiki/series";30
39
31
function setting(key, fallback) {40
function setting(key, fallback) {32
try {41
try {@@ -96,13 +105,22 @@ class HFYNovelsPlugin {96
constructor() {105
constructor() {97
this.id = "hfyNovels";106
this.id = "hfyNovels";98
this.name = "HFY Novels";107
this.name = "HFY Novels";99
this.version = "0.2.0";108
this.version = "0.3.0";100
this.site = "https://www.reddit.com/r/HFY/";109
this.site = "https://www.reddit.com/r/HFY/";101
this.icon = "icon.png";110
this.icon = "icon.png";102
// Geen webStorageUtilized: Reddit-login zit in cookies. LNReader's111
// Geen webStorageUtilized: Reddit-login zit in cookies. LNReader's103
// fetchApi (native fetch) stuurt de WebView-cookies automatisch mee112
// fetchApi (native fetch) stuurt de WebView-cookies automatisch mee104
// voor hetzelfde domein; localStorage is daarvoor niet nodig.113
// voor hetzelfde domein; localStorage is daarvoor niet nodig.105
this.filters = {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
},106
kind: {124
kind: {107
value: "all",125
value: "all",108
label: "Type",126
label: "Type",@@ -126,7 +144,7 @@ class HFYNovelsPlugin {126
this.pluginSettings = {144
this.pluginSettings = {127
indexBaseUrl: {145
indexBaseUrl: {128
value: DEFAULT_INDEX_BASE,146
value: DEFAULT_INDEX_BASE,129
label: "Basis-URL van de index",147
label: "Basis-URL van de statische index (laatste fallback)",130
type: "Text",148
type: "Text",131
},149
},132
indexRev: {150
indexRev: {@@ -136,12 +154,12 @@ class HFYNovelsPlugin {136
},154
},137
chapterSource: {155
chapterSource: {138
value: "auto",156
value: "auto",139
label: "Bron voor hoofdstuktekst",157
label: "Bron voor catalogus en hoofdstukken",140
type: "Select",158
type: "Select",141
options: [159
options: [142
{ label: "Automatisch (Reddit, anders Arctic Shift)", value: "auto" },160
{ label: "Automatisch (Reddit, anders Arctic Shift, anders index)", value: "auto" },143
{ label: "Alleen Reddit", value: "reddit" },161
{ label: "Alleen Reddit", value: "reddit" },144
{ label: "Alleen Arctic Shift", value: "arctic" },162
{ label: "Alleen Arctic Shift (daarna index)", value: "arctic" },145
],163
],146
},164
},147
chapterSourceUrl: {165
chapterSourceUrl: {@@ -193,16 +211,20 @@ class HFYNovelsPlugin {193
* Maakt "Open in WebView" op een reeks of hoofdstuk tot een Reddit-URL,211
* Maakt "Open in WebView" op een reeks of hoofdstuk tot een Reddit-URL,194
* zodat je daar kunt inloggen. Zonder deze functie zou LNReader212
* zodat je daar kunt inloggen. Zonder deze functie zou LNReader195
* site+path plakken (bijv. /r/HFY/series/id), wat geen geldige Reddit-pagina is.213
* site+path plakken (bijv. /r/HFY/series/id), wat geen geldige Reddit-pagina is.196
* Het reeks-id is de oudste post (eerste deel).214
* Het reeks-id is de oudste post (eerste deel), of een wiki-slug.197
*/215
*/198
resolveUrl(path, isNovel) {216
resolveUrl(path, isNovel) {199
const raw = String(path || "").trim();217
const raw = String(path || "").trim();200
if (/^https?:\/\//i.test(raw)) {218
if (/^https?:\/\//i.test(raw)) {201
return raw;219
return raw;202
}220
}221
const wikiSlug = wikiSlugFromPath(raw);222
if (wikiSlug) {223
return REDDIT_WWW + "/r/HFY/wiki/series/" + wikiSlug + "/";224
}203
const id = chapterIdFromPath(raw) || chapterIdFromPath(raw.replace(/^series\//, "chapter/"));225
const id = chapterIdFromPath(raw) || chapterIdFromPath(raw.replace(/^series\//, "chapter/"));204
if (id) {226
if (id) {205
return "https://www.reddit.com/r/HFY/comments/" + id + "/";227
return REDDIT_WWW + "/r/HFY/comments/" + id + "/";206
}228
}207
return this.site;229
return this.site;208
}230
}@@ -229,10 +251,162 @@ class HFYNovelsPlugin {229
async popularNovels(pageNo, options) {251
async popularNovels(pageNo, options) {230
const opts = options || {};252
const opts = options || {};231
const filters = opts.filters || {};253
const filters = opts.filters || {};232
const showLatest = !!opts.showLatestNovels;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
}233
const page = Math.max(1, parseInt(pageNo, 10) || 1);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 een337
// 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
}234
404
235
// Zonder extra filters is de catalogus al op update-datum gesorteerd.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);236
const usePagedCatalog =410
const usePagedCatalog =237
!showLatest &&411
!showLatest &&238
(!filters.kind || filters.kind.value === "all") &&412
(!filters.kind || filters.kind.value === "all") &&@@ -251,16 +425,14 @@ class HFYNovelsPlugin {251
}425
}252
426
253
const search = await readJson(this.indexUrl("search-index.json"), "zoekindex");427
const search = await readJson(this.indexUrl("search-index.json"), "zoekindex");254
let items = (search.novels || []).filter((n) => this.matchesFilters(n, filters));428
const items = (search.novels || []).filter((n) => this.matchesFilters(n, filters));255
const size = (await this.loadMeta()).pageSize || PAGE_FALLBACK_SIZE;429
const size = (await this.loadMeta()).pageSize || PAGE_FALLBACK_SIZE;256
const start = (page - 1) * size;430
const start = (page - 1) * size;257
return items.slice(start, start + size).map((n) => toNovelItem(n, this));431
return items.slice(start, start + size).map((n) => toNovelItem(n, this));258
}432
}259
433
260
async searchNovels(searchTerm, pageNo) {434
async staticSearch(searchTerm, pageNo) {261
const term = String(searchTerm || "")435
const term = String(searchTerm || "").trim().toLowerCase();262
.trim()263
.toLowerCase();264
if (!term) {436
if (!term) {265
return [];437
return [];266
}438
}@@ -271,23 +443,11 @@ class HFYNovelsPlugin {271
const author = String(n.author || "").toLowerCase();443
const author = String(n.author || "").toLowerCase();272
return name.includes(term) || author.includes(term);444
return name.includes(term) || author.includes(term);273
});445
});274
const size = PAGE_FALLBACK_SIZE;446
const start = (page - 1) * PAGE_FALLBACK_SIZE;275
const start = (page - 1) * size;447
return hits.slice(start, start + PAGE_FALLBACK_SIZE).map((n) => toNovelItem(n, this));276
return hits.slice(start, start + size).map((n) => toNovelItem(n, this));277
}278
279
seriesFileFor(novelPath) {280
const id = String(novelPath || "")281
.replace(/^series\//, "")282
.replace(/\/+$/, "");283
if (!id) {284
throw new Error("HFY Novels: leeg reeks-pad");285
}286
const prefix = id.charAt(0).toLowerCase() || "x";287
return "series/" + prefix + "/" + id + ".json";288
}448
}289
449
290
async parseNovel(novelPath) {450
async staticParseNovel(novelPath) {291
const data = await readJson(this.indexUrl(this.seriesFileFor(novelPath)), "reeks " + novelPath);451
const data = await readJson(this.indexUrl(this.seriesFileFor(novelPath)), "reeks " + novelPath);292
return {452
return {293
path: data.path || novelPath,453
path: data.path || novelPath,@@ -307,43 +467,927 @@ class HFYNovelsPlugin {307
};467
};308
}468
}309
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
}310
480
311
async parseChapter(chapterPath) {481
async liveLatestNovels(page, filters, mode) {312
const id = chapterIdFromPath(chapterPath);482
const fetched = await this.loadRecentOcPosts(page, mode);313
if (!id) {483
if (!fetched.ok) {314
throw new Error("HFY Novels: ongeldig hoofdstuk-id");484
return fetched;485
}486
const grouped = groupPostsIntoNovels(fetched.posts, fetched.wikiEntries || []);487
const items = grouped488
.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" };315
}604
}316
const mode = this.chapterMode();317
let redditError = "";318
if (mode !== "arctic") {605
if (mode !== "arctic") {319
const reddit = await tryRedditChapter(id);606
const reddit = await fetchRedditWikiMarkdown(WIKI_INDEX_PATH);320
if (reddit.ok) {607
if (reddit.ok) {321
return renderChapter(reddit.post, "Reddit");608
const entries = parseSeriesIndex(reddit.markdown);609
if (entries.length) {610
writeWikiCache(entries);611
return { ok: true, entries: entries, source: "reddit" };612
}322
}613
}323
redditError = reddit.reason || "onbekende fout";324
if (mode === "reddit") {614
if (mode === "reddit") {325
throw new Error(615
return { ok: false, reason: (reddit && reddit.reason) || "lege wiki-index" };326
"HFY Novels: Reddit gaf geen hoofdstuk (" +327
redditError +328
"). Log in via Open in WebView op reddit.com en probeer het opnieuw.",329
);330
}616
}331
}617
}332
const arctic = await tryArcticChapter(this.chapterSource(), id);618
const arctic = await fetchArcticWikiPage(this.chapterSource(), WIKI_INDEX_PATH);333
if (arctic.ok) {619
if (arctic.ok) {334
const suffix = mode === "arctic" ? "Arctic Shift" : "Arctic Shift (fallback)";620
const entries = parseSeriesIndex(arctic.markdown);335
return renderChapter(arctic.post, suffix);621
if (entries.length) {622
writeWikiCache(entries);623
return { ok: true, entries: entries, source: "arctic" };624
}336
}625
}337
if (redditError) {626
return { ok: false, reason: (arctic && arctic.reason) || "wiki-index niet beschikbaar" };338
throw new Error(627
}339
"HFY Novels: Reddit (" +628
340
redditError +629
async loadWikiPage(slug, mode) {341
") en Arctic Shift (" +630
const path = WIKI_INDEX_PATH + "/" + slug;342
(arctic.reason || "geen tekst") +631
if (mode !== "arctic") {343
") faalden allebei.",632
const reddit = await fetchRedditWikiMarkdown(path);344
);633
if (reddit.ok) {634
return { ok: true, entry: parseSeriesPage(reddit.markdown, path), source: "reddit" };635
}636
if (mode === "reddit") {637
return reddit;638
}345
}639
}346
throw new Error("HFY Novels: kon hoofdstuk " + id + " niet laden (" + (arctic.reason || "geen tekst") + ")");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
747
function 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
763
function 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
776
function 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
787
function 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
805
function 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 optioneel811
}812
}813
814
function 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
844
function 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
877
function 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
894
function cleanHeading(raw) {895
let text = String(raw || "").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1");896
text = text.replace(/[*_`]+/g, "");897
return text.replace(/\s+/g, " ").trim();898
}899
900
function 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
908
function 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
926
function 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
936
function 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
959
function 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
971
function 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
980
function 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
988
function sameAuthor(a, b) {989
return String((a && a.author) || "").toLowerCase() === String((b && b.author) || "").toLowerCase();990
}991
992
function 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
1003
function 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
1025
function 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
1039
function 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
1045
function 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 normalize1051
}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
1066
function 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
1085
function 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
1093
function 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
1118
function 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
1205
function 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
1221
async 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
1240
async 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
1263
async 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
1277
async 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
1290
async 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
1300
async 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
1317
function 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
1329
async 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
1348
async 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
1369
async 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
1380
async 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) };347
}1391
}348
}1392
}349
1393
@@ -358,10 +1402,7 @@ function chapterIdFromPath(chapterPath) {358
1402
359
function redditJsonUrls(id) {1403
function redditJsonUrls(id) {360
const path = "/r/HFY/comments/" + id + "/.json?raw_json=1";1404
const path = "/r/HFY/comments/" + id + "/.json?raw_json=1";361
return [1405
return [REDDIT_WWW + path, REDDIT_OLD + path];362
"https://www.reddit.com" + path,363
"https://old.reddit.com" + path,364
];365
}1406
}366
1407
367
function looksLikeHtml(text) {1408
function looksLikeHtml(text) {@@ -4,7 +4,7 @@
4
4
"name": "HFY Novels",
5
5
"site": "https://www.reddit.com/r/HFY/",
6
6
"lang": "English",
7
"version": "0.2.0",
7
"version": "0.3.0",
8
8
"url": "https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/hfy-novels.js?rev=master",
9
9
"iconUrl": "https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/icon.png?rev=master",
10
10
"hasSettings": true
@@ -4,7 +4,7 @@4
"name": "HFY Novels",4
"name": "HFY Novels",5
"site": "https://www.reddit.com/r/HFY/",5
"site": "https://www.reddit.com/r/HFY/",6
"lang": "English",6
"lang": "English",7
"version": "0.2.0",7
"version": "0.3.0",8
"url": "https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/hfy-novels.js?rev=master",8
"url": "https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/hfy-novels.js?rev=master",9
"iconUrl": "https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/icon.png?rev=master",9
"iconUrl": "https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/icon.png?rev=master",10
"hasSettings": true10
"hasSettings": true@@ -1 +1 @@
1
[{"id":"hfyNovels","name":"HFY Novels","site":"https://www.reddit.com/r/HFY/","lang":"English","version":"0.2.0","url":"https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/hfy-novels.js?rev=master","iconUrl":"https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/icon.png?rev=master","hasSettings":true}]
1
[{"id":"hfyNovels","name":"HFY Novels","site":"https://www.reddit.com/r/HFY/","lang":"English","version":"0.3.0","url":"https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/hfy-novels.js?rev=master","iconUrl":"https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/icon.png?rev=master","hasSettings":true}]
@@ -1 +1 @@1
[{"id":"hfyNovels","name":"HFY Novels","site":"https://www.reddit.com/r/HFY/","lang":"English","version":"0.2.0","url":"https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/hfy-novels.js?rev=master","iconUrl":"https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/icon.png?rev=master","hasSettings":true}]1
[{"id":"hfyNovels","name":"HFY Novels","site":"https://www.reddit.com/r/HFY/","lang":"English","version":"0.3.0","url":"https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/hfy-novels.js?rev=master","iconUrl":"https://git.timfalken.com/timfa/hfy-novels/-/raw/.dist/icon.png?rev=master","hasSettings":true}]@@ -2,7 +2,9 @@
2
2
3
3
Een [LNReader](https://www.lnreader.app/)-pluginbron voor verhalen van [r/HFY](https://www.reddit.com/r/HFY/). Deze repository is zelf de plugin-repository: LNReader laadt een `plugins.min.json` en een gebouwde plugin-JS via de raw-URL van BlueGit.
4
4
5
De index bevat **alleen metadata** (titel, auteur, hoofdstukvolgorde, Reddit-IDs). De tekst van de verhalen staat er niet in. Die haalt de plugin pas op als je een hoofdstuk opent: eerst via Reddit-JSON (met de sessie uit de WebView), en bij een fout, 403 of loginpagina via [Arctic Shift](https://arctic-shift.photon-reddit.com/).
5
Vanaf versie **0.3.0** zoekt en ontdekt de plugin **live op je telefoon**, met de Reddit-sessie uit de LNReader-WebView. Ieder account (jij of iemand anders die de plugin gebruikt) gebruikt zijn eigen cookies. De repo bevat daarnaast een statische metadata-index (titel, auteur, hoofdstukvolgorde, Reddit-IDs — geen verhaaltekst) als laatste fallback, en voor gebruik zonder login.
6
7
Hoofdstuktekst wordt pas opgehaald als je een hoofdstuk opent: eerst via Reddit-JSON (met diezelfde WebView-sessie), en bij een fout, 403 of loginpagina via [Arctic Shift](https://arctic-shift.photon-reddit.com/).
6
8
7
9
## In LNReader zetten
8
10
@@ -21,10 +23,14 @@ BlueGit serveert raw-bestanden als `text/plain`. Dat is voor LNReader geen probl
21
23
22
24
## Wat de plugin kan
23
25
24
- Bladeren (paginagewijs, nieuwste reeksen eerst) en zoeken op titel of auteur.
25
- Filters: reeksen / oneshots, NSFW verbergen of tonen.
26
- Een reeks openen met de volledige hoofdstuklijst.
26
- Bladeren door de live [wiki-serie-index van r/HFY](https://www.reddit.com/r/HFY/wiki/series/) (gecached in de plugin-opslag, standaard zes uur).
27
- Het tabblad **Latest** of het filter **Nieuw**: recente OC-posts, gegroepeerd tot reeksen.
28
- Live zoeken op r/HFY (`search.json` met `restrict_sr=1`): wiki-series en losse *Part 1*-posts worden tot één novel gegroepeerd.
29
- Een reeks openen: de hoofdstuklijst komt live uit de wiki-inhoudsopgave, of anders via next-links en titelgelijkenis.
27
30
- Een hoofdstuk lezen (eerst Reddit-JSON, anders Arctic Shift; onderaan het hoofdstuk staat welke bron is gebruikt).
31
- Filters: Wiki-series / Nieuw, reeksen / oneshots, NSFW verbergen of tonen.
32
33
Bij een fout, 403 of loginpagina valt zoeken, bladeren en de hoofdstuklijst terug op Arctic Shift, en daarna op de statische index in deze repo.
28
34
29
35
Elk verhaal zonder eigen omslag krijgt een **deterministische cover**: uit een hash van de titel volgt een kleurverloop of patroon, met de titel leesbaar erop. Dezelfde titel geeft altijd dezelfde cover; lange titels worden netjes afgebroken. De indexer schrijft een PNG (240×360) in `public/covers/`, omdat LNReader covers via expo-image laadt. Zonder Pillow valt hij terug op SVG. De covers worden bij het bouwen van de index meegenomen, ook in CI.
30
36
@@ -33,10 +39,11 @@ Elk verhaal zonder eigen omslag krijgt een **deterministische cover**: uit een h
33
39
34
40
Anonieme Reddit-JSON is sinds 2026 vaak geblokkeerd. Als je in LNReader op reddit.com inlogt, stuurt de plugin die cookies automatisch mee (dezelfde cookie-jar als de WebView; `fetchApi` gebruikt dezelfde User-Agent).
35
41
36
1. Werk de plugin bij als LNReader een update naar een nieuwere versie toont (bronnen verversen, daarna bijwerken).
42
1. Werk de plugin bij als LNReader een update naar **0.3.0** toont (bronnen verversen, daarna bijwerken).
37
43
2. Open de plugin **HFY Novels** → **Website** (of in een reeks/hoofdstuk **Open in WebView**). Je komt op r/HFY.
38
3. Log in op Reddit en sluit de WebView daarna af.
39
4. Open een hoofdstuk. Onderaan de tekst staat **Bron: Reddit** als het via je sessie lukte, of **Bron: Arctic Shift (fallback)** als Reddit een 403 of loginpagina teruggaf.
44
3. Log in op Reddit met *jouw* account en sluit de WebView daarna af. Iemand anders (bijvoorbeeld op een andere telefoon) logt met een eigen account in; de sessies blijven gescheiden.
45
4. Ga terug naar de plugin: **Popular** toont de wiki-series, **Latest** (of filter **Nieuw**) recente OC-posts. Zoek op een titel. Open een reeks en controleer of de hoofdstuklijst klopt, en open daarna een hoofdstuk.
46
5. Onderaan de hoofdstuktekst staat **Bron: Reddit** als het via je sessie lukte, of **Bron: Arctic Shift (fallback)** als Reddit een 403 of loginpagina teruggaf. Zonder login blijven bladeren en lezen werken via Arctic Shift en de statische index.
40
47
41
48
In de plugin-instellingen kun je de bron vastzetten op *Automatisch*, *Alleen Reddit* of *Alleen Arctic Shift*, en zo nodig de index-URL of Arctic Shift-basis-URL aanpassen.
42
49
@@ -48,7 +55,7 @@ Omdat anonieme Reddit-JSON (`/wiki/series.json` en `url/.json`) sinds 2026 geblo
48
55
49
56
De uitvoer staat in `public/`: een gesplitste catalogus, een zoekindex en per reeks een JSON-bestand met de hoofdstuklijst (geen verhaaltekst). Onderbroken runs hervatten via `public/.cache/` (alleen metadata en links).
50
57
51
BlueGit heeft geen geplande pipelines. Op `master` staat een handmatige job `update-index` (of start een web-pipeline). Die ververst de index incrementeel. Om de nieuwe index ook terug in de repo te zetten, voeg je het geheim `INDEX_PUSH_TOKEN` toe (een token met schrijfrechten op deze repo, niet het job-token). Zonder dat geheim blijft de index een artifact.
58
De plugin heeft geen dagelijkse pipeline nodig: nieuwe verhalen worden op de telefoon ontdekt. De statische index blijft als fallback bestaan en mag handmatig worden ververst. Op `master` staat daarvoor de job `update-index` (handmatig, of via een web-pipeline). Om het resultaat terug in de repo te zetten is het optionele geheim `INDEX_PUSH_TOKEN` nodig (een token met schrijfrechten, niet het job-token). Zonder dat geheim blijft de index een artifact.
52
59
53
60
Lokaal:
54
61
@@ -63,11 +70,12 @@ Zet `HFY_LIVE=1` bij de plugintest om `parseChapter` ook tegen de echte Arctic S
63
70
64
71
## Beperkingen
65
72
66
- De catalogus volgt de wiki, niet een eigen scan van de subreddit. De wiki is zelf een momentopname (Arctic Shift bewaart een revisie) en is niet altijd volledig of bijgewerkt. Ontbrekende subpagina’s in het archief betekenen vaak dat een serie nog geen hoofdstukken in de index krijgt.
67
- Hoofdstukken na het laatste wiki-deel worden via next-links en titelgelijkenis toegevoegd; die heuristiek kan een keten te vroeg stoppen of (zelden) een verkeerde link volgen.
68
- Hoofdstukken komen bij voorkeur van Reddit-JSON met je WebView-sessie. Zonder login (of bij een 403/blokpagina) valt de plugin terug op Arctic Shift, een onofficieel archief zonder uptime-garantie.
73
- Live zoeken en de wiki-index werken het best na inloggen in de WebView. Zonder sessie (of bij een 403) valt de plugin terug op Arctic Shift en daarna op de statische index, die niet elke nieuwe post bevat.
74
- De wiki-cache in de plugin-opslag is zes uur geldig; in die periode zie je geen verse wiki-wijzigingen tot de cache verloopt (of je de app-gegevens wist).
75
- Next-links en titelgelijkenis kunnen een keten te vroeg stoppen of (zelden) een verkeerde link volgen.
76
- Arctic Shift is een onofficieel archief zonder uptime-garantie. Titelzoeken op heel r/HFY wordt door die API soms geweigerd (te actieve subreddit).
69
77
- Auteurs houden het auteursrecht op hun verhalen. Deze repo host die teksten bewust niet. Gebruik is bedoeld voor persoonlijk lezen; een openbare herpublicatie van de teksten is iets anders.
70
- `--max-pages` is een budget voor API-verzoeken per run. Een eerste volledige index kan meerdere `update-index`-runs nodig hebben.
78
- `--max-pages` is een budget voor API-verzoeken bij een handmatige index-run.
71
79
72
80
Zie [docs/ASSUMPTIONS.md](docs/ASSUMPTIONS.md) voor de aannames en juridische kaders.
73
81
@@ -2,7 +2,9 @@2
2
3
Een [LNReader](https://www.lnreader.app/)-pluginbron voor verhalen van [r/HFY](https://www.reddit.com/r/HFY/). Deze repository is zelf de plugin-repository: LNReader laadt een `plugins.min.json` en een gebouwde plugin-JS via de raw-URL van BlueGit.3
Een [LNReader](https://www.lnreader.app/)-pluginbron voor verhalen van [r/HFY](https://www.reddit.com/r/HFY/). Deze repository is zelf de plugin-repository: LNReader laadt een `plugins.min.json` en een gebouwde plugin-JS via de raw-URL van BlueGit.4
4
5
De index bevat **alleen metadata** (titel, auteur, hoofdstukvolgorde, Reddit-IDs). De tekst van de verhalen staat er niet in. Die haalt de plugin pas op als je een hoofdstuk opent: eerst via Reddit-JSON (met de sessie uit de WebView), en bij een fout, 403 of loginpagina via [Arctic Shift](https://arctic-shift.photon-reddit.com/).5
Vanaf versie **0.3.0** zoekt en ontdekt de plugin **live op je telefoon**, met de Reddit-sessie uit de LNReader-WebView. Ieder account (jij of iemand anders die de plugin gebruikt) gebruikt zijn eigen cookies. De repo bevat daarnaast een statische metadata-index (titel, auteur, hoofdstukvolgorde, Reddit-IDs — geen verhaaltekst) als laatste fallback, en voor gebruik zonder login.6
7
Hoofdstuktekst wordt pas opgehaald als je een hoofdstuk opent: eerst via Reddit-JSON (met diezelfde WebView-sessie), en bij een fout, 403 of loginpagina via [Arctic Shift](https://arctic-shift.photon-reddit.com/).6
8
7
## In LNReader zetten9
## In LNReader zetten8
10
@@ -21,10 +23,14 @@ BlueGit serveert raw-bestanden als `text/plain`. Dat is voor LNReader geen probl21
23
22
## Wat de plugin kan24
## Wat de plugin kan23
25
24
- Bladeren (paginagewijs, nieuwste reeksen eerst) en zoeken op titel of auteur.26
- Bladeren door de live [wiki-serie-index van r/HFY](https://www.reddit.com/r/HFY/wiki/series/) (gecached in de plugin-opslag, standaard zes uur).25
- Filters: reeksen / oneshots, NSFW verbergen of tonen.27
- Het tabblad **Latest** of het filter **Nieuw**: recente OC-posts, gegroepeerd tot reeksen.26
- Een reeks openen met de volledige hoofdstuklijst.28
- Live zoeken op r/HFY (`search.json` met `restrict_sr=1`): wiki-series en losse *Part 1*-posts worden tot één novel gegroepeerd.29
- Een reeks openen: de hoofdstuklijst komt live uit de wiki-inhoudsopgave, of anders via next-links en titelgelijkenis.27
- Een hoofdstuk lezen (eerst Reddit-JSON, anders Arctic Shift; onderaan het hoofdstuk staat welke bron is gebruikt).30
- Een hoofdstuk lezen (eerst Reddit-JSON, anders Arctic Shift; onderaan het hoofdstuk staat welke bron is gebruikt).31
- Filters: Wiki-series / Nieuw, reeksen / oneshots, NSFW verbergen of tonen.32
33
Bij een fout, 403 of loginpagina valt zoeken, bladeren en de hoofdstuklijst terug op Arctic Shift, en daarna op de statische index in deze repo.28
34
29
Elk verhaal zonder eigen omslag krijgt een **deterministische cover**: uit een hash van de titel volgt een kleurverloop of patroon, met de titel leesbaar erop. Dezelfde titel geeft altijd dezelfde cover; lange titels worden netjes afgebroken. De indexer schrijft een PNG (240×360) in `public/covers/`, omdat LNReader covers via expo-image laadt. Zonder Pillow valt hij terug op SVG. De covers worden bij het bouwen van de index meegenomen, ook in CI.35
Elk verhaal zonder eigen omslag krijgt een **deterministische cover**: uit een hash van de titel volgt een kleurverloop of patroon, met de titel leesbaar erop. Dezelfde titel geeft altijd dezelfde cover; lange titels worden netjes afgebroken. De indexer schrijft een PNG (240×360) in `public/covers/`, omdat LNReader covers via expo-image laadt. Zonder Pillow valt hij terug op SVG. De covers worden bij het bouwen van de index meegenomen, ook in CI.30
36
@@ -33,10 +39,11 @@ Elk verhaal zonder eigen omslag krijgt een **deterministische cover**: uit een h33
39
34
Anonieme Reddit-JSON is sinds 2026 vaak geblokkeerd. Als je in LNReader op reddit.com inlogt, stuurt de plugin die cookies automatisch mee (dezelfde cookie-jar als de WebView; `fetchApi` gebruikt dezelfde User-Agent).40
Anonieme Reddit-JSON is sinds 2026 vaak geblokkeerd. Als je in LNReader op reddit.com inlogt, stuurt de plugin die cookies automatisch mee (dezelfde cookie-jar als de WebView; `fetchApi` gebruikt dezelfde User-Agent).35
41
36
1. Werk de plugin bij als LNReader een update naar een nieuwere versie toont (bronnen verversen, daarna bijwerken).42
1. Werk de plugin bij als LNReader een update naar **0.3.0** toont (bronnen verversen, daarna bijwerken).37
2. Open de plugin **HFY Novels** → **Website** (of in een reeks/hoofdstuk **Open in WebView**). Je komt op r/HFY.43
2. Open de plugin **HFY Novels** → **Website** (of in een reeks/hoofdstuk **Open in WebView**). Je komt op r/HFY.38
3. Log in op Reddit en sluit de WebView daarna af.44
3. Log in op Reddit met *jouw* account en sluit de WebView daarna af. Iemand anders (bijvoorbeeld op een andere telefoon) logt met een eigen account in; de sessies blijven gescheiden.39
4. Open een hoofdstuk. Onderaan de tekst staat **Bron: Reddit** als het via je sessie lukte, of **Bron: Arctic Shift (fallback)** als Reddit een 403 of loginpagina teruggaf.45
4. Ga terug naar de plugin: **Popular** toont de wiki-series, **Latest** (of filter **Nieuw**) recente OC-posts. Zoek op een titel. Open een reeks en controleer of de hoofdstuklijst klopt, en open daarna een hoofdstuk.46
5. Onderaan de hoofdstuktekst staat **Bron: Reddit** als het via je sessie lukte, of **Bron: Arctic Shift (fallback)** als Reddit een 403 of loginpagina teruggaf. Zonder login blijven bladeren en lezen werken via Arctic Shift en de statische index.40
47
41
In de plugin-instellingen kun je de bron vastzetten op *Automatisch*, *Alleen Reddit* of *Alleen Arctic Shift*, en zo nodig de index-URL of Arctic Shift-basis-URL aanpassen.48
In de plugin-instellingen kun je de bron vastzetten op *Automatisch*, *Alleen Reddit* of *Alleen Arctic Shift*, en zo nodig de index-URL of Arctic Shift-basis-URL aanpassen.42
49
@@ -48,7 +55,7 @@ Omdat anonieme Reddit-JSON (`/wiki/series.json` en `url/.json`) sinds 2026 geblo48
55
49
De uitvoer staat in `public/`: een gesplitste catalogus, een zoekindex en per reeks een JSON-bestand met de hoofdstuklijst (geen verhaaltekst). Onderbroken runs hervatten via `public/.cache/` (alleen metadata en links).56
De uitvoer staat in `public/`: een gesplitste catalogus, een zoekindex en per reeks een JSON-bestand met de hoofdstuklijst (geen verhaaltekst). Onderbroken runs hervatten via `public/.cache/` (alleen metadata en links).50
57
51
BlueGit heeft geen geplande pipelines. Op `master` staat een handmatige job `update-index` (of start een web-pipeline). Die ververst de index incrementeel. Om de nieuwe index ook terug in de repo te zetten, voeg je het geheim `INDEX_PUSH_TOKEN` toe (een token met schrijfrechten op deze repo, niet het job-token). Zonder dat geheim blijft de index een artifact.58
De plugin heeft geen dagelijkse pipeline nodig: nieuwe verhalen worden op de telefoon ontdekt. De statische index blijft als fallback bestaan en mag handmatig worden ververst. Op `master` staat daarvoor de job `update-index` (handmatig, of via een web-pipeline). Om het resultaat terug in de repo te zetten is het optionele geheim `INDEX_PUSH_TOKEN` nodig (een token met schrijfrechten, niet het job-token). Zonder dat geheim blijft de index een artifact.52
59
53
Lokaal:60
Lokaal:54
61
@@ -63,11 +70,12 @@ Zet `HFY_LIVE=1` bij de plugintest om `parseChapter` ook tegen de echte Arctic S63
70
64
## Beperkingen71
## Beperkingen65
72
66
- De catalogus volgt de wiki, niet een eigen scan van de subreddit. De wiki is zelf een momentopname (Arctic Shift bewaart een revisie) en is niet altijd volledig of bijgewerkt. Ontbrekende subpagina’s in het archief betekenen vaak dat een serie nog geen hoofdstukken in de index krijgt.73
- Live zoeken en de wiki-index werken het best na inloggen in de WebView. Zonder sessie (of bij een 403) valt de plugin terug op Arctic Shift en daarna op de statische index, die niet elke nieuwe post bevat.67
- Hoofdstukken na het laatste wiki-deel worden via next-links en titelgelijkenis toegevoegd; die heuristiek kan een keten te vroeg stoppen of (zelden) een verkeerde link volgen.74
- De wiki-cache in de plugin-opslag is zes uur geldig; in die periode zie je geen verse wiki-wijzigingen tot de cache verloopt (of je de app-gegevens wist).68
- Hoofdstukken komen bij voorkeur van Reddit-JSON met je WebView-sessie. Zonder login (of bij een 403/blokpagina) valt de plugin terug op Arctic Shift, een onofficieel archief zonder uptime-garantie.75
- Next-links en titelgelijkenis kunnen een keten te vroeg stoppen of (zelden) een verkeerde link volgen.76
- Arctic Shift is een onofficieel archief zonder uptime-garantie. Titelzoeken op heel r/HFY wordt door die API soms geweigerd (te actieve subreddit).69
- Auteurs houden het auteursrecht op hun verhalen. Deze repo host die teksten bewust niet. Gebruik is bedoeld voor persoonlijk lezen; een openbare herpublicatie van de teksten is iets anders.77
- Auteurs houden het auteursrecht op hun verhalen. Deze repo host die teksten bewust niet. Gebruik is bedoeld voor persoonlijk lezen; een openbare herpublicatie van de teksten is iets anders.70
- `--max-pages` is een budget voor API-verzoeken per run. Een eerste volledige index kan meerdere `update-index`-runs nodig hebben.78
- `--max-pages` is een budget voor API-verzoeken bij een handmatige index-run.71
79
72
Zie [docs/ASSUMPTIONS.md](docs/ASSUMPTIONS.md) voor de aannames en juridische kaders.80
Zie [docs/ASSUMPTIONS.md](docs/ASSUMPTIONS.md) voor de aannames en juridische kaders.73
81
@@ -22,11 +22,17 @@ De vaste URL die je in LNReader plakt, wijst naar `rev=master`, zodat een featur
22
22
23
23
Deze repo neemt het *principe* over (ketens van HFY-hoofdstukken vinden en leesbaar maken), niet de code. hfydl is GPL-3; onze indexer en plugin zijn opnieuw geschreven onder MIT. De lijst met series komt uit de r/HFY-wiki; hoofdstukken worden vanaf het eerste of index-deel gevolgd via next-links en titelgelijkenis (lexicale embedder, optioneel model2vec `minishlab/potion-base-8M`, cosine ≥ 0.5). Tags en part-nummers gaan van de titel af voordat het gemeenschappelijke voorvoegsel wordt bepaald.
24
24
25
## Bron van de series: de r/HFY-wiki, opgehaald via Arctic Shift
25
## Live catalogus op de telefoon, via de WebView-sessie
26
26
27
Een index opbouwen via de officiële Reddit Data API vereist OAuth, een geregistreerde app, een beschrijvende User-Agent, en 100 verzoeken per minuut per client-id. Verkeer zonder OAuth wordt geblokkeerd. Dat past slecht bij CI op een datacenter-IP en bij een plugin die vanaf telefoons zou authenticeren.
27
Vanaf plugin 0.3.0 haalt de plugin de catalogus live op, met `fetchApi`. Cookies uit de LNReader-WebView gaan automatisch mee naar reddit.com, per account op dat toestel. Er is geen Reddit-app, geen `client_credentials` en geen gedeelde bot-login nodig: ieder gebruikt zijn eigen inlog.
28
28
29
Daarom haalt de indexer de wiki en de post-metadata op bij [Arctic Shift](https://github.com/ArthurHeitmann/arctic_shift), een publiek archief met een eigen rate limit: `/api/subreddits/wikis` voor https://www.reddit.com/r/HFY/wiki/series/ en de gelinkte subpagina’s, en `/api/posts/ids` in plaats van `url/.json`. De plugin gebruikt dezelfde API voor de hoofdstuktekst op het moment van lezen. Arctic Shift is een onofficiële, best-effort-dienst. Als die wegvalt, blijft bladeren in de laatste index werken, maar openen van hoofdstukken niet.
29
Volgorde voor zoeken, de wiki-index, recente OC-posts en de hoofdstuklijst: eerst Reddit-JSON (`/r/HFY/search.json?restrict_sr=1`, `/r/HFY/wiki/series.json` en subpagina’s, `/r/HFY/new.json`, daarna next-links), bij een fout, 403 of login-/blokpagina [Arctic Shift](https://github.com/ArthurHeitmann/arctic_shift), en als laatste de statische metadata-index in deze repo. De wiki-index wordt tot zes uur in `@libs/storage` bewaard (`wikiSeriesIndex`), zodat die niet bij elk bladerscherm opnieuw wordt opgehaald.
30
31
## Bron van de statische fallback-index: de r/HFY-wiki, via Arctic Shift
32
33
Een index in CI opbouwen via de officiële Reddit Data API vereist OAuth, een geregistreerde app, een beschrijvende User-Agent, en 100 verzoeken per minuut per client-id. Verkeer zonder OAuth wordt geblokkeerd. Dat past slecht bij CI op een datacenter-IP. Die route is niet ingebouwd; de plugin authenticeert op de telefoon.
34
35
Daarom haalt de (handmatige) indexer de wiki en de post-metadata op bij Arctic Shift: `/api/subreddits/wikis` voor https://www.reddit.com/r/HFY/wiki/series/ en de gelinkte subpagina’s, en `/api/posts/ids` in plaats van `url/.json`. Arctic Shift is een onofficiële, best-effort-dienst. Als die wegvalt, blijft de live Reddit-route op de telefoon werken zolang de gebruiker is ingelogd; zonder login blijft bladeren in de laatste statische index werken.
30
36
31
37
Arctic Shift heeft (stand november 2026) de volledige serie-index, maar slechts een deel van de serie-subpagina’s (in een steekproef ongeveer een op de zes). Zonder subpagina is er vaak geen eerste-deel-link; titelzoeken op heel r/HFY wordt door de API geweigerd (HTTP 422, te actieve subreddit). Als de auteur wél bekend is, zoeken we op auteur + titel. Series die zo geen zaadpost krijgen, blijven buiten de index tot de wiki-subpagina in het archief verschijnt.
32
38
@@ -44,22 +50,22 @@ We slaan in `public/` geen `selftext` op. Selftext wordt alleen in geheugen gebr
44
50
45
51
r/HFY heeft honderdduizenden posts en ruim 385.000 leden (stand november 2026). We scannen die subreddit niet pagina voor pagina; de wiki is de catalogus. `--max-pages` is een budget voor API-verzoeken per run (wiki-batches en post-IDs). `--full` zet dat budget uit. Incrementeel (`--incremental`) behoudt bestaande reeksen en voegt nieuwe wiki-items of volgende hoofdstukken toe.
46
52
47
De eerste index in de repo is een momentopname van de wiki-reeksen die we konden lopen. Nieuwe hoofdstukken komen erbij zodra iemand `update-index` start of de indexer lokaal draait. Reeksen zonder zaad in het archief ontbreken tot een latere run.
53
De statische index in de repo is een momentopname en is niet nodig voor dagelijkse updates: nieuwe verhalen worden op de telefoon ontdekt. Een handmatige `update-index`-run of een lokale indexer-run ververst alleen de fallback. Reeksen die live noch in die snapshot staan, ontbreken tot een van beide ze vindt.
48
54
49
55
Statusvelden (`Ongoing`, `On Hiatus`, `Completed`) zijn een schatting (oneshot = voltooid, meer dan 180 dagen stil = on hold). Auteurs markeren dat niet betrouwbaar in flairs.
50
56
51
57
## CI en geheimen
52
58
53
59
- Tests draaien op elke branch. Er gaan geen geheimen in de logs; tokens worden niet geprint.
54
- `CI_JOB_TOKEN` mag niet pushen. Terugschrijven van de index kan alleen met het optionele geheim `INDEX_PUSH_TOKEN`.
60
- `CI_JOB_TOKEN` mag niet pushen. Een handmatige index terugschrijven kan alleen met het optionele geheim `INDEX_PUSH_TOKEN`. Er is geen dagelijkse pipeline-trigger; die is bewust niet gebouwd.
55
61
- Er is geen `--delete` in een deploy, en er wordt niets gewijzigd op auth.timfalken.com.
56
62
- Tags en releases worden niet automatisch gezet.
57
63
58
64
## Plugin-runtime
59
65
60
De tests evalueren de plugin met `new Function` en een begrensde `require`, en serveren de fixture-index over HTTP als `text/plain`, hetzelfde type als BlueGit-raw. `parseChapter` wordt getest met een mock van Reddit-JSON (succes, HTTP 403 en login-HTML) en een fallback naar Arctic Shift, en optioneel live (`HFY_LIVE=1`). We hebben de plugin niet in de LNReader-Android-app zelf geïnstalleerd.
66
De tests evalueren de plugin met `new Function` en een begrensde `require`, en serveren de fixture-index over HTTP als `text/plain`, hetzelfde type als BlueGit-raw. `popularNovels`, `searchNovels`, `parseNovel` en `parseChapter` worden getest met mocks van Reddit-JSON (succes, HTTP 403) en Arctic Shift, plus de statische fallback, en optioneel live (`HFY_LIVE=1` voor `parseChapter`). We hebben de plugin niet in de LNReader-Android-app zelf geïnstalleerd.
61
67
62
LNReader's `fetchApi` is native `fetch` plus de app-User-Agent. Cookies uit de WebView worden door React Native automatisch meegestuurd naar hetzelfde domein; je hoeft geen `Cookie`-header te zetten. `webStorageUtilized` is niet gezet: dat kopieert alleen `localStorage`/`sessionStorage`, terwijl Reddit-login in cookies zit. De plugin-`site` is `https://www.reddit.com/r/HFY/`, zodat **Website** / **Open in WebView** op reddit.com opent. `resolveUrl` zet een reeks- of hoofdstukpad om naar `https://www.reddit.com/r/HFY/comments/<id>/`.
68
LNReader's `fetchApi` is native `fetch` plus de app-User-Agent. Cookies uit de WebView worden door React Native automatisch meegestuurd naar hetzelfde domein; je hoeft geen `Cookie`-header te zetten. `webStorageUtilized` is niet gezet: dat kopieert alleen `localStorage`/`sessionStorage`, terwijl Reddit-login in cookies zit. De plugin-`site` is `https://www.reddit.com/r/HFY/`, zodat **Website** / **Open in WebView** op reddit.com opent. `resolveUrl` zet een reeks- of hoofdstukpad om naar `https://www.reddit.com/r/HFY/comments/<id>/`, en een wiki-pad naar `https://www.reddit.com/r/HFY/wiki/series/<slug>/`.
63
69
64
70
LNReader eist dat de repository-URL `plugins.min.json` bevat (regex in `SettingsRepositoryScreen.tsx`).
65
71
@@ -22,11 +22,17 @@ De vaste URL die je in LNReader plakt, wijst naar `rev=master`, zodat een featur22
22
23
Deze repo neemt het *principe* over (ketens van HFY-hoofdstukken vinden en leesbaar maken), niet de code. hfydl is GPL-3; onze indexer en plugin zijn opnieuw geschreven onder MIT. De lijst met series komt uit de r/HFY-wiki; hoofdstukken worden vanaf het eerste of index-deel gevolgd via next-links en titelgelijkenis (lexicale embedder, optioneel model2vec `minishlab/potion-base-8M`, cosine ≥ 0.5). Tags en part-nummers gaan van de titel af voordat het gemeenschappelijke voorvoegsel wordt bepaald.23
Deze repo neemt het *principe* over (ketens van HFY-hoofdstukken vinden en leesbaar maken), niet de code. hfydl is GPL-3; onze indexer en plugin zijn opnieuw geschreven onder MIT. De lijst met series komt uit de r/HFY-wiki; hoofdstukken worden vanaf het eerste of index-deel gevolgd via next-links en titelgelijkenis (lexicale embedder, optioneel model2vec `minishlab/potion-base-8M`, cosine ≥ 0.5). Tags en part-nummers gaan van de titel af voordat het gemeenschappelijke voorvoegsel wordt bepaald.24
24
25
## Bron van de series: de r/HFY-wiki, opgehaald via Arctic Shift25
## Live catalogus op de telefoon, via de WebView-sessie26
26
27
Een index opbouwen via de officiële Reddit Data API vereist OAuth, een geregistreerde app, een beschrijvende User-Agent, en 100 verzoeken per minuut per client-id. Verkeer zonder OAuth wordt geblokkeerd. Dat past slecht bij CI op een datacenter-IP en bij een plugin die vanaf telefoons zou authenticeren.27
Vanaf plugin 0.3.0 haalt de plugin de catalogus live op, met `fetchApi`. Cookies uit de LNReader-WebView gaan automatisch mee naar reddit.com, per account op dat toestel. Er is geen Reddit-app, geen `client_credentials` en geen gedeelde bot-login nodig: ieder gebruikt zijn eigen inlog.28
28
29
Daarom haalt de indexer de wiki en de post-metadata op bij [Arctic Shift](https://github.com/ArthurHeitmann/arctic_shift), een publiek archief met een eigen rate limit: `/api/subreddits/wikis` voor https://www.reddit.com/r/HFY/wiki/series/ en de gelinkte subpagina’s, en `/api/posts/ids` in plaats van `url/.json`. De plugin gebruikt dezelfde API voor de hoofdstuktekst op het moment van lezen. Arctic Shift is een onofficiële, best-effort-dienst. Als die wegvalt, blijft bladeren in de laatste index werken, maar openen van hoofdstukken niet.29
Volgorde voor zoeken, de wiki-index, recente OC-posts en de hoofdstuklijst: eerst Reddit-JSON (`/r/HFY/search.json?restrict_sr=1`, `/r/HFY/wiki/series.json` en subpagina’s, `/r/HFY/new.json`, daarna next-links), bij een fout, 403 of login-/blokpagina [Arctic Shift](https://github.com/ArthurHeitmann/arctic_shift), en als laatste de statische metadata-index in deze repo. De wiki-index wordt tot zes uur in `@libs/storage` bewaard (`wikiSeriesIndex`), zodat die niet bij elk bladerscherm opnieuw wordt opgehaald.30
31
## Bron van de statische fallback-index: de r/HFY-wiki, via Arctic Shift32
33
Een index in CI opbouwen via de officiële Reddit Data API vereist OAuth, een geregistreerde app, een beschrijvende User-Agent, en 100 verzoeken per minuut per client-id. Verkeer zonder OAuth wordt geblokkeerd. Dat past slecht bij CI op een datacenter-IP. Die route is niet ingebouwd; de plugin authenticeert op de telefoon.34
35
Daarom haalt de (handmatige) indexer de wiki en de post-metadata op bij Arctic Shift: `/api/subreddits/wikis` voor https://www.reddit.com/r/HFY/wiki/series/ en de gelinkte subpagina’s, en `/api/posts/ids` in plaats van `url/.json`. Arctic Shift is een onofficiële, best-effort-dienst. Als die wegvalt, blijft de live Reddit-route op de telefoon werken zolang de gebruiker is ingelogd; zonder login blijft bladeren in de laatste statische index werken.30
36
31
Arctic Shift heeft (stand november 2026) de volledige serie-index, maar slechts een deel van de serie-subpagina’s (in een steekproef ongeveer een op de zes). Zonder subpagina is er vaak geen eerste-deel-link; titelzoeken op heel r/HFY wordt door de API geweigerd (HTTP 422, te actieve subreddit). Als de auteur wél bekend is, zoeken we op auteur + titel. Series die zo geen zaadpost krijgen, blijven buiten de index tot de wiki-subpagina in het archief verschijnt.37
Arctic Shift heeft (stand november 2026) de volledige serie-index, maar slechts een deel van de serie-subpagina’s (in een steekproef ongeveer een op de zes). Zonder subpagina is er vaak geen eerste-deel-link; titelzoeken op heel r/HFY wordt door de API geweigerd (HTTP 422, te actieve subreddit). Als de auteur wél bekend is, zoeken we op auteur + titel. Series die zo geen zaadpost krijgen, blijven buiten de index tot de wiki-subpagina in het archief verschijnt.32
38
@@ -44,22 +50,22 @@ We slaan in `public/` geen `selftext` op. Selftext wordt alleen in geheugen gebr44
50
45
r/HFY heeft honderdduizenden posts en ruim 385.000 leden (stand november 2026). We scannen die subreddit niet pagina voor pagina; de wiki is de catalogus. `--max-pages` is een budget voor API-verzoeken per run (wiki-batches en post-IDs). `--full` zet dat budget uit. Incrementeel (`--incremental`) behoudt bestaande reeksen en voegt nieuwe wiki-items of volgende hoofdstukken toe.51
r/HFY heeft honderdduizenden posts en ruim 385.000 leden (stand november 2026). We scannen die subreddit niet pagina voor pagina; de wiki is de catalogus. `--max-pages` is een budget voor API-verzoeken per run (wiki-batches en post-IDs). `--full` zet dat budget uit. Incrementeel (`--incremental`) behoudt bestaande reeksen en voegt nieuwe wiki-items of volgende hoofdstukken toe.46
52
47
De eerste index in de repo is een momentopname van de wiki-reeksen die we konden lopen. Nieuwe hoofdstukken komen erbij zodra iemand `update-index` start of de indexer lokaal draait. Reeksen zonder zaad in het archief ontbreken tot een latere run.53
De statische index in de repo is een momentopname en is niet nodig voor dagelijkse updates: nieuwe verhalen worden op de telefoon ontdekt. Een handmatige `update-index`-run of een lokale indexer-run ververst alleen de fallback. Reeksen die live noch in die snapshot staan, ontbreken tot een van beide ze vindt.48
54
49
Statusvelden (`Ongoing`, `On Hiatus`, `Completed`) zijn een schatting (oneshot = voltooid, meer dan 180 dagen stil = on hold). Auteurs markeren dat niet betrouwbaar in flairs.55
Statusvelden (`Ongoing`, `On Hiatus`, `Completed`) zijn een schatting (oneshot = voltooid, meer dan 180 dagen stil = on hold). Auteurs markeren dat niet betrouwbaar in flairs.50
56
51
## CI en geheimen57
## CI en geheimen52
58
53
- Tests draaien op elke branch. Er gaan geen geheimen in de logs; tokens worden niet geprint.59
- Tests draaien op elke branch. Er gaan geen geheimen in de logs; tokens worden niet geprint.54
- `CI_JOB_TOKEN` mag niet pushen. Terugschrijven van de index kan alleen met het optionele geheim `INDEX_PUSH_TOKEN`.60
- `CI_JOB_TOKEN` mag niet pushen. Een handmatige index terugschrijven kan alleen met het optionele geheim `INDEX_PUSH_TOKEN`. Er is geen dagelijkse pipeline-trigger; die is bewust niet gebouwd.55
- Er is geen `--delete` in een deploy, en er wordt niets gewijzigd op auth.timfalken.com.61
- Er is geen `--delete` in een deploy, en er wordt niets gewijzigd op auth.timfalken.com.56
- Tags en releases worden niet automatisch gezet.62
- Tags en releases worden niet automatisch gezet.57
63
58
## Plugin-runtime64
## Plugin-runtime59
65
60
De tests evalueren de plugin met `new Function` en een begrensde `require`, en serveren de fixture-index over HTTP als `text/plain`, hetzelfde type als BlueGit-raw. `parseChapter` wordt getest met een mock van Reddit-JSON (succes, HTTP 403 en login-HTML) en een fallback naar Arctic Shift, en optioneel live (`HFY_LIVE=1`). We hebben de plugin niet in de LNReader-Android-app zelf geïnstalleerd.66
De tests evalueren de plugin met `new Function` en een begrensde `require`, en serveren de fixture-index over HTTP als `text/plain`, hetzelfde type als BlueGit-raw. `popularNovels`, `searchNovels`, `parseNovel` en `parseChapter` worden getest met mocks van Reddit-JSON (succes, HTTP 403) en Arctic Shift, plus de statische fallback, en optioneel live (`HFY_LIVE=1` voor `parseChapter`). We hebben de plugin niet in de LNReader-Android-app zelf geïnstalleerd.61
67
62
LNReader's `fetchApi` is native `fetch` plus de app-User-Agent. Cookies uit de WebView worden door React Native automatisch meegestuurd naar hetzelfde domein; je hoeft geen `Cookie`-header te zetten. `webStorageUtilized` is niet gezet: dat kopieert alleen `localStorage`/`sessionStorage`, terwijl Reddit-login in cookies zit. De plugin-`site` is `https://www.reddit.com/r/HFY/`, zodat **Website** / **Open in WebView** op reddit.com opent. `resolveUrl` zet een reeks- of hoofdstukpad om naar `https://www.reddit.com/r/HFY/comments/<id>/`.68
LNReader's `fetchApi` is native `fetch` plus de app-User-Agent. Cookies uit de WebView worden door React Native automatisch meegestuurd naar hetzelfde domein; je hoeft geen `Cookie`-header te zetten. `webStorageUtilized` is niet gezet: dat kopieert alleen `localStorage`/`sessionStorage`, terwijl Reddit-login in cookies zit. De plugin-`site` is `https://www.reddit.com/r/HFY/`, zodat **Website** / **Open in WebView** op reddit.com opent. `resolveUrl` zet een reeks- of hoofdstukpad om naar `https://www.reddit.com/r/HFY/comments/<id>/`, en een wiki-pad naar `https://www.reddit.com/r/HFY/wiki/series/<slug>/`.63
69
64
LNReader eist dat de repository-URL `plugins.min.json` bevat (regex in `SettingsRepositoryScreen.tsx`).70
LNReader eist dat de repository-URL `plugins.min.json` bevat (regex in `SettingsRepositoryScreen.tsx`).65
71
@@ -1,14 +1,18 @@
1
1
"use strict";
2
2
3
3
/**
4
* LNReader-plugin voor de zelfgehoste HFY-Novels-index.
4
* LNReader-plugin voor r/HFY.
5
5
*
6
* De catalogus (titels, auteurs, hoofdstuklijsten) komt uit een statische
7
* JSON-index. De verhaaltekst zelf staat niet in die index: parseChapter
8
* probeert eerst de echte Reddit-JSON (www.reddit.com/.../.json, daarna
9
* old.reddit.com) via fetchApi, zodat cookies uit de LNReader-WebView
10
* meegaan na inloggen op reddit.com. Bij een fout, 403 of login-/blokpagina
11
* valt de plugin terug op Arctic Shift.
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.
12
16
*
13
17
* Dit bestand is CommonJS, zoals de officiële gecompileerde plugins:
14
18
* require(...) uit de LNReader-allowlist, en `exports.default = instance`.
@@ -27,6 +31,11 @@ const DEFAULT_INDEX_BASE =
27
31
const DEFAULT_INDEX_REV = "master";
28
32
const DEFAULT_CHAPTER_SOURCE = "https://arctic-shift.photon-reddit.com";
29
33
const PAGE_FALLBACK_SIZE = 50;
34
const WIKI_CACHE_KEY = "wikiSeriesIndex";
35
const WIKI_CACHE_MS = 6 * 60 * 60 * 1000;
36
const REDDIT_WWW = "https://www.reddit.com";
37
const REDDIT_OLD = "https://old.reddit.com";
38
const WIKI_INDEX_PATH = "/r/HFY/wiki/series";
30
39
31
40
function setting(key, fallback) {
32
41
try {
@@ -96,13 +105,22 @@ class HFYNovelsPlugin {
96
105
constructor() {
97
106
this.id = "hfyNovels";
98
107
this.name = "HFY Novels";
99
this.version = "0.2.0";
108
this.version = "0.3.0";
100
109
this.site = "https://www.reddit.com/r/HFY/";
101
110
this.icon = "icon.png";
102
111
// Geen webStorageUtilized: Reddit-login zit in cookies. LNReader's
103
112
// fetchApi (native fetch) stuurt de WebView-cookies automatisch mee
104
113
// voor hetzelfde domein; localStorage is daarvoor niet nodig.
105
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
},
106
124
kind: {
107
125
value: "all",
108
126
label: "Type",
@@ -126,7 +144,7 @@ class HFYNovelsPlugin {
126
144
this.pluginSettings = {
127
145
indexBaseUrl: {
128
146
value: DEFAULT_INDEX_BASE,
129
label: "Basis-URL van de index",
147
label: "Basis-URL van de statische index (laatste fallback)",
130
148
type: "Text",
131
149
},
132
150
indexRev: {
@@ -136,12 +154,12 @@ class HFYNovelsPlugin {
136
154
},
137
155
chapterSource: {
138
156
value: "auto",
139
label: "Bron voor hoofdstuktekst",
157
label: "Bron voor catalogus en hoofdstukken",
140
158
type: "Select",
141
159
options: [
142
{ label: "Automatisch (Reddit, anders Arctic Shift)", value: "auto" },
160
{ label: "Automatisch (Reddit, anders Arctic Shift, anders index)", value: "auto" },
143
161
{ label: "Alleen Reddit", value: "reddit" },
144
{ label: "Alleen Arctic Shift", value: "arctic" },
162
{ label: "Alleen Arctic Shift (daarna index)", value: "arctic" },
145
163
],
146
164
},
147
165
chapterSourceUrl: {
@@ -193,16 +211,20 @@ class HFYNovelsPlugin {
193
211
* Maakt "Open in WebView" op een reeks of hoofdstuk tot een Reddit-URL,
194
212
* zodat je daar kunt inloggen. Zonder deze functie zou LNReader
195
213
* site+path plakken (bijv. /r/HFY/series/id), wat geen geldige Reddit-pagina is.
196
* Het reeks-id is de oudste post (eerste deel).
214
* Het reeks-id is de oudste post (eerste deel), of een wiki-slug.
197
215
*/
198
216
resolveUrl(path, isNovel) {
199
217
const raw = String(path || "").trim();
200
218
if (/^https?:\/\//i.test(raw)) {
201
219
return raw;
202
220
}
221
const wikiSlug = wikiSlugFromPath(raw);
222
if (wikiSlug) {
223
return REDDIT_WWW + "/r/HFY/wiki/series/" + wikiSlug + "/";
224
}
203
225
const id = chapterIdFromPath(raw) || chapterIdFromPath(raw.replace(/^series\//, "chapter/"));
204
226
if (id) {
205
return "https://www.reddit.com/r/HFY/comments/" + id + "/";
227
return REDDIT_WWW + "/r/HFY/comments/" + id + "/";
206
228
}
207
229
return this.site;
208
230
}
@@ -229,10 +251,162 @@ class HFYNovelsPlugin {
229
251
async popularNovels(pageNo, options) {
230
252
const opts = options || {};
231
253
const filters = opts.filters || {};
232
const showLatest = !!opts.showLatestNovels;
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
}
233
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
}
234
404
235
// Zonder extra filters is de catalogus al op update-datum gesorteerd.
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);
236
410
const usePagedCatalog =
237
411
!showLatest &&
238
412
(!filters.kind || filters.kind.value === "all") &&
@@ -251,16 +425,14 @@ class HFYNovelsPlugin {
251
425
}
252
426
253
427
const search = await readJson(this.indexUrl("search-index.json"), "zoekindex");
254
let items = (search.novels || []).filter((n) => this.matchesFilters(n, filters));
428
const items = (search.novels || []).filter((n) => this.matchesFilters(n, filters));
255
429
const size = (await this.loadMeta()).pageSize || PAGE_FALLBACK_SIZE;
256
430
const start = (page - 1) * size;
257
431
return items.slice(start, start + size).map((n) => toNovelItem(n, this));
258
432
}
259
433
260
async searchNovels(searchTerm, pageNo) {
261
const term = String(searchTerm || "")
262
.trim()
263
.toLowerCase();
434
async staticSearch(searchTerm, pageNo) {
435
const term = String(searchTerm || "").trim().toLowerCase();
264
436
if (!term) {
265
437
return [];
266
438
}
@@ -271,23 +443,11 @@ class HFYNovelsPlugin {
271
443
const author = String(n.author || "").toLowerCase();
272
444
return name.includes(term) || author.includes(term);
273
445
});
274
const size = PAGE_FALLBACK_SIZE;
275
const start = (page - 1) * size;
276
return hits.slice(start, start + size).map((n) => toNovelItem(n, this));
277
}
278
279
seriesFileFor(novelPath) {
280
const id = String(novelPath || "")
281
.replace(/^series\//, "")
282
.replace(/\/+$/, "");
283
if (!id) {
284
throw new Error("HFY Novels: leeg reeks-pad");
285
}
286
const prefix = id.charAt(0).toLowerCase() || "x";
287
return "series/" + prefix + "/" + id + ".json";
446
const start = (page - 1) * PAGE_FALLBACK_SIZE;
447
return hits.slice(start, start + PAGE_FALLBACK_SIZE).map((n) => toNovelItem(n, this));
288
448
}
289
449
290
async parseNovel(novelPath) {
450
async staticParseNovel(novelPath) {
291
451
const data = await readJson(this.indexUrl(this.seriesFileFor(novelPath)), "reeks " + novelPath);
292
452
return {
293
453
path: data.path || novelPath,
@@ -307,43 +467,927 @@ class HFYNovelsPlugin {
307
467
};
308
468
}
309
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
}
310
480
311
async parseChapter(chapterPath) {
312
const id = chapterIdFromPath(chapterPath);
313
if (!id) {
314
throw new Error("HFY Novels: ongeldig hoofdstuk-id");
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" };
315
604
}
316
const mode = this.chapterMode();
317
let redditError = "";
318
605
if (mode !== "arctic") {
319
const reddit = await tryRedditChapter(id);
606
const reddit = await fetchRedditWikiMarkdown(WIKI_INDEX_PATH);
320
607
if (reddit.ok) {
321
return renderChapter(reddit.post, "Reddit");
608
const entries = parseSeriesIndex(reddit.markdown);
609
if (entries.length) {
610
writeWikiCache(entries);
611
return { ok: true, entries: entries, source: "reddit" };
612
}
322
613
}
323
redditError = reddit.reason || "onbekende fout";
324
614
if (mode === "reddit") {
325
throw new Error(
326
"HFY Novels: Reddit gaf geen hoofdstuk (" +
327
redditError +
328
"). Log in via Open in WebView op reddit.com en probeer het opnieuw.",
329
);
615
return { ok: false, reason: (reddit && reddit.reason) || "lege wiki-index" };
330
616
}
331
617
}
332
const arctic = await tryArcticChapter(this.chapterSource(), id);
618
const arctic = await fetchArcticWikiPage(this.chapterSource(), WIKI_INDEX_PATH);
333
619
if (arctic.ok) {
334
const suffix = mode === "arctic" ? "Arctic Shift" : "Arctic Shift (fallback)";
335
return renderChapter(arctic.post, suffix);
620
const entries = parseSeriesIndex(arctic.markdown);
621
if (entries.length) {
622
writeWikiCache(entries);
623
return { ok: true, entries: entries, source: "arctic" };
624
}
336
625
}
337
if (redditError) {
338
throw new Error(
339
"HFY Novels: Reddit (" +
340
redditError +
341
") en Arctic Shift (" +
342
(arctic.reason || "geen tekst") +
343
") faalden allebei.",
344
);
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
}
345
639
}
346
throw new Error("HFY Novels: kon hoofdstuk " + id + " niet laden (" + (arctic.reason || "geen tekst") + ")");
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
747
function 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
763
function 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
776
function 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
787
function 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
805
function 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
814
function 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
844
function 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
877
function 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
894
function cleanHeading(raw) {
895
let text = String(raw || "").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1");
896
text = text.replace(/[*_`]+/g, "");
897
return text.replace(/\s+/g, " ").trim();
898
}
899
900
function 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
908
function 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
926
function 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
936
function 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
959
function 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
971
function 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
980
function 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
988
function sameAuthor(a, b) {
989
return String((a && a.author) || "").toLowerCase() === String((b && b.author) || "").toLowerCase();
990
}
991
992
function 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
1003
function 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
1025
function 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
1039
function 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
1045
function 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
1066
function 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
1085
function 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
1093
function 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
1118
function 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
1205
function 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
1221
async 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
1240
async 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
1263
async 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
1277
async 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
1290
async 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
1300
async 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
1317
function 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
1329
async 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
1348
async 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
1369
async 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
1380
async 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) };
347
1391
}
348
1392
}
349
1393
@@ -358,10 +1402,7 @@ function chapterIdFromPath(chapterPath) {
358
1402
359
1403
function redditJsonUrls(id) {
360
1404
const path = "/r/HFY/comments/" + id + "/.json?raw_json=1";
361
return [
362
"https://www.reddit.com" + path,
363
"https://old.reddit.com" + path,
364
];
1405
return [REDDIT_WWW + path, REDDIT_OLD + path];
365
1406
}
366
1407
367
1408
function looksLikeHtml(text) {
@@ -1,14 +1,18 @@1
"use strict";1
"use strict";2
2
3
/**3
/**4
* LNReader-plugin voor de zelfgehoste HFY-Novels-index.4
* LNReader-plugin voor r/HFY.5
*5
*6
* De catalogus (titels, auteurs, hoofdstuklijsten) komt uit een statische6
* De catalogus wordt live opgebouwd op de telefoon, met de Reddit-sessie7
* JSON-index. De verhaaltekst zelf staat niet in die index: parseChapter7
* uit de LNReader-WebView (elk account heeft zijn eigen cookies):8
* probeert eerst de echte Reddit-JSON (www.reddit.com/.../.json, daarna8
* - popularNovels: de wiki-serie-index, plus 'Nieuw' / Latest met recente OC-posts9
* old.reddit.com) via fetchApi, zodat cookies uit de LNReader-WebView9
* - searchNovels: r/HFY/search.json (restrict_sr=1), plus hits uit de wiki,10
* meegaan na inloggen op reddit.com. Bij een fout, 403 of login-/blokpagina10
* waarbij series en losse Part-1-posts tot één novel worden gegroepeerd11
* valt de plugin terug op Arctic Shift.11
* - parseNovel: wiki-inhoudsopgave, anders next-links met titelgelijkenis12
*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.12
*16
*13
* Dit bestand is CommonJS, zoals de officiële gecompileerde plugins:17
* Dit bestand is CommonJS, zoals de officiële gecompileerde plugins:14
* require(...) uit de LNReader-allowlist, en `exports.default = instance`.18
* require(...) uit de LNReader-allowlist, en `exports.default = instance`.@@ -27,6 +31,11 @@ const DEFAULT_INDEX_BASE =27
const DEFAULT_INDEX_REV = "master";31
const DEFAULT_INDEX_REV = "master";28
const DEFAULT_CHAPTER_SOURCE = "https://arctic-shift.photon-reddit.com";32
const DEFAULT_CHAPTER_SOURCE = "https://arctic-shift.photon-reddit.com";29
const PAGE_FALLBACK_SIZE = 50;33
const PAGE_FALLBACK_SIZE = 50;34
const WIKI_CACHE_KEY = "wikiSeriesIndex";35
const WIKI_CACHE_MS = 6 * 60 * 60 * 1000;36
const REDDIT_WWW = "https://www.reddit.com";37
const REDDIT_OLD = "https://old.reddit.com";38
const WIKI_INDEX_PATH = "/r/HFY/wiki/series";30
39
31
function setting(key, fallback) {40
function setting(key, fallback) {32
try {41
try {@@ -96,13 +105,22 @@ class HFYNovelsPlugin {96
constructor() {105
constructor() {97
this.id = "hfyNovels";106
this.id = "hfyNovels";98
this.name = "HFY Novels";107
this.name = "HFY Novels";99
this.version = "0.2.0";108
this.version = "0.3.0";100
this.site = "https://www.reddit.com/r/HFY/";109
this.site = "https://www.reddit.com/r/HFY/";101
this.icon = "icon.png";110
this.icon = "icon.png";102
// Geen webStorageUtilized: Reddit-login zit in cookies. LNReader's111
// Geen webStorageUtilized: Reddit-login zit in cookies. LNReader's103
// fetchApi (native fetch) stuurt de WebView-cookies automatisch mee112
// fetchApi (native fetch) stuurt de WebView-cookies automatisch mee104
// voor hetzelfde domein; localStorage is daarvoor niet nodig.113
// voor hetzelfde domein; localStorage is daarvoor niet nodig.105
this.filters = {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
},106
kind: {124
kind: {107
value: "all",125
value: "all",108
label: "Type",126
label: "Type",@@ -126,7 +144,7 @@ class HFYNovelsPlugin {126
this.pluginSettings = {144
this.pluginSettings = {127
indexBaseUrl: {145
indexBaseUrl: {128
value: DEFAULT_INDEX_BASE,146
value: DEFAULT_INDEX_BASE,129
label: "Basis-URL van de index",147
label: "Basis-URL van de statische index (laatste fallback)",130
type: "Text",148
type: "Text",131
},149
},132
indexRev: {150
indexRev: {@@ -136,12 +154,12 @@ class HFYNovelsPlugin {136
},154
},137
chapterSource: {155
chapterSource: {138
value: "auto",156
value: "auto",139
label: "Bron voor hoofdstuktekst",157
label: "Bron voor catalogus en hoofdstukken",140
type: "Select",158
type: "Select",141
options: [159
options: [142
{ label: "Automatisch (Reddit, anders Arctic Shift)", value: "auto" },160
{ label: "Automatisch (Reddit, anders Arctic Shift, anders index)", value: "auto" },143
{ label: "Alleen Reddit", value: "reddit" },161
{ label: "Alleen Reddit", value: "reddit" },144
{ label: "Alleen Arctic Shift", value: "arctic" },162
{ label: "Alleen Arctic Shift (daarna index)", value: "arctic" },145
],163
],146
},164
},147
chapterSourceUrl: {165
chapterSourceUrl: {@@ -193,16 +211,20 @@ class HFYNovelsPlugin {193
* Maakt "Open in WebView" op een reeks of hoofdstuk tot een Reddit-URL,211
* Maakt "Open in WebView" op een reeks of hoofdstuk tot een Reddit-URL,194
* zodat je daar kunt inloggen. Zonder deze functie zou LNReader212
* zodat je daar kunt inloggen. Zonder deze functie zou LNReader195
* site+path plakken (bijv. /r/HFY/series/id), wat geen geldige Reddit-pagina is.213
* site+path plakken (bijv. /r/HFY/series/id), wat geen geldige Reddit-pagina is.196
* Het reeks-id is de oudste post (eerste deel).214
* Het reeks-id is de oudste post (eerste deel), of een wiki-slug.197
*/215
*/198
resolveUrl(path, isNovel) {216
resolveUrl(path, isNovel) {199
const raw = String(path || "").trim();217
const raw = String(path || "").trim();200
if (/^https?:\/\//i.test(raw)) {218
if (/^https?:\/\//i.test(raw)) {201
return raw;219
return raw;202
}220
}221
const wikiSlug = wikiSlugFromPath(raw);222
if (wikiSlug) {223
return REDDIT_WWW + "/r/HFY/wiki/series/" + wikiSlug + "/";224
}203
const id = chapterIdFromPath(raw) || chapterIdFromPath(raw.replace(/^series\//, "chapter/"));225
const id = chapterIdFromPath(raw) || chapterIdFromPath(raw.replace(/^series\//, "chapter/"));204
if (id) {226
if (id) {205
return "https://www.reddit.com/r/HFY/comments/" + id + "/";227
return REDDIT_WWW + "/r/HFY/comments/" + id + "/";206
}228
}207
return this.site;229
return this.site;208
}230
}@@ -229,10 +251,162 @@ class HFYNovelsPlugin {229
async popularNovels(pageNo, options) {251
async popularNovels(pageNo, options) {230
const opts = options || {};252
const opts = options || {};231
const filters = opts.filters || {};253
const filters = opts.filters || {};232
const showLatest = !!opts.showLatestNovels;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
}233
const page = Math.max(1, parseInt(pageNo, 10) || 1);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 een337
// 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
}234
404
235
// Zonder extra filters is de catalogus al op update-datum gesorteerd.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);236
const usePagedCatalog =410
const usePagedCatalog =237
!showLatest &&411
!showLatest &&238
(!filters.kind || filters.kind.value === "all") &&412
(!filters.kind || filters.kind.value === "all") &&@@ -251,16 +425,14 @@ class HFYNovelsPlugin {251
}425
}252
426
253
const search = await readJson(this.indexUrl("search-index.json"), "zoekindex");427
const search = await readJson(this.indexUrl("search-index.json"), "zoekindex");254
let items = (search.novels || []).filter((n) => this.matchesFilters(n, filters));428
const items = (search.novels || []).filter((n) => this.matchesFilters(n, filters));255
const size = (await this.loadMeta()).pageSize || PAGE_FALLBACK_SIZE;429
const size = (await this.loadMeta()).pageSize || PAGE_FALLBACK_SIZE;256
const start = (page - 1) * size;430
const start = (page - 1) * size;257
return items.slice(start, start + size).map((n) => toNovelItem(n, this));431
return items.slice(start, start + size).map((n) => toNovelItem(n, this));258
}432
}259
433
260
async searchNovels(searchTerm, pageNo) {434
async staticSearch(searchTerm, pageNo) {261
const term = String(searchTerm || "")435
const term = String(searchTerm || "").trim().toLowerCase();262
.trim()263
.toLowerCase();264
if (!term) {436
if (!term) {265
return [];437
return [];266
}438
}@@ -271,23 +443,11 @@ class HFYNovelsPlugin {271
const author = String(n.author || "").toLowerCase();443
const author = String(n.author || "").toLowerCase();272
return name.includes(term) || author.includes(term);444
return name.includes(term) || author.includes(term);273
});445
});274
const size = PAGE_FALLBACK_SIZE;446
const start = (page - 1) * PAGE_FALLBACK_SIZE;275
const start = (page - 1) * size;447
return hits.slice(start, start + PAGE_FALLBACK_SIZE).map((n) => toNovelItem(n, this));276
return hits.slice(start, start + size).map((n) => toNovelItem(n, this));277
}278
279
seriesFileFor(novelPath) {280
const id = String(novelPath || "")281
.replace(/^series\//, "")282
.replace(/\/+$/, "");283
if (!id) {284
throw new Error("HFY Novels: leeg reeks-pad");285
}286
const prefix = id.charAt(0).toLowerCase() || "x";287
return "series/" + prefix + "/" + id + ".json";288
}448
}289
449
290
async parseNovel(novelPath) {450
async staticParseNovel(novelPath) {291
const data = await readJson(this.indexUrl(this.seriesFileFor(novelPath)), "reeks " + novelPath);451
const data = await readJson(this.indexUrl(this.seriesFileFor(novelPath)), "reeks " + novelPath);292
return {452
return {293
path: data.path || novelPath,453
path: data.path || novelPath,@@ -307,43 +467,927 @@ class HFYNovelsPlugin {307
};467
};308
}468
}309
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
}310
480
311
async parseChapter(chapterPath) {481
async liveLatestNovels(page, filters, mode) {312
const id = chapterIdFromPath(chapterPath);482
const fetched = await this.loadRecentOcPosts(page, mode);313
if (!id) {483
if (!fetched.ok) {314
throw new Error("HFY Novels: ongeldig hoofdstuk-id");484
return fetched;485
}486
const grouped = groupPostsIntoNovels(fetched.posts, fetched.wikiEntries || []);487
const items = grouped488
.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" };315
}604
}316
const mode = this.chapterMode();317
let redditError = "";318
if (mode !== "arctic") {605
if (mode !== "arctic") {319
const reddit = await tryRedditChapter(id);606
const reddit = await fetchRedditWikiMarkdown(WIKI_INDEX_PATH);320
if (reddit.ok) {607
if (reddit.ok) {321
return renderChapter(reddit.post, "Reddit");608
const entries = parseSeriesIndex(reddit.markdown);609
if (entries.length) {610
writeWikiCache(entries);611
return { ok: true, entries: entries, source: "reddit" };612
}322
}613
}323
redditError = reddit.reason || "onbekende fout";324
if (mode === "reddit") {614
if (mode === "reddit") {325
throw new Error(615
return { ok: false, reason: (reddit && reddit.reason) || "lege wiki-index" };326
"HFY Novels: Reddit gaf geen hoofdstuk (" +327
redditError +328
"). Log in via Open in WebView op reddit.com en probeer het opnieuw.",329
);330
}616
}331
}617
}332
const arctic = await tryArcticChapter(this.chapterSource(), id);618
const arctic = await fetchArcticWikiPage(this.chapterSource(), WIKI_INDEX_PATH);333
if (arctic.ok) {619
if (arctic.ok) {334
const suffix = mode === "arctic" ? "Arctic Shift" : "Arctic Shift (fallback)";620
const entries = parseSeriesIndex(arctic.markdown);335
return renderChapter(arctic.post, suffix);621
if (entries.length) {622
writeWikiCache(entries);623
return { ok: true, entries: entries, source: "arctic" };624
}336
}625
}337
if (redditError) {626
return { ok: false, reason: (arctic && arctic.reason) || "wiki-index niet beschikbaar" };338
throw new Error(627
}339
"HFY Novels: Reddit (" +628
340
redditError +629
async loadWikiPage(slug, mode) {341
") en Arctic Shift (" +630
const path = WIKI_INDEX_PATH + "/" + slug;342
(arctic.reason || "geen tekst") +631
if (mode !== "arctic") {343
") faalden allebei.",632
const reddit = await fetchRedditWikiMarkdown(path);344
);633
if (reddit.ok) {634
return { ok: true, entry: parseSeriesPage(reddit.markdown, path), source: "reddit" };635
}636
if (mode === "reddit") {637
return reddit;638
}345
}639
}346
throw new Error("HFY Novels: kon hoofdstuk " + id + " niet laden (" + (arctic.reason || "geen tekst") + ")");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
747
function 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
763
function 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
776
function 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
787
function 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
805
function 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 optioneel811
}812
}813
814
function 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
844
function 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
877
function 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
894
function cleanHeading(raw) {895
let text = String(raw || "").replace(/\[([^\]]+)\]\([^)]+\)/g, "$1");896
text = text.replace(/[*_`]+/g, "");897
return text.replace(/\s+/g, " ").trim();898
}899
900
function 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
908
function 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
926
function 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
936
function 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
959
function 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
971
function 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
980
function 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
988
function sameAuthor(a, b) {989
return String((a && a.author) || "").toLowerCase() === String((b && b.author) || "").toLowerCase();990
}991
992
function 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
1003
function 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
1025
function 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
1039
function 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
1045
function 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 normalize1051
}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
1066
function 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
1085
function 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
1093
function 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
1118
function 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
1205
function 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
1221
async 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
1240
async 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
1263
async 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
1277
async 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
1290
async 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
1300
async 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
1317
function 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
1329
async 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
1348
async 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
1369
async 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
1380
async 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) };347
}1391
}348
}1392
}349
1393
@@ -358,10 +1402,7 @@ function chapterIdFromPath(chapterPath) {358
1402
359
function redditJsonUrls(id) {1403
function redditJsonUrls(id) {360
const path = "/r/HFY/comments/" + id + "/.json?raw_json=1";1404
const path = "/r/HFY/comments/" + id + "/.json?raw_json=1";361
return [1405
return [REDDIT_WWW + path, REDDIT_OLD + path];362
"https://www.reddit.com" + path,363
"https://old.reddit.com" + path,364
];365
}1406
}366
1407
367
function looksLikeHtml(text) {1408
function looksLikeHtml(text) {@@ -5,8 +5,9 @@
5
5
* Laadt de plugin zoals LNReader dat doet: custom require-allowlist,
6
6
* `new Function`, en daarna popularNovels / searchNovels / parseNovel /
7
7
* parseChapter. De index komt van een lokale HTTP-server (testdata of
8
* public/). parseChapter probeert eerst een mock van Reddit-JSON en
9
* valt terug op Arctic Shift; optioneel een live verzoek als HFY_LIVE=1.
8
* public/). parseChapter, searchNovels, popularNovels en parseNovel
9
* gebruiken gemockte Reddit-JSON, vallen terug op Arctic Shift en daarna
10
* op de statische index; optioneel een live verzoek als HFY_LIVE=1.
10
11
*/
11
12
12
13
const fs = require("fs");
@@ -36,10 +37,13 @@ function startServer(rootDir, chapterHandler, redditHandler) {
36
37
return new Promise((resolve, reject) => {
37
38
const server = http.createServer((req, res) => {
38
39
const url = new URL(req.url, "http://127.0.0.1");
39
if (url.pathname.startsWith("/api/posts/ids")) {
40
if (url.pathname.startsWith("/api/posts/ids") || url.pathname.startsWith("/api/posts/search") || url.pathname.startsWith("/api/subreddits/wikis")) {
40
41
return chapterHandler(url, res);
41
42
}
42
if (redditHandler && /\/r\/HFY\/comments\//i.test(url.pathname) && url.pathname.endsWith(".json")) {
43
if (redditHandler && /\/r\/HFY\//i.test(url.pathname)) {
44
return redditHandler(url, res, req);
45
}
46
if (redditHandler && url.pathname.startsWith("/api/info")) {
43
47
return redditHandler(url, res, req);
44
48
}
45
49
// ?rev=master zoals BlueGit raw-URLs; voor het bestand negeren we de query.
@@ -104,7 +108,12 @@ function blockHtml() {
104
108
function defaultRedditHandler(mode) {
105
109
return function redditHandler(url, res) {
106
110
const m = url.pathname.match(/\/comments\/([0-9a-z]{4,10})\//i);
107
const id = m ? m[1].toLowerCase() : "unknown";
111
if (!m) {
112
res.writeHead(404, { "Content-Type": "text/plain" });
113
res.end("no default reddit mock for " + url.pathname);
114
return;
115
}
116
const id = m[1].toLowerCase();
108
117
if (mode === "ok") {
109
118
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
110
119
res.end(JSON.stringify(redditListingFor(id)));
@@ -126,6 +135,11 @@ function defaultRedditHandler(mode) {
126
135
}
127
136
128
137
function defaultChapterHandler(url, res) {
138
if (!url.pathname.startsWith("/api/posts/ids")) {
139
res.writeHead(404, { "Content-Type": "text/plain" });
140
res.end("no default arctic mock for " + url.pathname);
141
return;
142
}
129
143
const ids = url.searchParams.get("ids") || "";
130
144
const id = ids.split(",")[0];
131
145
const fixture = {
@@ -184,8 +198,11 @@ function loadPlugin(storageMap, fetchApi) {
184
198
get(key) {
185
199
return storageMap[key];
186
200
},
187
set(key, value) {
201
set(key, value, expires) {
188
202
storageMap[key] = value;
203
if (expires != null) {
204
storageMap[key + "__expires"] = expires;
205
}
189
206
},
190
207
},
191
208
},
@@ -216,6 +233,266 @@ function assert(cond, msg) {
216
233
}
217
234
}
218
235
236
function wikiIndexPayload(markdown) {
237
return { kind: "wikipage", data: { content_md: markdown, revision_date: 1700000000 } };
238
}
239
240
function listingOf(posts) {
241
return {
242
kind: "Listing",
243
data: {
244
children: posts.map((p) => ({ kind: "t3", data: p })),
245
after: null,
246
},
247
};
248
}
249
250
function samplePost(id, title, author, extras) {
251
const extra = extras || {};
252
return Object.assign(
253
{
254
id,
255
title,
256
author,
257
created_utc: extra.created_utc || 1700000000,
258
permalink: "/r/HFY/comments/" + id + "/",
259
selftext: extra.selftext || "",
260
selftext_html: extra.selftext_html || "",
261
link_flair_text: extra.flair || "OC-Series",
262
over_18: !!extra.over_18,
263
is_self: true,
264
},
265
extra.more || {},
266
);
267
}
268
269
async function testLiveCatalog(origin, fixtureRoot, fetchApi, assert) {
270
const wikiIndex = fs.readFileSync(path.join(ROOT, "testdata", "wiki", "index.md"), "utf8");
271
const wikiTerran = fs.readFileSync(path.join(ROOT, "testdata", "wiki", "pages", "the_last_terran.md"), "utf8");
272
const state = { redditMode: "ok", arcticMode: "ok", wikiHits: 0, searchHits: 0, arcticSearchHits: 0, arcticWikiHits: 0, infoHits: 0 };
273
274
function redditHandler(url, res) {
275
if (state.redditMode === "403") {
276
res.writeHead(403, { "Content-Type": "text/html; charset=utf-8" });
277
res.end(blockHtml());
278
return;
279
}
280
if (state.redditMode === "error") {
281
res.writeHead(500, { "Content-Type": "text/plain" });
282
res.end("reddit mock error");
283
return;
284
}
285
if (/\/wiki\/series\/the_last_terran/i.test(url.pathname)) {
286
state.wikiHits += 1;
287
res.writeHead(200, { "Content-Type": "application/json" });
288
res.end(JSON.stringify(wikiIndexPayload(wikiTerran)));
289
return;
290
}
291
if (/\/wiki\/series/i.test(url.pathname)) {
292
state.wikiHits += 1;
293
res.writeHead(200, { "Content-Type": "application/json" });
294
res.end(JSON.stringify(wikiIndexPayload(wikiIndex)));
295
return;
296
}
297
if (/\/search\.json$/i.test(url.pathname)) {
298
state.searchHits += 1;
299
const q = (url.searchParams.get("q") || "").toLowerCase();
300
const posts = [];
301
if (q.indexOf("terran") !== -1 || q.indexOf("writerone") !== -1) {
302
posts.push(
303
samplePost("aaa111", "[OC] The Last Terran - Part 1", "WriterOne", {
304
flair: "OC-FirstOfSeries",
305
selftext: "[Next](https://www.reddit.com/r/HFY/comments/aaa222/x/)",
306
}),
307
);
308
}
309
if (q.indexOf("star") !== -1 || q.indexOf("boat") !== -1) {
310
posts.push(
311
samplePost("ddd001", "[OC] Star Boat - Part 1", "Skipper", {
312
flair: "OC-FirstOfSeries",
313
selftext: "[Next](https://www.reddit.com/r/HFY/comments/ddd002/x/)",
314
created_utc: 1700345600,
315
}),
316
);
317
posts.push(
318
samplePost("ddd002", "Star Boat - Part 2", "Skipper", {
319
flair: "OC-Series",
320
selftext: "[Prev](https://www.reddit.com/r/HFY/comments/ddd001/x/)",
321
created_utc: 1700432000,
322
}),
323
);
324
}
325
res.writeHead(200, { "Content-Type": "application/json" });
326
res.end(JSON.stringify(listingOf(posts)));
327
return;
328
}
329
if (/\/new\.json$/i.test(url.pathname)) {
330
state.searchHits += 1;
331
res.writeHead(200, { "Content-Type": "application/json" });
332
res.end(
333
JSON.stringify(
334
listingOf([
335
samplePost("ddd001", "[OC] Star Boat - Part 1", "Skipper", {
336
flair: "OC-FirstOfSeries",
337
created_utc: 1700345600,
338
}),
339
samplePost("meta999", "[META] Weekly thread", "ModAccount", { flair: "Meta", created_utc: 1700345700 }),
340
samplePost("aaa444", "The Last Terran - Part 4", "WriterOne", {
341
flair: "OC-Series",
342
created_utc: 1700259200,
343
}),
344
]),
345
),
346
);
347
return;
348
}
349
if (url.pathname.startsWith("/api/info") || /\/comments\/[0-9a-z]+\//i.test(url.pathname)) {
350
state.infoHits += 1;
351
const ids = [];
352
if (url.searchParams.get("id")) {
353
url.searchParams.get("id").split(",").forEach((raw) => ids.push(raw.replace(/^t3_/, "").toLowerCase()));
354
} else {
355
const m = url.pathname.match(/\/comments\/([0-9a-z]{4,10})\//i);
356
if (m) ids.push(m[1].toLowerCase());
357
}
358
const posts = ids.map((id) => {
359
if (id === "aaa222") {
360
return samplePost(id, "The Last Terran - Part 2", "WriterOne", {
361
selftext: "[Prev](https://www.reddit.com/r/HFY/comments/aaa111/x/) | [Next](https://www.reddit.com/r/HFY/comments/aaa333/x/)",
362
});
363
}
364
if (id === "aaa333") {
365
return samplePost(id, "The Last Terran Chapter 3", "WriterOne", {
366
selftext: "[Previous](/r/HFY/comments/aaa222/)",
367
});
368
}
369
return samplePost(id, "The Last Terran - Part 1", "WriterOne", {
370
flair: "OC-FirstOfSeries",
371
selftext: "[Next](https://www.reddit.com/r/HFY/comments/aaa222/x/)",
372
});
373
});
374
res.writeHead(200, { "Content-Type": "application/json" });
375
if (/\/comments\//i.test(url.pathname)) {
376
res.end(JSON.stringify(redditListingFor(ids[0] || "aaa111")));
377
} else {
378
res.end(JSON.stringify(listingOf(posts)));
379
}
380
return;
381
}
382
res.writeHead(404, { "Content-Type": "text/plain" });
383
res.end("no reddit mock for " + url.pathname);
384
}
385
386
function arcticHandler(url, res) {
387
if (url.pathname.startsWith("/api/subreddits/wikis")) {
388
state.arcticWikiHits += 1;
389
if (state.arcticMode === "fail") {
390
res.writeHead(500, { "Content-Type": "text/plain" });
391
res.end("arctic wiki fail");
392
return;
393
}
394
const paths = url.searchParams.get("paths") || "";
395
const content = /the_last_terran/.test(paths) ? wikiTerran : wikiIndex;
396
res.writeHead(200, { "Content-Type": "application/json" });
397
res.end(JSON.stringify({ data: [{ path: paths, content }] }));
398
return;
399
}
400
if (url.pathname.startsWith("/api/posts/search")) {
401
state.arcticSearchHits += 1;
402
if (state.arcticMode === "fail") {
403
res.writeHead(500, { "Content-Type": "text/plain" });
404
res.end("arctic search fail");
405
return;
406
}
407
const q = ((url.searchParams.get("query") || "") + " " + (url.searchParams.get("title") || "")).toLowerCase();
408
const posts = [];
409
if (!q.trim() || q.indexOf("star") !== -1 || q.indexOf("boat") !== -1) {
410
posts.push(
411
samplePost("ddd001", "[OC] Star Boat - Part 1", "Skipper", {
412
flair: "OC-FirstOfSeries",
413
created_utc: 1700345600,
414
}),
415
);
416
}
417
res.writeHead(200, { "Content-Type": "application/json" });
418
res.end(JSON.stringify({ data: posts }));
419
return;
420
}
421
if (url.pathname.startsWith("/api/posts/ids")) {
422
return defaultChapterHandler(url, res);
423
}
424
res.writeHead(404);
425
res.end("no arctic mock");
426
}
427
428
const started = await startServer(fixtureRoot, arcticHandler, redditHandler);
429
const liveFetch = makeFetchApi(started.url);
430
try {
431
const storageMap = {
432
indexBaseUrl: origin,
433
indexRev: "",
434
chapterSourceUrl: started.url,
435
chapterSource: "auto",
436
};
437
const plugin = loadPlugin(storageMap, liveFetch);
438
439
state.wikiHits = 0;
440
const popular = await plugin.popularNovels(1, { showLatestNovels: false, filters: {} });
441
assert(popular.some((n) => n.name === "The Last Terran"), "live wiki-index levert The Last Terran");
442
assert(popular.some((n) => n.path.indexOf("the_last_terran") !== -1 || n.path === "wiki/the_last_terran"), "wiki-pad zonder first-id");
443
const hitsAfterFirst = state.wikiHits;
444
assert(hitsAfterFirst >= 1, "wiki-index is bij Reddit opgehaald");
445
const popularAgain = await plugin.popularNovels(1, { filters: {} });
446
assert(popularAgain.some((n) => n.name === "The Last Terran"), "cache levert opnieuw de wiki-index");
447
assert(state.wikiHits === hitsAfterFirst, "tweede popularNovels gebruikt de wiki-cache");
448
449
const latest = await plugin.popularNovels(1, { showLatestNovels: true, filters: {} });
450
assert(latest.some((n) => /Star Boat/i.test(n.name)), "Latest/Nieuw toont recente OC-reeks");
451
assert(latest.every((n) => !/Weekly thread/i.test(n.name)), "Meta-posts blijven buiten Nieuw");
452
const viaFilter = await plugin.popularNovels(1, { filters: { catalog: { value: "new" } } });
453
assert(viaFilter.some((n) => /Star Boat/i.test(n.name)), "filter Nieuw toont recente OC-posts");
454
455
const search = await plugin.searchNovels("terran", 1);
456
assert(search.some((n) => /Last Terran/i.test(n.name)), "searchNovels groepeert wiki + Part 1");
457
const star = await plugin.searchNovels("Star Boat", 1);
458
assert(star.length === 1 && /Star Boat/i.test(star[0].name), "losse Part 1+2 worden één novel");
459
assert(star[0].path === "series/ddd001", "nieuwe reeks gebruikt het eerste deel als id");
460
461
const novel = await plugin.parseNovel("wiki/the_last_terran");
462
assert(novel.name === "The Last Terran", "parseNovel gebruikt de wiki-titel");
463
assert(novel.author === "WriterOne", "parseNovel leest de auteur uit de wiki");
464
assert(novel.chapters.length === 3, "parseNovel volgt de wiki-TOC");
465
assert(novel.chapters[0].path === "chapter/aaa111", "eerste hoofdstukpad");
466
assert(novel.chapters[2].path === "chapter/aaa333", "laatste TOC-hoofdstuk");
467
468
delete storageMap.wikiSeriesIndex;
469
state.redditMode = "403";
470
state.arcticMode = "ok";
471
state.arcticWikiHits = 0;
472
const plugin403 = loadPlugin(storageMap, liveFetch);
473
const popular403 = await plugin403.popularNovels(1, { filters: {} });
474
assert(popular403.some((n) => n.name === "The Last Terran"), "403 valt terug op Arctic Shift-wiki");
475
assert(state.arcticWikiHits >= 1, "403 roept Arctic Shift-wiki aan");
476
477
const search403 = await plugin403.searchNovels("Star Boat", 1);
478
assert(search403.some((n) => /Star Boat/i.test(n.name)), "zoeken valt bij 403 terug op Arctic Shift");
479
480
delete storageMap.wikiSeriesIndex;
481
state.redditMode = "403";
482
state.arcticMode = "fail";
483
const pluginStatic = loadPlugin(storageMap, liveFetch);
484
const popularStatic = await pluginStatic.popularNovels(1, { filters: {} });
485
assert(popularStatic.some((n) => n.path === "series/aaa111"), "na Reddit+Arctic Shift volgt de statische index");
486
const searchStatic = await pluginStatic.searchNovels("terran", 1);
487
assert(searchStatic.length === 1 && searchStatic[0].path === "series/aaa111", "zoeken valt terug op de statische index");
488
489
console.log("✔️ live catalogus: wiki, Nieuw, zoeken, parseNovel, 403→Arctic Shift→index");
490
} finally {
491
started.server.close();
492
}
493
}
494
495
219
496
async function main() {
220
497
const fixtureRoot = path.join(ROOT, "testdata", "index-fixture");
221
498
const redditState = { mode: "ok", redditHits: 0, arcticHits: 0 };
@@ -241,13 +518,15 @@ async function main() {
241
518
242
519
assert(plugin.id === "hfyNovels", "id");
243
520
assert(plugin.name === "HFY Novels", "name");
244
assert(plugin.version === "0.2.0", "version");
521
assert(plugin.version === "0.3.0", "version");
245
522
assert(plugin.site === "https://www.reddit.com/r/HFY/", "site");
246
523
assert(plugin.pluginSettings && plugin.pluginSettings.indexBaseUrl, "pluginSettings");
524
assert(plugin.filters && plugin.filters.catalog, "filter Nieuw/wiki");
247
525
assert(plugin.pluginSettings.chapterSource && plugin.pluginSettings.chapterSource.type === "Select", "bron-instelling is Select");
248
526
assert(typeof plugin.resolveUrl === "function", "resolveUrl");
249
527
assert(plugin.resolveUrl("chapter/aaa111") === "https://www.reddit.com/r/HFY/comments/aaa111/", "resolveUrl hoofdstuk");
250
528
assert(plugin.resolveUrl("series/aaa111", true) === "https://www.reddit.com/r/HFY/comments/aaa111/", "resolveUrl reeks");
529
assert(plugin.resolveUrl("wiki/the_last_terran", true) === "https://www.reddit.com/r/HFY/wiki/series/the_last_terran/", "resolveUrl wiki");
251
530
252
531
const popular = await plugin.popularNovels(1, { showLatestNovels: false, filters: {} });
253
532
assert(Array.isArray(popular) && popular.length >= 1, "popularNovels geeft items");
@@ -357,6 +636,9 @@ async function main() {
357
636
}
358
637
assert(threw, "ongeldig hoofdstuk-id wordt geweigerd");
359
638
639
// --- Live catalogus: wiki, zoeken, parseNovel, met fallbacks ---
640
await testLiveCatalog(url, fixtureRoot, fetchApi, assert);
641
360
642
// Manifest-vorm controleren.
361
643
const manifest = JSON.parse(fs.readFileSync(path.join(ROOT, ".dist", "plugins.json"), "utf8"));
362
644
const manifestMin = JSON.parse(fs.readFileSync(path.join(ROOT, ".dist", "plugins.min.json"), "utf8"));
@@ -5,8 +5,9 @@5
* Laadt de plugin zoals LNReader dat doet: custom require-allowlist,5
* Laadt de plugin zoals LNReader dat doet: custom require-allowlist,6
* `new Function`, en daarna popularNovels / searchNovels / parseNovel /6
* `new Function`, en daarna popularNovels / searchNovels / parseNovel /7
* parseChapter. De index komt van een lokale HTTP-server (testdata of7
* parseChapter. De index komt van een lokale HTTP-server (testdata of8
* public/). parseChapter probeert eerst een mock van Reddit-JSON en8
* public/). parseChapter, searchNovels, popularNovels en parseNovel9
* valt terug op Arctic Shift; optioneel een live verzoek als HFY_LIVE=1.9
* gebruiken gemockte Reddit-JSON, vallen terug op Arctic Shift en daarna10
* op de statische index; optioneel een live verzoek als HFY_LIVE=1.10
*/11
*/11
12
12
const fs = require("fs");13
const fs = require("fs");@@ -36,10 +37,13 @@ function startServer(rootDir, chapterHandler, redditHandler) {36
return new Promise((resolve, reject) => {37
return new Promise((resolve, reject) => {37
const server = http.createServer((req, res) => {38
const server = http.createServer((req, res) => {38
const url = new URL(req.url, "http://127.0.0.1");39
const url = new URL(req.url, "http://127.0.0.1");39
if (url.pathname.startsWith("/api/posts/ids")) {40
if (url.pathname.startsWith("/api/posts/ids") || url.pathname.startsWith("/api/posts/search") || url.pathname.startsWith("/api/subreddits/wikis")) {40
return chapterHandler(url, res);41
return chapterHandler(url, res);41
}42
}42
if (redditHandler && /\/r\/HFY\/comments\//i.test(url.pathname) && url.pathname.endsWith(".json")) {43
if (redditHandler && /\/r\/HFY\//i.test(url.pathname)) {44
return redditHandler(url, res, req);45
}46
if (redditHandler && url.pathname.startsWith("/api/info")) {43
return redditHandler(url, res, req);47
return redditHandler(url, res, req);44
}48
}45
// ?rev=master zoals BlueGit raw-URLs; voor het bestand negeren we de query.49
// ?rev=master zoals BlueGit raw-URLs; voor het bestand negeren we de query.@@ -104,7 +108,12 @@ function blockHtml() {104
function defaultRedditHandler(mode) {108
function defaultRedditHandler(mode) {105
return function redditHandler(url, res) {109
return function redditHandler(url, res) {106
const m = url.pathname.match(/\/comments\/([0-9a-z]{4,10})\//i);110
const m = url.pathname.match(/\/comments\/([0-9a-z]{4,10})\//i);107
const id = m ? m[1].toLowerCase() : "unknown";111
if (!m) {112
res.writeHead(404, { "Content-Type": "text/plain" });113
res.end("no default reddit mock for " + url.pathname);114
return;115
}116
const id = m[1].toLowerCase();108
if (mode === "ok") {117
if (mode === "ok") {109
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });118
res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });110
res.end(JSON.stringify(redditListingFor(id)));119
res.end(JSON.stringify(redditListingFor(id)));@@ -126,6 +135,11 @@ function defaultRedditHandler(mode) {126
}135
}127
136
128
function defaultChapterHandler(url, res) {137
function defaultChapterHandler(url, res) {138
if (!url.pathname.startsWith("/api/posts/ids")) {139
res.writeHead(404, { "Content-Type": "text/plain" });140
res.end("no default arctic mock for " + url.pathname);141
return;142
}129
const ids = url.searchParams.get("ids") || "";143
const ids = url.searchParams.get("ids") || "";130
const id = ids.split(",")[0];144
const id = ids.split(",")[0];131
const fixture = {145
const fixture = {@@ -184,8 +198,11 @@ function loadPlugin(storageMap, fetchApi) {184
get(key) {198
get(key) {185
return storageMap[key];199
return storageMap[key];186
},200
},187
set(key, value) {201
set(key, value, expires) {188
storageMap[key] = value;202
storageMap[key] = value;203
if (expires != null) {204
storageMap[key + "__expires"] = expires;205
}189
},206
},190
},207
},191
},208
},@@ -216,6 +233,266 @@ function assert(cond, msg) {216
}233
}217
}234
}218
235
236
function wikiIndexPayload(markdown) {237
return { kind: "wikipage", data: { content_md: markdown, revision_date: 1700000000 } };238
}239
240
function listingOf(posts) {241
return {242
kind: "Listing",243
data: {244
children: posts.map((p) => ({ kind: "t3", data: p })),245
after: null,246
},247
};248
}249
250
function samplePost(id, title, author, extras) {251
const extra = extras || {};252
return Object.assign(253
{254
id,255
title,256
author,257
created_utc: extra.created_utc || 1700000000,258
permalink: "/r/HFY/comments/" + id + "/",259
selftext: extra.selftext || "",260
selftext_html: extra.selftext_html || "",261
link_flair_text: extra.flair || "OC-Series",262
over_18: !!extra.over_18,263
is_self: true,264
},265
extra.more || {},266
);267
}268
269
async function testLiveCatalog(origin, fixtureRoot, fetchApi, assert) {270
const wikiIndex = fs.readFileSync(path.join(ROOT, "testdata", "wiki", "index.md"), "utf8");271
const wikiTerran = fs.readFileSync(path.join(ROOT, "testdata", "wiki", "pages", "the_last_terran.md"), "utf8");272
const state = { redditMode: "ok", arcticMode: "ok", wikiHits: 0, searchHits: 0, arcticSearchHits: 0, arcticWikiHits: 0, infoHits: 0 };273
274
function redditHandler(url, res) {275
if (state.redditMode === "403") {276
res.writeHead(403, { "Content-Type": "text/html; charset=utf-8" });277
res.end(blockHtml());278
return;279
}280
if (state.redditMode === "error") {281
res.writeHead(500, { "Content-Type": "text/plain" });282
res.end("reddit mock error");283
return;284
}285
if (/\/wiki\/series\/the_last_terran/i.test(url.pathname)) {286
state.wikiHits += 1;287
res.writeHead(200, { "Content-Type": "application/json" });288
res.end(JSON.stringify(wikiIndexPayload(wikiTerran)));289
return;290
}291
if (/\/wiki\/series/i.test(url.pathname)) {292
state.wikiHits += 1;293
res.writeHead(200, { "Content-Type": "application/json" });294
res.end(JSON.stringify(wikiIndexPayload(wikiIndex)));295
return;296
}297
if (/\/search\.json$/i.test(url.pathname)) {298
state.searchHits += 1;299
const q = (url.searchParams.get("q") || "").toLowerCase();300
const posts = [];301
if (q.indexOf("terran") !== -1 || q.indexOf("writerone") !== -1) {302
posts.push(303
samplePost("aaa111", "[OC] The Last Terran - Part 1", "WriterOne", {304
flair: "OC-FirstOfSeries",305
selftext: "[Next](https://www.reddit.com/r/HFY/comments/aaa222/x/)",306
}),307
);308
}309
if (q.indexOf("star") !== -1 || q.indexOf("boat") !== -1) {310
posts.push(311
samplePost("ddd001", "[OC] Star Boat - Part 1", "Skipper", {312
flair: "OC-FirstOfSeries",313
selftext: "[Next](https://www.reddit.com/r/HFY/comments/ddd002/x/)",314
created_utc: 1700345600,315
}),316
);317
posts.push(318
samplePost("ddd002", "Star Boat - Part 2", "Skipper", {319
flair: "OC-Series",320
selftext: "[Prev](https://www.reddit.com/r/HFY/comments/ddd001/x/)",321
created_utc: 1700432000,322
}),323
);324
}325
res.writeHead(200, { "Content-Type": "application/json" });326
res.end(JSON.stringify(listingOf(posts)));327
return;328
}329
if (/\/new\.json$/i.test(url.pathname)) {330
state.searchHits += 1;331
res.writeHead(200, { "Content-Type": "application/json" });332
res.end(333
JSON.stringify(334
listingOf([335
samplePost("ddd001", "[OC] Star Boat - Part 1", "Skipper", {336
flair: "OC-FirstOfSeries",337
created_utc: 1700345600,338
}),339
samplePost("meta999", "[META] Weekly thread", "ModAccount", { flair: "Meta", created_utc: 1700345700 }),340
samplePost("aaa444", "The Last Terran - Part 4", "WriterOne", {341
flair: "OC-Series",342
created_utc: 1700259200,343
}),344
]),345
),346
);347
return;348
}349
if (url.pathname.startsWith("/api/info") || /\/comments\/[0-9a-z]+\//i.test(url.pathname)) {350
state.infoHits += 1;351
const ids = [];352
if (url.searchParams.get("id")) {353
url.searchParams.get("id").split(",").forEach((raw) => ids.push(raw.replace(/^t3_/, "").toLowerCase()));354
} else {355
const m = url.pathname.match(/\/comments\/([0-9a-z]{4,10})\//i);356
if (m) ids.push(m[1].toLowerCase());357
}358
const posts = ids.map((id) => {359
if (id === "aaa222") {360
return samplePost(id, "The Last Terran - Part 2", "WriterOne", {361
selftext: "[Prev](https://www.reddit.com/r/HFY/comments/aaa111/x/) | [Next](https://www.reddit.com/r/HFY/comments/aaa333/x/)",362
});363
}364
if (id === "aaa333") {365
return samplePost(id, "The Last Terran Chapter 3", "WriterOne", {366
selftext: "[Previous](/r/HFY/comments/aaa222/)",367
});368
}369
return samplePost(id, "The Last Terran - Part 1", "WriterOne", {370
flair: "OC-FirstOfSeries",371
selftext: "[Next](https://www.reddit.com/r/HFY/comments/aaa222/x/)",372
});373
});374
res.writeHead(200, { "Content-Type": "application/json" });375
if (/\/comments\//i.test(url.pathname)) {376
res.end(JSON.stringify(redditListingFor(ids[0] || "aaa111")));377
} else {378
res.end(JSON.stringify(listingOf(posts)));379
}380
return;381
}382
res.writeHead(404, { "Content-Type": "text/plain" });383
res.end("no reddit mock for " + url.pathname);384
}385
386
function arcticHandler(url, res) {387
if (url.pathname.startsWith("/api/subreddits/wikis")) {388
state.arcticWikiHits += 1;389
if (state.arcticMode === "fail") {390
res.writeHead(500, { "Content-Type": "text/plain" });391
res.end("arctic wiki fail");392
return;393
}394
const paths = url.searchParams.get("paths") || "";395
const content = /the_last_terran/.test(paths) ? wikiTerran : wikiIndex;396
res.writeHead(200, { "Content-Type": "application/json" });397
res.end(JSON.stringify({ data: [{ path: paths, content }] }));398
return;399
}400
if (url.pathname.startsWith("/api/posts/search")) {401
state.arcticSearchHits += 1;402
if (state.arcticMode === "fail") {403
res.writeHead(500, { "Content-Type": "text/plain" });404
res.end("arctic search fail");405
return;406
}407
const q = ((url.searchParams.get("query") || "") + " " + (url.searchParams.get("title") || "")).toLowerCase();408
const posts = [];409
if (!q.trim() || q.indexOf("star") !== -1 || q.indexOf("boat") !== -1) {410
posts.push(411
samplePost("ddd001", "[OC] Star Boat - Part 1", "Skipper", {412
flair: "OC-FirstOfSeries",413
created_utc: 1700345600,414
}),415
);416
}417
res.writeHead(200, { "Content-Type": "application/json" });418
res.end(JSON.stringify({ data: posts }));419
return;420
}421
if (url.pathname.startsWith("/api/posts/ids")) {422
return defaultChapterHandler(url, res);423
}424
res.writeHead(404);425
res.end("no arctic mock");426
}427
428
const started = await startServer(fixtureRoot, arcticHandler, redditHandler);429
const liveFetch = makeFetchApi(started.url);430
try {431
const storageMap = {432
indexBaseUrl: origin,433
indexRev: "",434
chapterSourceUrl: started.url,435
chapterSource: "auto",436
};437
const plugin = loadPlugin(storageMap, liveFetch);438
439
state.wikiHits = 0;440
const popular = await plugin.popularNovels(1, { showLatestNovels: false, filters: {} });441
assert(popular.some((n) => n.name === "The Last Terran"), "live wiki-index levert The Last Terran");442
assert(popular.some((n) => n.path.indexOf("the_last_terran") !== -1 || n.path === "wiki/the_last_terran"), "wiki-pad zonder first-id");443
const hitsAfterFirst = state.wikiHits;444
assert(hitsAfterFirst >= 1, "wiki-index is bij Reddit opgehaald");445
const popularAgain = await plugin.popularNovels(1, { filters: {} });446
assert(popularAgain.some((n) => n.name === "The Last Terran"), "cache levert opnieuw de wiki-index");447
assert(state.wikiHits === hitsAfterFirst, "tweede popularNovels gebruikt de wiki-cache");448
449
const latest = await plugin.popularNovels(1, { showLatestNovels: true, filters: {} });450
assert(latest.some((n) => /Star Boat/i.test(n.name)), "Latest/Nieuw toont recente OC-reeks");451
assert(latest.every((n) => !/Weekly thread/i.test(n.name)), "Meta-posts blijven buiten Nieuw");452
const viaFilter = await plugin.popularNovels(1, { filters: { catalog: { value: "new" } } });453
assert(viaFilter.some((n) => /Star Boat/i.test(n.name)), "filter Nieuw toont recente OC-posts");454
455
const search = await plugin.searchNovels("terran", 1);456
assert(search.some((n) => /Last Terran/i.test(n.name)), "searchNovels groepeert wiki + Part 1");457
const star = await plugin.searchNovels("Star Boat", 1);458
assert(star.length === 1 && /Star Boat/i.test(star[0].name), "losse Part 1+2 worden één novel");459
assert(star[0].path === "series/ddd001", "nieuwe reeks gebruikt het eerste deel als id");460
461
const novel = await plugin.parseNovel("wiki/the_last_terran");462
assert(novel.name === "The Last Terran", "parseNovel gebruikt de wiki-titel");463
assert(novel.author === "WriterOne", "parseNovel leest de auteur uit de wiki");464
assert(novel.chapters.length === 3, "parseNovel volgt de wiki-TOC");465
assert(novel.chapters[0].path === "chapter/aaa111", "eerste hoofdstukpad");466
assert(novel.chapters[2].path === "chapter/aaa333", "laatste TOC-hoofdstuk");467
468
delete storageMap.wikiSeriesIndex;469
state.redditMode = "403";470
state.arcticMode = "ok";471
state.arcticWikiHits = 0;472
const plugin403 = loadPlugin(storageMap, liveFetch);473
const popular403 = await plugin403.popularNovels(1, { filters: {} });474
assert(popular403.some((n) => n.name === "The Last Terran"), "403 valt terug op Arctic Shift-wiki");475
assert(state.arcticWikiHits >= 1, "403 roept Arctic Shift-wiki aan");476
477
const search403 = await plugin403.searchNovels("Star Boat", 1);478
assert(search403.some((n) => /Star Boat/i.test(n.name)), "zoeken valt bij 403 terug op Arctic Shift");479
480
delete storageMap.wikiSeriesIndex;481
state.redditMode = "403";482
state.arcticMode = "fail";483
const pluginStatic = loadPlugin(storageMap, liveFetch);484
const popularStatic = await pluginStatic.popularNovels(1, { filters: {} });485
assert(popularStatic.some((n) => n.path === "series/aaa111"), "na Reddit+Arctic Shift volgt de statische index");486
const searchStatic = await pluginStatic.searchNovels("terran", 1);487
assert(searchStatic.length === 1 && searchStatic[0].path === "series/aaa111", "zoeken valt terug op de statische index");488
489
console.log("✔️ live catalogus: wiki, Nieuw, zoeken, parseNovel, 403→Arctic Shift→index");490
} finally {491
started.server.close();492
}493
}494
495
219
async function main() {496
async function main() {220
const fixtureRoot = path.join(ROOT, "testdata", "index-fixture");497
const fixtureRoot = path.join(ROOT, "testdata", "index-fixture");221
const redditState = { mode: "ok", redditHits: 0, arcticHits: 0 };498
const redditState = { mode: "ok", redditHits: 0, arcticHits: 0 };@@ -241,13 +518,15 @@ async function main() {241
518
242
assert(plugin.id === "hfyNovels", "id");519
assert(plugin.id === "hfyNovels", "id");243
assert(plugin.name === "HFY Novels", "name");520
assert(plugin.name === "HFY Novels", "name");244
assert(plugin.version === "0.2.0", "version");521
assert(plugin.version === "0.3.0", "version");245
assert(plugin.site === "https://www.reddit.com/r/HFY/", "site");522
assert(plugin.site === "https://www.reddit.com/r/HFY/", "site");246
assert(plugin.pluginSettings && plugin.pluginSettings.indexBaseUrl, "pluginSettings");523
assert(plugin.pluginSettings && plugin.pluginSettings.indexBaseUrl, "pluginSettings");524
assert(plugin.filters && plugin.filters.catalog, "filter Nieuw/wiki");247
assert(plugin.pluginSettings.chapterSource && plugin.pluginSettings.chapterSource.type === "Select", "bron-instelling is Select");525
assert(plugin.pluginSettings.chapterSource && plugin.pluginSettings.chapterSource.type === "Select", "bron-instelling is Select");248
assert(typeof plugin.resolveUrl === "function", "resolveUrl");526
assert(typeof plugin.resolveUrl === "function", "resolveUrl");249
assert(plugin.resolveUrl("chapter/aaa111") === "https://www.reddit.com/r/HFY/comments/aaa111/", "resolveUrl hoofdstuk");527
assert(plugin.resolveUrl("chapter/aaa111") === "https://www.reddit.com/r/HFY/comments/aaa111/", "resolveUrl hoofdstuk");250
assert(plugin.resolveUrl("series/aaa111", true) === "https://www.reddit.com/r/HFY/comments/aaa111/", "resolveUrl reeks");528
assert(plugin.resolveUrl("series/aaa111", true) === "https://www.reddit.com/r/HFY/comments/aaa111/", "resolveUrl reeks");529
assert(plugin.resolveUrl("wiki/the_last_terran", true) === "https://www.reddit.com/r/HFY/wiki/series/the_last_terran/", "resolveUrl wiki");251
530
252
const popular = await plugin.popularNovels(1, { showLatestNovels: false, filters: {} });531
const popular = await plugin.popularNovels(1, { showLatestNovels: false, filters: {} });253
assert(Array.isArray(popular) && popular.length >= 1, "popularNovels geeft items");532
assert(Array.isArray(popular) && popular.length >= 1, "popularNovels geeft items");@@ -357,6 +636,9 @@ async function main() {357
}636
}358
assert(threw, "ongeldig hoofdstuk-id wordt geweigerd");637
assert(threw, "ongeldig hoofdstuk-id wordt geweigerd");359
638
639
// --- Live catalogus: wiki, zoeken, parseNovel, met fallbacks ---640
await testLiveCatalog(url, fixtureRoot, fetchApi, assert);641
360
// Manifest-vorm controleren.642
// Manifest-vorm controleren.361
const manifest = JSON.parse(fs.readFileSync(path.join(ROOT, ".dist", "plugins.json"), "utf8"));643
const manifest = JSON.parse(fs.readFileSync(path.join(ROOT, ".dist", "plugins.json"), "utf8"));362
const manifestMin = JSON.parse(fs.readFileSync(path.join(ROOT, ".dist", "plugins.min.json"), "utf8"));644
const manifestMin = JSON.parse(fs.readFileSync(path.join(ROOT, ".dist", "plugins.min.json"), "utf8"));