Outlook Web App Unread Count Favicon

Adds an unread count favicon

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Outlook Web App Unread Count Favicon
// @namespace    http://mattstow.com
// @version      0.1
// @description  Adds an unread count favicon
// @author       Matt Stow
// @match        https://outlook.office365.com/mail/*
// @match        https://outlook.live.com/mail/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    if (window.top !== window.self) {
        return;
    }

    var faviconEl = document.querySelector('[rel="shortcut icon"]');

    function setUnreadIconOnLoad (el) {
        var unreadEl = el.querySelector('.screenReaderOnly');
        var count = unreadEl ? unreadEl.parentElement.textContent.replace('unread', '') : 'O';

        faviconEl.href = 'https://dummyimage.com/32x32/0072c6/fff.png&text=' + count;
    }

    function setUreadIconOnChange (el) {
        var replacedUnread = el.textContent.replace('unread', '');
        var count = replacedUnread ? replacedUnread : 'O';

        faviconEl.href = 'https://dummyimage.com/32x32/0072c6/fff.png&text=' + count;
    }

    setTimeout(function () {
        var inboxEl = document.querySelector('[title="Inbox"]');
        var unreadConfig = { characterData: true, childList: true, subtree: true };
        var faviconConfig = { attributeFilter: ['href'] };

        var unreadObserver = new MutationObserver(function (mutations) {
            setUreadIconOnChange(mutations[0].target);
        });

        var faviconObserver = new MutationObserver(function (mutations) {
            mutations.forEach(function (mutation) {
                if (!mutation.target.href.includes('dummyimage.com')) {
                    setUnreadIconOnLoad(inboxEl);
                }
            });
        });

        setUnreadIconOnLoad(inboxEl);
        unreadObserver.observe(inboxEl, unreadConfig);
        faviconObserver.observe(faviconEl, faviconConfig);
    }, 2000);
})();