From eef3b310e7081c4a636bb29739fb99cb917d4974 Mon Sep 17 00:00:00 2001 From: Joseph Hale <47901316+thehale@users.noreply.github.com> Date: Tue, 23 May 2023 14:43:25 -0700 Subject: feat(tt-rss): Optionally limit notifications to a Category. (#370) --- recipes/tt-rss/webview.js | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) (limited to 'recipes/tt-rss/webview.js') diff --git a/recipes/tt-rss/webview.js b/recipes/tt-rss/webview.js index 3e3a0d3..7d90615 100644 --- a/recipes/tt-rss/webview.js +++ b/recipes/tt-rss/webview.js @@ -1,16 +1,41 @@ "use strict"; module.exports = Ferdium => { + const _notificationCategory = new URL(window.location.href).searchParams.get("ferdiumNotificationCategory"); + const getMessages = function getMessages() { - // Initialize empty vars - var unread = 0; - var match = []; - // Extract the number from the title - match = document.title.match(/^\((\d+)\) Tiny Tiny RSS$/); - // Some logic to handle the match groups - unread = match != null && match.length > 0 ? match[1] : 0; - // Set unread msgs badge - Ferdium.setBadge(Number.parseInt(unread, 10)); + if (_notificationCategory) { + countMessagesUnderCategory(_notificationCategory); + } else { + countMessages(); + } + }; + + const countMessagesUnderCategory = (notificationCategory) => { + var elements = document.querySelectorAll(".dijitTreeIsRoot") + var directMessages = 0; + var indirectMessages = 0; + for (var element of elements) { + var label = element.querySelectorAll(".dijitTreeLabel")[0]; + var unreadNode = element.querySelectorAll(".unread")[0]; + var unreadAmount = Ferdium.safeParseInt(unreadNode.textContent); + if (label.textContent === notificationCategory) { + directMessages += unreadAmount; + } else { + indirectMessages += unreadAmount; + } + } + Ferdium.setBadge(directMessages, indirectMessages); + }; + + const _countMessagesFromTitle = () => { + var match = document.title.match(/^\((\d+)\) Tiny Tiny RSS$/); + var count = match != null && match.length > 0 ? match[1] : 0; + return Ferdium.safeParseInt(count); + } + + const countMessages = () => { + Ferdium.setBadge(_countMessagesFromTitle()); }; const loopFunc = () => { -- cgit v1.2.3-70-g09d2