123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- const presence = new Presence({
- clientId: "691080074006495303",
- }),
- browsingTimestamp = Math.floor(Date.now() / 1000);
- const enum Assets {
- Logo = "https://cdn.rcd.gg/PreMiD/websites/N/Nuxt.js/assets/0.png",
-
- }
- async function getStrings() {
- return presence.getStrings(
- {
- browse: "general.browsing",
- search: "general.searchFor",
- viewHome: "general.viewHome",
- buttonReadArticle: "general.buttonReadArticle",
- buttonViewPage: "general.buttonViewPage",
- buttonViewProfile: "general.buttonViewProfile",
- },
- await presence.getSetting<string>("lang").catch(() => "en")
- );
- }
- function capitalizeFirstLetter(string: string) {
- if (!string) return "Undefined";
- return (
- string.trim().charAt(0).toUpperCase() + string.trim().slice(1).toLowerCase()
- );
- }
- function imgPath(path: string, hostname: string) {
- if (!path) return Assets.Logo;
- if (path.includes(hostname)) return `https://${path.replace("//", "")}`;
- else return `https://${hostname}${path}`;
- }
- let strings: Awaited<ReturnType<typeof getStrings>>,
- oldLang: string = null;
- presence.on("UpdateData", async () => {
- let presenceData: PresenceData = {
- startTimestamp: browsingTimestamp,
- largeImageKey: Assets.Logo,
- };
- const [newLang, privacy, buttons, covers] = await Promise.all([
- presence.getSetting<string>("lang").catch(() => "en"),
- presence.getSetting<boolean>("privacy"),
- presence.getSetting<boolean>("buttons"),
- presence.getSetting<boolean>("covers"),
- ]),
- search = document.querySelector<HTMLInputElement>(
- 'input[id="docsearch-input"]'
- ),
- ogTitle = document
- .querySelector('meta[property="og:title"]')
- ?.getAttribute("content")
- ?.toLowerCase(),
- docTitle = document.querySelector('[class="d-heading-title"]'),
- subActive = document.querySelector('[class*="-text-active"]'),
- docusContent = !document.querySelector('[class*="docus-content"]'),
- { pathname, hostname, href } = document.location;
- if (oldLang !== newLang || !strings) {
- oldLang = newLang;
- strings = await getStrings();
- }
- if (privacy) {
- presenceData.details = strings.browse;
- presence.setActivity(presenceData);
- return;
- }
- if (search?.value) {
- presenceData.details = strings.search;
- presenceData.state = search.value;
- presenceData.smallImageKey = Assets.Search;
- presence.setActivity(presenceData);
- return;
- }
- switch (pathname.split("/")[1].replace(/-/gm, "")) {
- case "casestudies": {
- if (!docusContent) {
- presenceData.smallImageKey = Assets.Reading;
- presenceData.details = "Reading a case studie about";
- presenceData.state = ogTitle;
- presenceData.buttons = [
- {
- label: "Read Case Studie",
- url: href,
- },
- ];
- } else presenceData.details = "Viewing all case studies";
- break;
- }
- case "deployments": {
- if (!docusContent) {
- presenceData.smallImageKey = Assets.Reading;
- presenceData.details = `Reading about ${ogTitle} nuxt deployment`;
- if (subActive?.textContent) presenceData.state = subActive.textContent;
- presenceData.buttons = [
- {
- label: strings.buttonReadArticle,
- url: href,
- },
- ];
- } else presenceData.details = "Viewing all deployment options";
- break;
- }
- case "announcements": {
- if (!docusContent) {
- presenceData.smallImageKey = Assets.Reading;
- presenceData.details = "Reading announcement about";
- if (subActive?.textContent)
- presenceData.state = `${ogTitle} - ${subActive.textContent}`;
- else presenceData.state = ogTitle;
- presenceData.buttons = [
- {
- label: strings.buttonReadArticle,
- url: href,
- },
- ];
- presenceData.largeImageKey = imgPath(
- document.querySelector('[class="object-cover"]')?.getAttribute("src"),
- hostname
- );
- } else presenceData.details = "Viewing all announcements";
- break;
- }
- case "docs": {
- if (!docusContent && docTitle) {
- presenceData.details = "Reading documentation about";
- presenceData.state = `${document
- .querySelector('li[class="active"] > h5')
- ?.textContent?.trim()} - ${docTitle.textContent}`;
- presenceData.buttons = [
- {
- label: strings.buttonReadArticle,
- url: href,
- },
- ];
- } else presenceData.details = "Browsing the docs";
- break;
- }
- case "examples": {
- if (!docusContent && docTitle) {
- presenceData.details = "Viewing an example about";
- presenceData.state = `${document
- .querySelector('li[class="active"] > h5')
- ?.textContent?.trim()} - ${docTitle.textContent}`;
- presenceData.buttons = [
- {
- label: strings.buttonReadArticle,
- url: href,
- },
- ];
- } else presenceData.details = "Browsing examples";
- break;
- }
- case "tutorials": {
- if (!docusContent) {
- presenceData.details = "Reading a tutorial about";
- presenceData.state = capitalizeFirstLetter(ogTitle);
- presenceData.buttons = [
- {
- label: "View Tutorial",
- url: href,
- },
- ];
- } else presenceData.details = "Viewing all tutorials";
- break;
- }
- default: {
- const pages: Record<string, PresenceData> = {
- "": {
- details: strings.viewHome,
- },
- design: {
- details: "Viewing design options",
- },
- faq: {
- details: "Viewing FAQs",
- },
- partners: {
- details: "Viewing partners",
- },
- support: {
- details: "Viewing the support page",
- },
- teams: {
- details: "Viewing the Nuxt.js team",
- },
- videocourses: {
- details: "Viewing all video coruses",
- },
- sponsors: {
- details: "Viewing all sponsors",
- },
- releases: {
- details: "Viewing all releases",
- },
- themes: {
- details: "Viewing all themes",
- },
- testimonials: {
- smallImageKey: Assets.Reading,
- details: "Reading all testimonials",
- buttons: [
- {
- label: "Read Testimonials",
- url: href,
- },
- ],
- },
- showcase: {
- details: `Viewing ${document
- .querySelector('button[class*="rounded-md"]')
- ?.textContent?.toLowerCase()} showcases`,
- },
- };
- for (const [path, data] of Object.entries(pages)) {
- if (pathname.replace(/-/gm, "").includes(path))
- presenceData = { ...presenceData, ...data };
- }
- }
- }
- if (buttons && !presenceData.buttons && pathname !== "/") {
- presenceData.buttons = [
- {
- label: strings.buttonViewPage,
- url: href,
- },
- ];
- }
- if (!buttons && presenceData.buttons) delete presenceData.buttons;
- if (!covers && presenceData.largeImageKey !== Assets.Logo)
- presenceData.largeImageKey = Assets.Logo;
- if (presenceData.details) presence.setActivity(presenceData);
- else presence.setActivity();
- });
|