From 879a9628723ea5b386fa5b5a41a7a200bc95f919 Mon Sep 17 00:00:00 2001 From: aka paul <50n50@noreply.localhost> Date: Wed, 4 Feb 2026 18:50:33 +0000 Subject: [PATCH] Update animelib/animelib.js --- animelib/animelib.js | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/animelib/animelib.js b/animelib/animelib.js index 7553286..55479ac 100644 --- a/animelib/animelib.js +++ b/animelib/animelib.js @@ -1,21 +1,32 @@ async function searchResults(keyword) { const results = []; try { - const response = await fetchv2("https://hapi.hentaicdn.org/api/anime?fields[]=rate_avg&fields[]=rate&fields[]=releaseDate&q=" + keyword); + const response = await fetchv2( + "https://hapi.hentaicdn.org/api/anime?fields[]=rate_avg&fields[]=rate&fields[]=releaseDate&q=" + keyword + ); const json = await response.json(); if (json && Array.isArray(json.data)) { for (const item of json.data) { + const title = item.eng_name || item.name || "Unknown"; const image = item.cover?.default || item.cover?.thumbnail || ""; + results.push({ - title: item.eng_name || item.name || "Unknown", - image: "https://passthrough-worker.simplepostrequest.workers.dev/?simple=" + image + "&referer=https://animelib.org/", - href: item.slug_url || item.slug || item.id + title, + image: "https://passthrough-worker.simplepostrequest.workers.dev/?simple=" + + image + + "&referer=https://animelib.org/", + href: item.slug_url || item.slug || item.id, + _score: scoreTitle(title, keyword) }); } } - return JSON.stringify(results); + results.sort((a, b) => a._score - b._score); + + return JSON.stringify( + results.map(({ _score, ...rest }) => rest) + ); } catch (err) { return JSON.stringify([{ title: err.message, @@ -25,6 +36,16 @@ async function searchResults(keyword) { } } +function scoreTitle(title, keyword) { + const t = title.toLowerCase(); + const k = keyword.toLowerCase(); + + if (t === k) return 0; + if (t.startsWith(k)) return 1; + if (t.includes(k)) return 2; + return 3; +} + async function extractDetails(slug) { try { const response = await fetchv2(