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