From bb3bb110bdc79a27456d2e4a2914a67b13e82c4a Mon Sep 17 00:00:00 2001 From: Valentin Raillard Date: Sun, 29 Sep 2024 16:44:33 +0200 Subject: [PATCH] fix(AsuraScans): remake the presence to match asura website (#8762) --- websites/A/Asura Scans/metadata.json | 59 +++++++++- websites/A/Asura Scans/presence.ts | 165 ++++++++++++++++++++------- 2 files changed, 177 insertions(+), 47 deletions(-) diff --git a/websites/A/Asura Scans/metadata.json b/websites/A/Asura Scans/metadata.json index c9fcc79b28ab..4bbf62872f5a 100644 --- a/websites/A/Asura Scans/metadata.json +++ b/websites/A/Asura Scans/metadata.json @@ -5,10 +5,17 @@ "name": "theusaf", "id": "193714715631812608" }, + "contributors": [ + { + "name": "rois2coeurs", + "id": "234647775621414912" + } + ], "service": "Asura Scans", "description": { "en": "Read Comics on Asura Scans.", - "vi_VN": "Đọc truyện tranh tại Asura Scans." + "vi_VN": "Đọc truyện tranh tại Asura Scans.", + "fr": "Lisez des bandes dessinées sur Asura Scans." }, "url": [ "asurascans.com", @@ -17,10 +24,11 @@ "asurascanstr.com", "asura.nacm.xyz", "asuracomics.com", - "asuratoon.com" + "asuratoon.com", + "asuracomic.net" ], "regExp": "asura.*?[.].*?[/]", - "version": "1.2.11", + "version": "2.0.0", "logo": "https://cdn.rcd.gg/PreMiD/websites/A/Asura%20Scans/assets/logo.png", "thumbnail": "https://cdn.rcd.gg/PreMiD/websites/A/Asura%20Scans/assets/thumbnail.png", "color": "#6E3CAA", @@ -31,5 +39,50 @@ "manga", "manhua", "manhwa" + ], + "settings": [ + { + "id": "privacy", + "title": "Privacy Mode", + "icon": "fa-solid fa-user-secret", + "value": false + }, + { + "id": "chapterNumber", + "title": "Display Current Chapter", + "icon": "fa-solid fa-book", + "value": true, + "if": { + "privacy": false + } + }, + { + "id": "readingPercentage", + "title": "Display Reading %", + "icon": "fa-solid fa-percent", + "value": true, + "if": { + "privacy": false, + "chapterNumber": true + } + }, + { + "id": "showCover", + "title": "Show Cover", + "icon": "fa-solid fa-images", + "value": true, + "if": { + "privacy": false + } + }, + { + "id": "showButtons", + "title": "Show Buttons", + "icon": "fa-solid fa-compress-arrows-alt", + "value": true, + "if": { + "privacy": false + } + } ] } \ No newline at end of file diff --git a/websites/A/Asura Scans/presence.ts b/websites/A/Asura Scans/presence.ts index 9a058eca12a2..3c62c2d2ee04 100644 --- a/websites/A/Asura Scans/presence.ts +++ b/websites/A/Asura Scans/presence.ts @@ -1,54 +1,95 @@ -const presence = new Presence({ - clientId: "864304063804997702", - }), - browsingTimestamp = Math.floor(Date.now() / 1000); +const presence = new Presence({ clientId: "864304063804997702" }), + browsingTimestamp = Math.floor(Date.now() / 1000), + ASURA_SCANS_LOGO = + "https://cdn.rcd.gg/PreMiD/websites/A/Asura%20Scans/assets/logo.png", + CHAPTER_PROGRESS_SELECTOR = + "body > div:nth-child(4) > div > div > div > div.py-8.-mx-5.md\\:mx-0.flex.flex-col.items-center.justify-center"; -presence.on("UpdateData", () => { +class Comic { + title: string; + url: string; + image: string; +} + +const comic = new Comic(); + +presence.on("UpdateData", async () => { const { pathname, href } = window.location, presenceData: PresenceData = { startTimestamp: browsingTimestamp, - largeImageKey: - "https://cdn.rcd.gg/PreMiD/websites/A/Asura%20Scans/assets/logo.png", - }; + largeImageKey: ASURA_SCANS_LOGO, + type: ActivityType.Watching, + }, + [ + displayPercentage, + privacyMode, + displayChapter, + displayCover, + displayButtons, + ] = await Promise.all([ + presence.getSetting("readingPercentage"), + presence.getSetting("privacy"), + presence.getSetting("chapterNumber"), + presence.getSetting("showCover"), + presence.getSetting("showButtons"), + ]); - if (/^\/(page\/\d+\/?)?$/.test(pathname)) - presenceData.details = "Viewing Home Page"; - else if (/^\/manga\/?$/.test(pathname)) - presenceData.details = "Viewing Comic List"; - else if (/^\/manga\/[0-9a-z-]+\/?$/i.test(pathname)) { - presenceData.details = "Viewing Comic Page"; - presenceData.state = - document.querySelector(".entry-title").textContent; - presenceData.buttons = [ - { - label: "Visit Comic Page", - url: href, - }, - ]; - } else if ( - /\/[a-z-\d]+(chapter|ch|bolum)[a-z-\d]*-[0-9]+\/?$/i.test(pathname) - ) { - const progress = - (document.documentElement.scrollTop / - (document.querySelector("#readerarea").scrollHeight - - window.innerHeight)) * - 100; - presenceData.details = "Reading Comic"; - presenceData.state = `${ - document.querySelector(".entry-title").textContent - } - ${(progress > 100 ? 100 : progress).toFixed(1)}%`; - presenceData.buttons = [ - { - label: "Visit Comic Page", - url: document.querySelector(".allc > a").href, - }, - { - label: "Visit Chapter", - url: href, - }, - ]; + if (privacyMode) { + presenceData.details = "Browsing Asura Scans"; + presence.setActivity(presenceData); + return; + } + + if (onComicOrChapterPage(pathname) && isNewComic(href, comic)) { + comic.url = href.split("/chapter")[0]; + comic.title = document.title + .split("Chapter")[0] + .trim() + .split(" - ")[0] + .trim(); + if (displayCover) comic.image = await getComicImage(comic.url); + else comic.image = ASURA_SCANS_LOGO; + } + + if (onChapterPage(pathname)) { + presenceData.details = comic.title; + presenceData.largeImageKey = comic.image; + if (displayButtons) { + presenceData.buttons = [ + { + label: "Visit Comic Page", + url: comic.url, + }, + ]; + } + if (displayChapter) { + presenceData.state = `Chapter ${getChapterNumber()} ${ + displayPercentage ? `- ${getChapterProgress()}%` : "" + }`; + if (displayButtons) { + presenceData.buttons.push({ + label: "Visit Chapter", + url: href, + }); + } + } + } else if (onComicHomePage(pathname)) { + presenceData.details = "Viewing Comic Home Page"; + presenceData.largeImageKey = comic.image; + presenceData.state = comic.title; + if (displayButtons) { + presenceData.buttons = [ + { + label: "Visit Comic Page", + url: comic.url, + }, + ]; + } } else if (pathname.startsWith("/bookmark")) presenceData.details = "Viewing Bookmarks"; + else if (pathname.startsWith("/series")) + presenceData.details = "Viewing Comic List"; + else if (pathname === "/") presenceData.details = "Viewing Home Page"; else { presenceData.details = "Browsing Asura Scans"; presenceData.state = document.title; @@ -57,3 +98,39 @@ presence.on("UpdateData", () => { if (presenceData.details) presence.setActivity(presenceData); else presence.setActivity(); }); + +function onComicOrChapterPage(path: string) { + return /\/series\/[a-z-\d]+.*$/i.test(path); +} + +function onComicHomePage(path: string) { + return /\/series\/[a-z-\d]+$/i.test(path); +} + +function onChapterPage(path: string) { + return /\/series\/[a-z-\d]+\/chapter\/[0-9]+$/i.test(path); +} + +function isNewComic(path: string, comic: Comic) { + return comic.url !== path.split("/chapter")[0]; +} + +function getChapterNumber() { + return document.title.split("Chapter")[1].split("-")[0].trim(); +} + +function getChapterProgress() { + const progress = + (document.documentElement.scrollTop / + (document.querySelector(CHAPTER_PROGRESS_SELECTOR).scrollHeight - + window.innerHeight)) * + 100; + return progress > 100 ? 100 : progress.toFixed(1); +} + +async function getComicImage(comicHomePageURL: string): Promise { + const res = await (await fetch(comicHomePageURL)).text(); + return new DOMParser() + .parseFromString(res, "text/html") + .querySelector("head > meta[property='og:image']").content; +}