Hide X Purple-Star Notifications SPA Robust

Hides purple-star notifications on X, works with SPA navigation

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Hide X Purple-Star Notifications SPA Robust
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Hides purple-star notifications on X, works with SPA navigation
// @match        https://x.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let currentContainer = null;
    let observer = null;

    function hidePurpleStarNotifications(container) {
        if (!container) return;
        const paths = container.querySelectorAll('path');
        paths.forEach(path => {
            const dAttr = path.getAttribute('d');
            if (!dAttr) return;
            if (dAttr.includes("M22.99")) {
                let parent = path.parentElement;
                while (parent && parent.getAttribute('data-testid') !== 'cellInnerDiv') {
                    parent = parent.parentElement;
                }
                if (parent) parent.style.display = 'none';
            }
        });
    }

    function attachObserver(container) {
        if (observer) observer.disconnect(); // disconnect previous observer if any
        observer = new MutationObserver(() => hidePurpleStarNotifications(container));
        observer.observe(container, { childList: true, subtree: true });
        hidePurpleStarNotifications(container);
    }

    function checkNotificationsPage() {
        if (!location.href.includes("/notifications")) {
            currentContainer = null;
            if (observer) observer.disconnect();
            return;
        }

        const container = document.querySelector('div[aria-label="Timeline: Notifications"]');
        if (!container) return;

        if (container !== currentContainer) {
            currentContainer = container;
            attachObserver(container);
        }
    }

    // Check every 500ms for SPA navigation or container changes
    setInterval(checkNotificationsPage, 500);
})();