Google Fi Messages

Adds filtering to show only unread messages.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Google Fi Messages
// @namespace    http://tampermonkey.net/
// @version      2024-07-03
// @description  Adds filtering to show only unread messages.
// @author       gofreddo
// @match        https://messages.google.com/web/conversations
// @icon         https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to hide conversation items with data-e2e-is-unread="false"
    function hideUnreadFalseItems() {
        // Select all mws-conversation-list-item elements
        const conversationItems = document.querySelectorAll('mws-conversation-list-item');

        // Loop through each conversation item
        conversationItems.forEach(item => {
            // Find the child 'a' element with the attribute data-e2e-is-unread
            const aElement = item.querySelector('a[data-e2e-is-unread]');
            // Check if the attribute value is "false"
            if (aElement && aElement.getAttribute('data-e2e-is-unread') === 'false') {
                // Hide the conversation item
                item.style.display = 'none';
            }
        });
    }

    // Function to show all conversation items
    function showAll() {
        // Select all mws-conversation-list-item elements
        const conversationItems = document.querySelectorAll('mws-conversation-list-item');

        // Loop through each conversation item
        conversationItems.forEach(item => {
            item.style.display = '';
        });
    }

    // Function to add the "Unread" and "Show All" links to the main-nav-header
    function addNavLinks() {
        // Select the div with class "main-nav-header"
        const mainNavHeader = document.querySelector('.main-nav-header');

        if (mainNavHeader) {
            // Create the "Unread" link
            if (!mainNavHeader.querySelector('.unread-link')) {
                const unreadLink = document.createElement('a');
                unreadLink.href = '#';
                unreadLink.textContent = 'Unread';
                unreadLink.className = 'unread-link';
                unreadLink.style.marginLeft = '10px'; // Add some margin for better spacing

                // Add a click event listener to run hideUnreadFalseItems when clicked
                unreadLink.addEventListener('click', (event) => {
                    event.preventDefault(); // Prevent the default link behavior
                    hideUnreadFalseItems();
                });

                // Append the "Unread" link to the main-nav-header
                mainNavHeader.appendChild(unreadLink);
            }

            // Create the "Show All" link
            if (!mainNavHeader.querySelector('.show-all-link')) {
                const showAllLink = document.createElement('a');
                showAllLink.href = '#';
                showAllLink.textContent = 'Show All';
                showAllLink.className = 'show-all-link';
                showAllLink.style.marginLeft = '10px'; // Add some margin for better spacing

                // Add a click event listener to run showAll when clicked
                showAllLink.addEventListener('click', (event) => {
                    event.preventDefault(); // Prevent the default link behavior
                    showAll();
                });

                // Append the "Show All" link to the main-nav-header
                mainNavHeader.appendChild(showAllLink);
            }
        }
    }

    // Create a MutationObserver to watch for changes in the DOM
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            // Check if nodes were added
            mutation.addedNodes.forEach(node => {
                addNavLinks();
            });
        });
    });

    // Configure the observer to watch for child node additions
    const config = { childList: true, subtree: true };

    // Start observing the body or the container element that holds the conversation items
    observer.observe(document.body, config);

    // Initial run to add the nav links
    addNavLinks();

})();