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

refactor(Minecraft Wiki) use mw.config #8330

Merged
merged 12 commits into from
Apr 29, 2024
Merged
2 changes: 1 addition & 1 deletion websites/M/Minecraft Wiki/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"*://minecraft.wiki/*",
"*://*.minecraft.wiki/*"
],
"version": "1.0.1",
"version": "1.0.2",
"logo": "https://cdn.rcd.gg/PreMiD/websites/M/Minecraft%20Wiki/assets/logo.png",
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/M/Minecraft%20Wiki/assets/thumbnail.png",
"color": "#3776A9",
Expand Down
181 changes: 107 additions & 74 deletions websites/M/Minecraft Wiki/presence.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { getUserNamespace, getTalkNamespace } from "./util";

const presence = new Presence({
clientId: "1232903356025143297",
}),
browsingTimestamp = Math.floor(Date.now() / 1000);
let veactionLast: string | null = null;

function hasPermissions(): boolean {
return !document.querySelector(".permissions-errors");
}

presence.on("UpdateData", async () => {
async function prepare(): Promise<PresenceData> {
const presenceData: PresenceData = {
startTimestamp: browsingTimestamp,
},
{ href, pathname, search } = document.location,
{ href, search } = document.location,
searchParams = new URLSearchParams(search),
strings = await presence.getStrings({
viewHome: "general.viewHome",
Expand All @@ -38,27 +37,46 @@ presence.on("UpdateData", async () => {
btnViewThread: "apple.btnViewThread",
viewWatchlist: "minecraft wiki.viewWatchlist",
}),
mainPath = pathname.split("/").filter(Boolean)[1] ?? "/",
pageTitle = document.querySelector<HTMLMetaElement>(
"meta[property='og:title']"
)?.content,
specialNamespace = new URL(
document.querySelector<HTMLAnchorElement>("#t-specialpages a").href
).pathname
.split("/")
.filter(Boolean)[1]
.match(/.*(?=:[^_])/)?.[0],
userNamespace = await getUserNamespace(),
talkNamespace = await getTalkNamespace(),
currentNamespace = mainPath.match(/.*(?=:[^_])/)?.[0] ?? "";
{
"mw.config.values.wgPageName": wgPageName,
"mw.config.values.wgNamespaceNumber": wgNamespaceNumber,
"mw.config.values.wgTitle": wgTitle,
"mw.config.values.wgCanonicalSpecialPageName": wgCanonicalSpecialPageName,
"mw.config.values.wgRelevantPageName": wgRelevantPageName,
"mw.config.values.wgRelevantUserName": wgRelevantUserName,
"mw.config.values.wgIsMainPage": wgIsMainPage,
} = await presence.getPageVariable<{
"mw.config.values.wgPageName": string;
"mw.config.values.wgNamespaceNumber": number;
"mw.config.values.wgTitle": string;
"mw.config.values.wgCanonicalSpecialPageName": string | false;
"mw.config.values.wgRelevantPageName": string;
"mw.config.values.wgRelevantUserName": string | null;
"mw.config.values.wgIsMainPage": boolean | null;
}>(
"mw.config.values.wgPageName",
"mw.config.values.wgNamespaceNumber",
"mw.config.values.wgTitle",
"mw.config.values.wgCanonicalSpecialPageName",
"mw.config.values.wgRelevantPageName",
"mw.config.values.wgRelevantUserName",
"mw.config.values.wgIsMainPage"
),
pageTitle = wgPageName.replace(/_/g, " ");

veactionLast = searchParams.get("veaction");

presenceData.largeImageKey = getComputedStyle(
document.querySelector<HTMLAnchorElement>(".mw-wiki-logo")
).backgroundImage.match(/url\("(.+)"\)/)[1];
presenceData.largeImageKey =
getComputedStyle(
document.querySelector<HTMLAnchorElement>(".mw-wiki-logo")
).backgroundImage.match(/url\("(.+)"\)/)[1] ??
"https://cdn.rcd.gg/PreMiD/websites/M/Minecraft%20Wiki/assets/logo.png";

if (
searchParams.get("action") === "edit" ||
searchParams.get("veaction") === "edit"
searchParams.get("action") === "submit" ||
searchParams.get("veaction") === "edit" ||
searchParams.get("veaction") === "editsource"
) {
presenceData.details = hasPermissions()
? strings.editing
Expand All @@ -78,67 +96,82 @@ presence.on("UpdateData", async () => {
} else if (searchParams.get("search")) {
presenceData.details = strings.search;
presenceData.state = searchParams.get("search");
} else if (mainPath === "/") presenceData.details = strings.viewHome;
else if (currentNamespace === userNamespace) {
} else if (wgNamespaceNumber === 2) {
// User namespace
presenceData.details = strings.viewUser;
presenceData.state = pageTitle.slice(
decodeURIComponent(userNamespace).length + 1
);
presenceData.state = wgTitle;
presenceData.buttons = [{ label: strings.buttonViewProfile, url: href }];
} else if (
currentNamespace.toLowerCase().includes(talkNamespace.toLowerCase())
) {
} else if (wgNamespaceNumber % 2 === 1) {
// All talk namespaces
presenceData.details = strings.viewAThread;
presenceData.state =
currentNamespace === talkNamespace
? document.querySelector<HTMLSpanElement>(".mw-page-title-main")
: pageTitle;
presenceData.state = wgNamespaceNumber === 1 ? wgTitle : pageTitle;
presenceData.buttons = [{ label: strings.btnViewThread, url: href }];
} else if (currentNamespace === specialNamespace) {
if (document.querySelector<HTMLFormElement>("#mw-prefs-form"))
// Preferences (Special:Preferences)
presenceData.details = strings.advancedSettings;
else if (document.querySelector<HTMLFormElement>("#mw-watchlist-form"))
// Subscriptions (Special:Watchlist)
presenceData.details = strings.viewWatchlist;
else if (document.querySelector<HTMLUListElement>(".mw-rcfilters-head")) {
// Recent changes (Special:RecentChanges, Special:RecentChangesLinked)
presenceData.details = strings.viewRecentChanges;
presenceData.state = document.querySelector<HTMLAnchorElement>(
"#mw-content-subtitle a"
);
} else if (document.querySelector<HTMLFormElement>("#movepage")) {
// Moving a page (Special:MovePage)
presenceData.details = strings.moving;
presenceData.state = document.querySelector<HTMLAnchorElement>(
"#mw-content-subtitle a"
);
} else if (document.querySelector<HTMLDivElement>("#userloginForm"))
// Logging in (Special:UserLogin, Special:CreateAccount)
presenceData.details = strings.login;
else if (document.querySelector<HTMLFormElement>("#mw-upload-form"))
// Upload a file (Special:Upload)
presenceData.details = strings.upload;
else if (
document.querySelector<HTMLDivElement>(".mw-contributions-user-tools")
) {
// Contributions (Special:Contributions)
presenceData.details = strings.viewContributionsOf;
presenceData.state = pageTitle.split("/").slice(1).join("/");
} else {
presenceData.details = strings.viewAPage;
presenceData.state = pageTitle;
} else if (wgNamespaceNumber === -1) {
// Special namespace
switch (wgCanonicalSpecialPageName) {
case "Preferences":
// Preferences (Special:Preferences)
presenceData.details = strings.advancedSettings;
break;
case "Watchlist":
// Subscriptions (Special:Watchlist)
presenceData.details = strings.viewWatchlist;
break;
case "Recentchanges":
// Recent changes (Special:RecentChanges)
presenceData.details = strings.viewRecentChanges;
break;
case "Recentchangeslinked":
// Related changes (Special:RecentChangesLinked)
presenceData.details = strings.viewRecentChanges;
presenceData.state = wgRelevantPageName.replace(/_/g, " ");
break;
case "Movepage":
// Moving a page (Special:MovePage)
presenceData.details = strings.moving;
presenceData.state = wgRelevantPageName.replace(/_/g, " ");
break;
case "Userlogin":
case "CreateAccount":
// Logging in (Special:UserLogin, Special:CreateAccount)
presenceData.details = strings.login;
break;
case "Upload":
// Upload a file (Special:Upload)
presenceData.details = strings.upload;
presenceData.state = searchParams.get("wpDestFile");
break;
case "Contributions":
// Contributions (Special:Contributions)
presenceData.details = strings.viewContributionsOf;
presenceData.state = wgRelevantUserName;
break;
default:
presenceData.details = strings.viewAPage;
presenceData.state = pageTitle;
}
} else if (currentNamespace) {
const namespace = decodeURIComponent(currentNamespace);
presenceData.details = `${strings.readingAbout} ${namespace}`;
presenceData.state = pageTitle.slice(namespace.length + 1);
} else if (wgNamespaceNumber) {
// Not main namespace
presenceData.details = `${strings.readingAbout} ${pageTitle.split(":")[0]}`;
presenceData.state = wgTitle;
presenceData.buttons = [{ label: strings.buttonViewPage, url: href }];
} else {
} else if (wgIsMainPage) presenceData.details = strings.viewHome;
else {
presenceData.details = strings.viewAPage;
presenceData.state = pageTitle;
presenceData.buttons = [{ label: strings.buttonViewPage, url: href }];
}
return presenceData;
}

presence.setActivity(presenceData);
});
(async (): Promise<void> => {
let presenceData = await prepare();
presence.on("UpdateData", async () => {
if (
veactionLast !==
new URLSearchParams(document.location.search).get("veaction")
)
presenceData = await prepare();
else presence.setActivity(presenceData);
});
})();
41 changes: 0 additions & 41 deletions websites/M/Minecraft Wiki/util.ts

This file was deleted.

Loading