chimericdream / GreasemonkeyScripts

A library of my Greasemonkey scripts
1 stars 0 forks source link

Add Feedly unread count script #22

Open chimericdream opened 5 years ago

chimericdream commented 5 years ago
// ==UserScript==
// @name         Better Feedly Unread Count
// @version      1.0.0
// @description  Improve the unread count in the Feedly sidebar
// @author       chimericdream
// @match        https://feedly.com/i/*
// @grant        none
// @require      https://raw.githubusercontent.com/chimericdream/JS-Debounce/master/debounce.js
// ==/UserScript==

(function() {
  'use strict';

  const updateCount = fnDebounce(() => {
    const tabContainer = document.getElementById('feedlyTabs');
    const allItemsHeader = document.getElementById('latesttab_header');

    const tabCollection = tabContainer.getElementsByClassName('tab');
    const tabs = Array.prototype.filter.call(tabCollection, tab => tab.id.trim().match(/^user\/.+/) !== null);

    let fuzzy = false;
    let total = 0;

    tabs.forEach(tab => {
      const header = tab.children[0];
      const list = tab.children[1];
      const listItems = tab.getElementsByClassName('feedIndex');
      if (listItems.length > 0) {
        Array.prototype.forEach.call(listItems, item => {
          const itemCount = item.getElementsByClassName('streamUnreadCount')[0].innerHTML;
          if (itemCount === '1k+') {
            total += 1000;
            fuzzy = true;
          }
          else {
            total += Number(itemCount);
          }
        });
      }
      else {
        const itemCount = header.getElementsByClassName('streamUnreadCount')[0].innerHTML;
        if (itemCount === '1k+') {
          total += 1000;
          fuzzy = true;
        }
        else {
          total += Number(itemCount);
        }
      }
    });

    const allItemsCount = allItemsHeader.getElementsByClassName('streamUnreadCount')[0];
    if (fuzzy) {
      allItemsCount.innerHTML = `${total}+`;
    }
    else {
      allItemsCount.innerHTML = total;
    }
  }, 500);

  let realObserver = null;

  const cb = (mutationsList, observer) => {
    for (const mutation of mutationsList) {
      if (mutation.type === 'childList') {
        const tabContainer = document.getElementById('feedlyTabs');
        if (tabContainer !== null) {
          realObserver = new MutationObserver(updateCount);
          realObserver.observe(tabContainer, {childList: true, subtree: true})
          observer.disconnect();
        }
      }
    }
  };

  const observer = new MutationObserver(cb);
  observer.observe(document, {childList: true, subtree: true});
})();