Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AsuraScans): remake the presence to match asura website #8762

Merged
merged 4 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions websites/A/Asura Scans/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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
}
}
]
}
165 changes: 121 additions & 44 deletions websites/A/Asura Scans/presence.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>("readingPercentage"),
presence.getSetting<boolean>("privacy"),
presence.getSetting<boolean>("chapterNumber"),
presence.getSetting<boolean>("showCover"),
presence.getSetting<boolean>("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<HTMLHeadingElement>(".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<HTMLDivElement>("#readerarea").scrollHeight -
window.innerHeight)) *
100;
presenceData.details = "Reading Comic";
presenceData.state = `${
document.querySelector<HTMLHeadingElement>(".entry-title").textContent
} - ${(progress > 100 ? 100 : progress).toFixed(1)}%`;
presenceData.buttons = [
{
label: "Visit Comic Page",
url: document.querySelector<HTMLAnchorElement>(".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;
Expand All @@ -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<string> {
const res = await (await fetch(comicHomePageURL)).text();
return new DOMParser()
.parseFromString(res, "text/html")
.querySelector<HTMLMetaElement>("head > meta[property='og:image']").content;
}
Loading