Remove Important YouTube Notifications

Removes the "Important" section in the YouTube notification menu, putting the notifications whithin it back to their appropriate position in the notification list.

Tính đến 05-04-2025. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Remove Important YouTube Notifications
// @namespace   greasyfork.org/en/users/1436613
// @match       https://www.youtube.com/*
// @version     1.0
// @license     MIT
// @author      gosha305
// @description Removes the "Important" section in the YouTube notification menu, putting the notifications whithin it back to their appropriate position in the notification list.
// ==/UserScript==

let orderOfPrecedence;
let regularNotificationPanel;
let importantNotificationPanel;
let notificationsHandled = false;

const hideImportantNotificationsSection = "ytd-popup-container #sections:has(ytd-notification-renderer),ytd-popup-container #sections:has(ytd-notification-renderer) #section-title {display:none;} ytd-popup-container #sections:has(ytd-notification-renderer.unimportantNotification){display:unset !important;}";

let menuRemover = document.createElement("style");
menuRemover.textContent = hideImportantNotificationsSection;
document.head.appendChild(menuRemover);

let popupContainer = document.querySelector("ytd-popup-container");

(new MutationObserver(function(_,observer){
  const notificationMenu = document.querySelector("ytd-popup-container")?.lastChild
  if (notificationMenu?.nodeName == "TP-YT-IRON-DROPDOWN"){
    (new MutationObserver(function(mutationList){replaceNotifications(mutationList);})).observe(notificationMenu, {attributes: true});
    observer.disconnect();
  }
})).observe(popupContainer, {childList: true, subtree:true})

function replaceNotifications(mutationList){
  const importantNotifications = document.querySelectorAll("yt-multi-page-menu-section-renderer:nth-of-type(1) #items > ytd-notification-renderer");
  if (!importantNotifications || importantNotifications.length == 0) {
    return;
  }
  regularNotificationPanel = document.querySelector("yt-multi-page-menu-section-renderer:nth-of-type(2) #items");
  orderOfPrecedence = getOrderOfPrecendence();
  importantNotifications.forEach(element => {regularNotificationPanel.insertBefore(element,findAppropriatePosition(element)); element.classList.add("unimportantNotification")});
}


function getOrderOfPrecendence(){
  const seen = new Set();
  const nonNumberDateParts = Array.from(regularNotificationPanel.querySelectorAll("ytd-notification-renderer .metadata.style-scope.ytd-notification-renderer > yt-formatted-string:nth-of-type(2)")).map(e => e.textContent.split(" ").filter(part => isNaN(Number(part))).join(''));
  return nonNumberDateParts.filter(e => {if (seen.has(e)) return false; seen.add(e); return true;})
}


function findAppropriatePosition(element){
  const elementParts = element.querySelector(".metadata.style-scope.ytd-notification-renderer > yt-formatted-string:nth-of-type(2)").textContent.split(" ");
  const nonNumberPartsRank = orderOfPrecedence.indexOf(elementParts.filter(e => isNaN(Number(e))).join(''));
  const NumberParts = elementParts.find(e => !isNaN(Number(e)))
  const rep = Array.from(regularNotificationPanel.querySelectorAll("ytd-notification-renderer")).find(e => !compareUploadDateText(e.querySelector(".metadata.style-scope.ytd-notification-renderer > yt-formatted-string:nth-of-type(2)").textContent, nonNumberPartsRank, NumberParts));
  return rep;
}

function compareUploadDateText(date1, nonNumberPartsRank2,date2NumberParts){
  const date1Parts = date1.split(" ");
  const nonNumberPartsRank1 = orderOfPrecedence.indexOf(date1Parts.filter(e => isNaN(Number(e))).join(''));
  if (nonNumberPartsRank1 < nonNumberPartsRank2){
    return true;
  } else if (nonNumberPartsRank1 > nonNumberPartsRank2){
    return false;
  } else {
    return Number(date1Parts.find(e => !isNaN(Number(e)))) < date2NumberParts
  }
}