Skip to content

Commit

Permalink
Merge branch 'upstream/main' into notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
dusansimic committed Sep 3, 2023
2 parents c0b49cd + 0d38a71 commit c7eb882
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
9 changes: 6 additions & 3 deletions source/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,13 @@ async function closePreferences(): Promise<void> {

const preferencesOverlay = document.querySelector(selectors.preferencesSelector)!;

preferencesOverlayObserver.observe(preferencesOverlay, {childList: true});
// Get the parent of preferences, that's not getting deleted
const preferencesParent = preferencesOverlay.closest('div:not([class])')!;

const closeButton = await elementReady<HTMLElement>(selectors.closePreferencesButton, {stopOnDomReady: false});
closeButton?.click();
preferencesOverlayObserver.observe(preferencesParent, {childList: true});

const closeButton = preferencesOverlay.querySelector(selectors.closePreferencesButton)!;
(closeButton as HTMLElement)?.click();
}

function insertionListener(event: AnimationEvent): void {
Expand Down
22 changes: 22 additions & 0 deletions source/browser/conversation-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,19 @@ function countUnread(mutationsList: MutationRecord[]): void {
}
}

async function updateTrayIcon(): Promise<void> {
const chatsIcon = await elementReady(selectors.chatsIcon, {
stopOnDomReady: false,
});

// Extract messageCount from ariaLabel
const messageCount = chatsIcon?.ariaLabel?.match(/\d+/g) ?? 0;
ipc.callMain('update-tray-icon', messageCount);
}

window.addEventListener('load', async () => {
const sidebar = await elementReady('[role=navigation]:has([role=grid])', {stopOnDomReady: false});
const leftSidebar = await elementReady(`${selectors.leftSidebar}:has(${selectors.chatsIcon})`, {stopOnDomReady: false});

if (sidebar) {
const conversationListObserver = new MutationObserver(async () => sendConversationList());
Expand All @@ -246,4 +257,15 @@ window.addEventListener('load', async () => {
attributeFilter: ['class'],
});
}

if (leftSidebar) {
const chatsIconObserver = new MutationObserver(async () => updateTrayIcon());

chatsIconObserver.observe(leftSidebar, {
subtree: true,
childList: true,
attributes: true,
attributeFilter: ['aria-label'],
});
}
});
6 changes: 4 additions & 2 deletions source/browser/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
export default {
leftSidebar: '[class="x9f619 x1n2onr6 x1ja2u2z x78zum5 xdt5ytf x2lah0s x193iq5w xeuugli xycxndf xkhd6sd x4uap5 xexx8yu x18d9i69"]', // ! Tray icon dependency
chatsIcon: '[class="x1i10hfl xjqpnuy xa49m3k xqeqjp1 x2hbi6w x13fuv20 xu3j5b3 x1q0q8m5 x26u7qi x972fbf xcfux6l x1qhh985 xm0m39n x9f619 x1ypdohk xdl72j9 x2lah0s xe8uvvx x2lwn1j xeuugli x4uap5 xkhd6sd x1n2onr6 x16tdsg8 x1hl2dhg xggy1nq x1ja2u2z x1t137rt x87ps6o x1lku1pv x1a2a7pz x6s0dn4 x1q0g3np xn3w4p2 x1nn3v0j x1120s5i x1av1boa x1lq5wgf xgqcy7u x30kzoy x9jhf4c xdj266r x11i5rnm xat24cr x1mh8g0r x78zum5"]', // ! Tray icon dependency
conversationList: '[role=navigation] [role=grid] [class="x1n2onr6"]',
conversationSelector: '[role=main] [role=grid]',
notificationCheckbox: '._374b:nth-of-type(4) ._4ng2 input',
rightSidebarButtons: '.rq0escxv.l9j0dhe7.du4w35lb.j83agx80.cbu4d94t.g5gj957u.f4tghd1a.ifue306u.kuivcneq.t63ysoy8 [role=button]',
rightSidebarSegments: '.oajrlxb2.gs1a9yip.g5ia77u1.mtkw9kbi.tlpljxtp.qensuy8j.ppp5ayq2.goun2846.ccm00jje.s44p3ltw.mk2mc5f4.rt8b4zig.n8ej3o3l.agehan2d.sk4xxmp2.rq0escxv.nhd2j8a9.mg4g778l.pfnyh3mw.p7hjln8o.kvgmc6g5.cxmmr5t8.oygrvhab.hcukyx3x.tgvbjcpo.hpfvmrgz.jb3vyjys.rz4wbd8a.qt6c0cv9.a8nywdso.l9j0dhe7.i1ao9s8h.esuyzwwr.f1sip0of.du4w35lb.btwxx1t3.abiwlrkh.p8dawk7l.j83agx80.lzcic4wl.beltcj47.p86d2i9g.aot14ch1.kzx2olss',
muteIconNewDesign: 'path[d="M29.676 7.746c.353-.352.44-.92.15-1.324a1 1 0 00-1.524-.129L6.293 28.29a1 1 0 00.129 1.523c.404.29.972.204 1.324-.148l3.082-3.08A2.002 2.002 0 0112.242 26h15.244c.848 0 1.57-.695 1.527-1.541-.084-1.643-1.87-1.145-2.2-3.515l-1.073-8.157-.002-.01a1.976 1.976 0 01.562-1.656l3.376-3.375zm-9.165 20.252H15.51c-.313 0-.565.275-.506.575.274 1.38 1.516 2.422 3.007 2.422 1.49 0 2.731-1.042 3.005-2.422.06-.3-.193-.575-.505-.575zm-10.064-6.719L22.713 9.02a.997.997 0 00-.124-1.51 7.792 7.792 0 00-12.308 5.279l-1.04 7.897c-.089.672.726 1.074 1.206.594z"]',
// ! Very fragile selector (most likely cause of hidden dialog issue)
closePreferencesButton: 'div[role=dialog] .x92rtbv.x10l6tqk.xomnu4r.x1vjfegm div[role=button]',
closePreferencesButton: 'div[role=dialog] [class="x9f619 x1n2onr6 x1ja2u2z"] div[role=button]',
userMenu: '.qi72231t.o9w3sbdw.nu7423ey.tav9wjvu.flwp5yud.tghlliq5.gkg15gwv.s9ok87oh.s9ljgwtm.lxqftegz.bf1zulr9.frfouenu.bonavkto.djs4p424.r7bn319e.bdao358l.fsf7x5fv.tgm57n0e.jez8cy9q.s5oniofx.m8h3af8h.l7ghb35v.kjdc1dyq.kmwttqpk.dnr7xe2t.aeinzg81.srn514ro.oxkhqvkx.rl78xhln.nch0832m.om3e55n1.cr00lzj9.rn8ck1ys.s3jn8y49.g4tp4svg.o9erhkwx.dzqi5evh.hupbnkgi.hvb2xoa8.fxk3tzhb.jl2a5g8c.f14ij5to.l3ldwz01.icdlwmnq > .aglvbi8b.om3e55n1.i8zpp7h3.g4tp4svg',
userMenuNewSidebar: '[role=navigation] [role=button]',
selectedConversation: '[role=navigation] [role=grid] [role=row] [role=gridcell] [role=link][aria-current]',
// ! Very fragile selector (most likely cause of hidden dialog issue)
preferencesSelector: 'div[class="x78zum5 xdt5ytf xg6iff7 x1n2onr6 xwnxf6m"]',
preferencesSelector: 'div[role=dialog][class="x1n2onr6 x1ja2u2z x1afcbsf x78zum5 xdt5ytf x1a2a7pz x6ikm8r x10wlt62 x71s49j x1jx94hy xyi19xy x1ccrb07 xtf3nb5 x1pc53ja x104qc98 x1g2kw80 x16n5opg xl7ujzl xhkep3z xeb55yp x17omtbh"]',
// TODO: Fix this selector for new design
messengerSoundsSelector: '._374d ._6bkz',
conversationMenuSelectorNewDesign: '[role=menu]',
Expand Down
27 changes: 3 additions & 24 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,7 @@ app.on('ready', () => {
});
});

