Skip to content

Commit

Permalink
fix: decode content when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
theusaf committed Apr 26, 2024
1 parent bb8d68a commit 365f6ec
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
3 changes: 2 additions & 1 deletion websites/M/Minecraft Wiki/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"google classroom",
"youtube",
"kahoot",
"amazon"
"amazon",
"apple"
],
"id": "lang"
}
Expand Down
15 changes: 9 additions & 6 deletions websites/M/Minecraft Wiki/presence.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getUserNamespace, getTalkNamespace } from "./util";

const presence = new Presence({
clientId: "1232903356025143297",
}),
Expand Down Expand Up @@ -34,6 +36,7 @@ presence.on("UpdateData", async () => {
viewRecentChanges: "minecraft wiki.viewRecentChanges",
login: "kahoot.login",
subscriptions: "amazon.subscriptions",
btnViewThread: "apple.btnViewThread",
}),
mainPath = pathname.split("/").filter(Boolean)[1] ?? "/",
pageTitle = document.querySelector<HTMLMetaElement>(
Expand All @@ -47,7 +50,7 @@ presence.on("UpdateData", async () => {
.match(/.*(?=:[^_])/)?.[0],
userNamespace = await getUserNamespace(),
talkNamespace = await getTalkNamespace(),
currentNamespace = pageTitle.match(/.*(?=:[^_])/)?.[0] ?? "";
currentNamespace = mainPath.match(/.*(?=:[^_])/)?.[0] ?? "";

presenceData.largeImageKey = getComputedStyle(
document.querySelector<HTMLAnchorElement>(".mw-wiki-logo")
Expand Down Expand Up @@ -78,7 +81,9 @@ presence.on("UpdateData", async () => {
} else if (mainPath === "/") presenceData.details = strings.viewHome;
else if (currentNamespace === userNamespace) {
presenceData.details = strings.viewUser;
presenceData.state = pageTitle.slice(5);
presenceData.state = pageTitle.slice(
decodeURIComponent(userNamespace).length + 1
);
presenceData.buttons = [{ label: strings.buttonViewProfile, url: href }];
} else if (
currentNamespace.toLowerCase().includes(talkNamespace.toLowerCase())
Expand All @@ -88,6 +93,7 @@ presence.on("UpdateData", async () => {
currentNamespace === talkNamespace
? document.querySelector<HTMLSpanElement>(".mw-page-title-main")
: pageTitle;
presenceData.buttons = [{ label: strings.btnViewThread, url: href }];
} else if (currentNamespace === specialNamespace) {
// Preferences (Special:Preferences)
if (document.querySelector<HTMLFormElement>("#mw-prefs-form"))
Expand Down Expand Up @@ -118,10 +124,7 @@ presence.on("UpdateData", async () => {
presenceData.state = pageTitle;
}
} else if (currentNamespace) {
const namespace = pageTitle.slice(
0,
mainPath.match(/.*(?=:[^_])/)[0].length
);
const namespace = decodeURIComponent(currentNamespace);
presenceData.details = `${strings.readingAbout} ${namespace}`;
presenceData.state = pageTitle.slice(namespace.length + 1);
presenceData.buttons = [{ label: strings.buttonViewPage, url: href }];
Expand Down
32 changes: 26 additions & 6 deletions websites/M/Minecraft Wiki/util.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
async function getUserNamespace() {
const promiseCache = new Map<string, Promise<any>>();

export async function getUserNamespace() {
const cached = sessionStorage.getItem("PMD_UserNamespace");
if (cached) return cached;
const response = await fetch("/w/User:Example");
let response: Response;
const promise = promiseCache.get("getUserNamespace");
if (promise) response = await promise;
else {
const promise = fetch(
`https://${document.location.hostname}/w/User:Example`
);
promiseCache.set("getUserNamespace", promise);
response = await promise;
}
sessionStorage.setItem(
"PMD_UserNamespace",
response.url.split("/").filter(Boolean)[1].split(":")[0]
new URL(response.url).pathname.split("/").filter(Boolean)[1].split(":")[0]
);
return sessionStorage.getItem("PMD_UserNamespace");
}

async function getTalkNamespace() {
export async function getTalkNamespace() {
const cached = sessionStorage.getItem("PMD_TalkNamespace");
if (cached) return cached;
const response = await fetch("/w/Talk:Example");
let response: Response;
const promise = promiseCache.get("getTalkNamespace");
if (promise) response = await promise;
else {
const promise = fetch(
`https://${document.location.hostname}/w/Talk:Example`
);
promiseCache.set("getTalkNamespace", promise);
response = await promise;
}
sessionStorage.setItem(
"PMD_TalkNamespace",
response.url.split("/").filter(Boolean)[1].split(":")[0]
new URL(response.url).pathname.split("/").filter(Boolean)[1].split(":")[0]
);
return sessionStorage.getItem("PMD_TalkNamespace");
}

0 comments on commit 365f6ec

Please sign in to comment.