From 78d581191ef89e81dca73d34ad16115a6b9314fc Mon Sep 17 00:00:00 2001 From: beerpsi <92439990+beerpiss@users.noreply.github.com> Date: Mon, 17 Oct 2022 19:46:23 +0700 Subject: Fix getting badge count on messenger (#145) * Simplify getting badge count * Increment version * add fallback for badge count and fix style * fix boolean comparison Co-authored-by: Alphrag <34252790+Alphrag@users.noreply.github.com> --- recipes/messenger/webview.js | 48 +++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-) (limited to 'recipes/messenger/webview.js') diff --git a/recipes/messenger/webview.js b/recipes/messenger/webview.js index f7bc85b..7fc8ed6 100644 --- a/recipes/messenger/webview.js +++ b/recipes/messenger/webview.js @@ -10,27 +10,43 @@ function hideInstallMessage() { module.exports = Ferdium => { const getMessages = () => { - /*let count = [...document.querySelectorAll('.bp9cbjyn.j83agx80.owycx6da:not(.btwxx1t3)')] - .map(elem => { - const hasPing = !!elem.querySelector('.pq6dq46d.is6700om.qu0x051f.esr5mh6w.e9989ue4.r7d6kgcz.s45kfl79.emlxlaya.bkmhp75w.spb7xbtv.cyypbtt7.fwizqjfa'); - const isMuted = !!elem.querySelector('.a8c37x1j.ms05siws.l3qrxjdp.b7h9ocf4.trssfv1o'); - - return hasPing && !isMuted; - }) - .reduce((prev, curr) => prev + curr, 0);*/ let count = 0; - for (const node of document.querySelectorAll( - '[data-testid="mwthreadlist-item"]', - )) { - if (node.querySelector('.lrazzd5p, .is6700om, .o48pnaf2')) count += 1; + let newMessengerUI = false; + + /* + * try the counting with the new UI + */ + for (let href of ['/', '/requests/', '/marketplace/']) { + const elem = document.querySelector(`a[href^='${href}t/'][role='link'][tabindex='0']`).ariaLabel; + if (elem) { + newMessengerUI = true; + const match = elem.match(/(\d+)/g); + if (match) { + count += Ferdium.safeParseInt(match[0]); + } + } } /* - * add count of message requests on top of notification counter + * do the old counting if the interface is not the last one */ - const messageRequestsElement = document.querySelector('._5nxf'); - if (messageRequestsElement) { - count += Ferdium.safeParseInt(messageRequestsElement.textContent); + if (!newMessengerUI) { + count = [...document.querySelectorAll('.bp9cbjyn.j83agx80.owycx6da:not(.btwxx1t3)')] + .map(elem => { + const hasPing = !!elem.querySelector('.pq6dq46d.is6700om.qu0x051f.esr5mh6w.e9989ue4.r7d6kgcz.s45kfl79.emlxlaya.bkmhp75w.spb7xbtv.cyypbtt7.fwizqjfa'); + const isMuted = !!elem.querySelector('.a8c37x1j.ms05siws.l3qrxjdp.b7h9ocf4.trssfv1o'); + + return hasPing && !isMuted; + }) + .reduce((prev, curr) => prev + curr, 0); + + /* + * add count of message requests on top of notification counter + */ + const messageRequestsElement = document.querySelector('._5nxf'); + if (messageRequestsElement) { + count += Ferdium.safeParseInt(messageRequestsElement.textContent); + } } Ferdium.setBadge(count); -- cgit v1.2.3-54-g00ecf