Reddit notifications dropdown

add notifications dropdown to reddit

// ==UserScript==
// @name                Reddit notifications dropdown
// @namespace           https://greatest.deepsurf.us/users/821661
// @version             1.0.7
// @description         add notifications dropdown to reddit
// @author              hdyzen
// @match               https://www.reddit.com/*
// @run-at              document-start
// @allFrames           true
// @grant               GM_addStyle
// @icon                https://www.google.com/s2/favicons?domain=www.reddit.com/&sz=64
// @license             GPL-3.0-only
// ==/UserScript==

function initializeDropdown() {
    addMainStyles();

    const notificationDropdown = createIframeNotifications();

    const observer = new MutationObserver(() => {
        const notifications = document.querySelector("#notifications-inbox-button:not(:has(#notifications-dropdown))");

        if (!notifications || notifications.contains(notificationDropdown)) {
            return;
        }

        setupEventListeners(notifications);
        insertDropdown(notifications, notificationDropdown);
    });

    observer.observe(document.documentElement, { childList: true, subtree: true });
}

function handleIframeMode() {
    addIframeStyles();
    setupIframeLinkHandler();
}

function createIframeNotifications() {
    const iframe = document.createElement("iframe");
    iframe.id = "notifications-dropdown";
    iframe.src = "https://www.reddit.com/notifications?dropdown=true";
    return iframe;
}

function insertDropdown(button, dropdown) {
    button.appendChild(dropdown);
}

function setupEventListeners(button) {
    button.addEventListener("click", (event) => {
        event.preventDefault();
        event.stopPropagation();
        event.stopImmediatePropagation();
        button.classList.toggle("dropdown-visible");
    });
}

function addMainStyles() {
    GM_addStyle(`
        #notifications-inbox-button {
            overflow: visible !important;
            position: relative !important;
        }
        #notifications-dropdown {
            display: none;
            margin-inline: 1rem;
            position: absolute;
            top: 100%;
            right: 0;
            z-index: 99999999;
            height: clamp(5rem, 35rem, calc(100vh - var(--shreddit-header-height, 56px)));
            border: 1px solid var(--color-inverted-neutral-content);
            border-radius: 1rem;
            box-shadow: var(--elevation-md);
        }
        .dropdown-visible #notifications-dropdown {
            display: block;
        }
    `);
}

function addIframeStyles() {
    GM_addStyle(`
        *:not(:has(notifications-main-manager), notifications-main-manager, notifications-main-manager *),
        notifications-main-manager h1 {
            display: none !important;
        }
        shreddit-app {
            padding: 0 !important;
        }    
        shreddit-app[devicetype="mobile"] {
            margin-inline: 1rem;
        }
    `);
}

function setupIframeLinkHandler() {
    let interceptingNavigation;

    document.addEventListener("click", () => {
        if (interceptingNavigation) {
            return;
        }
        interceptingNavigation = true;

        unsafeWindow.navigation.addEventListener("navigate", (event) => {
            event.preventDefault();
            unsafeWindow.top.navigation.navigate(event.destination.url);
        });
    });
}

if (unsafeWindow.location.search.includes("dropdown=true")) {
    handleIframeMode();
} else {
    initializeDropdown();
}