Skip to content

Commit

Permalink
feat(WhatsApp): rewrite presence & add features (PreMiD#7832)
Browse files Browse the repository at this point in the history
* feat(WhatsApp): rewrite presence & add features

* feat(WhatsApp): rewrite presence & add features

* Apply suggestions from code review

Co-authored-by: Daniel Lau <[email protected]>
Signed-off-by: Dark_Ville <[email protected]>

---------

Signed-off-by: Dark_Ville <[email protected]>
Co-authored-by: Daniel Lau <[email protected]>
  • Loading branch information
Dark_Ville and theusaf authored Dec 24, 2023
1 parent 7ae5f2a commit da0bb40
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 40 deletions.
2 changes: 1 addition & 1 deletion websites/W/WhatsApp/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"ga_IE": "Seol agus faigh teachtaireachtaí WhatsApp go tapa ó do ríomhaire."
},
"url": "web.whatsapp.com",
"version": "2.2.6",
"version": "2.3.0",
"logo": "https://cdn.rcd.gg/PreMiD/websites/W/WhatsApp/assets/logo.png",
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/W/WhatsApp/assets/thumbnail.jpg",
"color": "#0dc143",
Expand Down
97 changes: 58 additions & 39 deletions websites/W/WhatsApp/presence.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,75 @@
const presence = new Presence({
clientId: "628019683718856714",
}),
getSettings = async (): Promise<{
showRecipient: boolean;
showNumbers: boolean;
}> => ({
showRecipient: await presence.getSetting<boolean>("showRecipient"),
showNumbers: await presence.getSetting<boolean>("showNumbers"),
});
browsingTimestamp = Math.floor(Date.now() / 1000);

presence.on("UpdateData", async () => {
const settings = await getSettings(),
const presenceData: PresenceData = {
largeImageKey:
"https://cdn.rcd.gg/PreMiD/websites/W/WhatsApp/assets/logo.png",
startTimestamp: browsingTimestamp,
},
typing = document.querySelector(
'span[class="selectable-text copyable-text"]'
);
),
[showRecipient, showNumbers] = await Promise.all([
presence.getSetting<boolean>("showRecipient"),
presence.getSetting<boolean>("showNumbers"),
]);

let name =
settings.showRecipient &&
document.querySelector('[data-testid="conversation-info-header"]')
.firstChild.firstChild?.textContent;
let name = document
.querySelector(".AmmtE")
?.querySelector('[class*="lhj4utae"]')?.textContent;

if (
settings.showNumbers === false &&
typeof name === "string" &&
!isNaN(Number(name.replace(/[^a-zA-Z0-9 ]/g, "").replaceAll(" ", "")))
name?.match(
/^[+]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$/gm
)?.[0] &&
!showNumbers
)
name = null;
name = "";

if (!name && !typing) {
if (!document.querySelector('[data-testid="conversation-info-header"]')) {
presence.setActivity({
largeImageKey:
"https://cdn.rcd.gg/PreMiD/websites/W/WhatsApp/assets/logo.png",
details: "Browsing...",
startTimestamp: Math.floor(Date.now() / 1000),
});
} else {
presence.setActivity({
largeImageKey:
"https://cdn.rcd.gg/PreMiD/websites/W/WhatsApp/assets/logo.png",
details: "Texting with someone",
state: "Just reading...",
startTimestamp: Math.floor(Date.now() / 1000),
});
switch (true) {
case !!document.querySelector(".ppled2lx"): {
// Community tab
presenceData.details = "Viewing communities";
break;
}
case !!document.querySelector(".mrcito7c.r96muop5"): {
// Status
presenceData.details = "Browsing all status updates";
break;
}
case !document.querySelector(
'[data-testid="conversation-info-header"]'
): {
presenceData.details = "Browsing...";
break;
}
default: {
presenceData.details = "Texting with someone";
presenceData.state = "Just reading...";
break;
}
}
} else if (document.querySelector('[role="tablist"]')) {
// if contact windows with media/documents/etc is open
presenceData.details = `Viewing ${document
.querySelector('button[aria-selected="true"]')
?.textContent?.toLowerCase()} in the chat with ${
!showRecipient ? "someone" : name
}`;
} else if (document.querySelector("._2Ts6i._1xFRo > span > div")) {
// If contact windows is open
presenceData.details = `Viewing contact info of ${
!showRecipient ? "someone" : name
}`;
} else {
presence.setActivity({
largeImageKey:
"https://cdn.rcd.gg/PreMiD/websites/W/WhatsApp/assets/logo.png",
details: `Texting with ${name || "someone"}`,
state: (typing?.textContent && "Typing...") || "Just reading...",
startTimestamp: Math.floor(Date.now() / 1000),
});
presenceData.details = `Texting with ${!showRecipient ? "someone" : name}`;
presenceData.state =
(typing?.textContent && "Typing...") || "Just reading...";
}

presence.setActivity(presenceData);
});

0 comments on commit da0bb40

Please sign in to comment.