YouTube Deactivate Important Notifications

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

目前为 2025-04-05 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube Deactivate Important Notifications
  3. // @namespace greasyfork.org/en/users/1436613
  4. // @match https://www.youtube.com/*
  5. // @version 1.1
  6. // @license MIT
  7. // @author gosha305
  8. // @description Removes the "Important" section in the YouTube notification menu, putting the notifications whithin it back to their appropriate position in the notification list.
  9. // ==/UserScript==
  10.  
  11. let orderOfPrecedence;
  12. let regularNotificationPanel;
  13. let importantNotificationPanel;
  14. let notificationsHandled = false;
  15.  
  16. const hideImportantNotificationsSection = "#sections yt-multi-page-menu-section-renderer:has(ytd-notification-renderer),ytd-popup-container #sections:has(ytd-notification-renderer) #section-title {display:none;} .unimportantNotifications{display:unset !important;}";
  17.  
  18. let menuRemover = document.createElement("style");
  19. menuRemover.textContent = hideImportantNotificationsSection;
  20. document.head.appendChild(menuRemover);
  21.  
  22. let popupContainer;
  23. function findPopup(){
  24. popupContainer = document.querySelector("ytd-popup-container")
  25. if (!popupContainer){
  26. setTimeout(200, findPopup);
  27. }
  28. }
  29. findPopup();
  30.  
  31. (new MutationObserver(function(_,observer){
  32. const notificationMenu = document.querySelector("ytd-popup-container")?.lastChild
  33. if (notificationMenu?.nodeName == "TP-YT-IRON-DROPDOWN"){
  34. (new MutationObserver(function(mutationList){replaceNotifications(mutationList);})).observe(notificationMenu, {attributes: true});
  35. observer.disconnect();
  36. }
  37. })).observe(popupContainer, {childList: true, subtree:true})
  38.  
  39.  
  40. function replaceNotifications(mutationList){
  41. const importantNotifications = document.querySelectorAll("yt-multi-page-menu-section-renderer:nth-of-type(1) #items > ytd-notification-renderer");
  42. if (!importantNotifications || importantNotifications.length == 0) {
  43. return;
  44. }
  45. regularNotificationPanel = document.querySelector("yt-multi-page-menu-section-renderer:nth-of-type(2)");
  46. regularNotificationPanelItems = regularNotificationPanel.querySelector("#items")
  47.  
  48. orderOfPrecedence = getOrderOfPrecendence();
  49. importantNotifications.forEach(notification => {regularNotificationPanelItems.insertBefore(notification,findAppropriatePosition(notification))});
  50. regularNotificationPanel.classList.add("unimportantNotifications");
  51. }
  52.  
  53.  
  54. function getOrderOfPrecendence(){
  55. const seen = new Set();
  56. 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(''));
  57. return nonNumberDateParts.filter(e => {if (seen.has(e)) return false; seen.add(e); return true;})
  58. }
  59.  
  60.  
  61. function findAppropriatePosition(element){
  62. const elementParts = element.querySelector(".metadata.style-scope.ytd-notification-renderer > yt-formatted-string:nth-of-type(2)").textContent.split(" ");
  63. const nonNumberPartsRank = orderOfPrecedence.indexOf(elementParts.filter(e => isNaN(Number(e))).join(''));
  64. const NumberParts = elementParts.find(e => !isNaN(Number(e)))
  65. 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));
  66. return rep;
  67. }
  68.  
  69. function compareUploadDateText(date1, nonNumberPartsRank2,date2NumberParts){
  70. const date1Parts = date1.split(" ");
  71. const nonNumberPartsRank1 = orderOfPrecedence.indexOf(date1Parts.filter(e => isNaN(Number(e))).join(''));
  72. if (nonNumberPartsRank1 < nonNumberPartsRank2){
  73. return true;
  74. } else if (nonNumberPartsRank1 > nonNumberPartsRank2){
  75. return false;
  76. } else {
  77. return Number(date1Parts.find(e => !isNaN(Number(e)))) < date2NumberParts
  78. }
  79. }