function getMessageCount(conversations: Conversation[]): number {
return conversations.filter(({unread}) => unread).length;
}

async function updateBadge(conversations: Conversation[]): Promise<void> {
// Ignore `Sindre messaged you` blinking
if (!Array.isArray(conversations)) {
return;
}

const messageCount = getMessageCount(conversations);

async function updateBadge(messageCount: number): Promise<void> {
if (!is.windows) {
if (config.get('showUnreadBadge') && !isDNDEnabled) {
app.badgeCount = messageCount;
Expand Down Expand Up @@ -154,16 +143,6 @@ function updateOverlayIcon({data, text}: {data: string; text: string}): void {
mainWindow.setOverlayIcon(img, text);
}

function updateTrayIcon(): void {
if (!config.get('showTrayIcon') || config.get('quitOnWindowClose')) {
tray.destroy();
} else {
tray.create(mainWindow);
}
}

ipc.answerRenderer('update-tray-icon', updateTrayIcon);

interface BeforeSendHeadersResponse {
cancel?: boolean;
requestHeaders?: Record<string, string>;
Expand Down Expand Up @@ -437,8 +416,8 @@ function createMainWindow(): BrowserWindow {
}

// Update badge on conversations change
ipc.answerRenderer('conversations', async (conversations: Conversation[]) => {
updateBadge(conversations);
ipc.answerRenderer('update-tray-icon', async (messageCount: number) => {
updateBadge(messageCount);
});

enableHiresResources();
Expand Down

0 comments on commit c7eb882

Please sign in to comment.