forked from PreMiD/Presences
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(WhatsApp): rewrite presence & add features (PreMiD#7832)
* 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
Showing
2 changed files
with
59 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |