GitHub Notification Inbox Toggle

Toggle hiding or showing done notifications in GitHub inbox

当前为 2025-05-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Notification Inbox Toggle
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.18
  5. // @description Toggle hiding or showing done notifications in GitHub inbox
  6. // @match https://github.com/notifications*
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. (function() {
  11. 'use strict';
  12.  
  13. // Retrieve initial states from sessionStorage or set defaults
  14. let isHidden = sessionStorage.getItem('isHidden') === 'true';
  15. let showDoneOnly = sessionStorage.getItem('showDoneOnly') === 'true';
  16. const doneSelectors = [
  17. 'svg.octicon-issue-closed',
  18. 'svg.octicon-git-pull-request-closed',
  19. 'svg.octicon-git-merge',
  20. 'svg.octicon-x',
  21. 'svg.octicon-stop',
  22. 'svg.octicon-rocket',
  23. 'svg.octicon-check',
  24. ];
  25.  
  26. const createButton = (text, positionY) => {
  27. const button = document.createElement('button');
  28. button.textContent = text;
  29. button.style.position = 'fixed';
  30. button.style.left = '50%';
  31. button.style.transform = 'translateX(-50%)';
  32. button.style.zIndex = '1000';
  33. button.style.padding = '5px 10px';
  34. button.style.border = '1px solid #ccc';
  35. button.style.borderRadius = '4px';
  36. button.style.cursor = 'pointer';
  37. button.style.transition = 'background-color 0.3s, color 0.3s';
  38. button.style.top = `${positionY}px`;
  39. button.style.backgroundColor = 'rgba(255, 255, 255, 0.9)';
  40. button.style.color = '#333';
  41. button.addEventListener('click', (event) => {
  42. event.preventDefault();
  43. if (text === 'Toggle Hidden Notifications') {
  44. isHidden = !isHidden;
  45. showDoneOnly = false;
  46. } else if (text === 'Show Only Done Notifications') {
  47. showDoneOnly = !showDoneOnly;
  48. isHidden = false;
  49. }
  50. saveFilterState();
  51. updateVisibleNotifications();
  52. });
  53. return button;
  54. };
  55.  
  56. const toggleVisibilityButton = createButton('Toggle Hidden Notifications', 10);
  57. const toggleShowDoneButton = createButton('Show Only Done Notifications', 50);
  58. document.body.appendChild(toggleVisibilityButton);
  59. document.body.appendChild(toggleShowDoneButton);
  60.  
  61. function updateButtonState(button, isActive) {
  62. button.style.backgroundColor = isActive ? '#4caf50' : 'rgba(255, 255, 255, 0.9)';
  63. button.style.color = isActive ? '#fff' : '#333';
  64. }
  65.  
  66. function updateVisibleNotifications() {
  67. const items = document.querySelectorAll('.js-navigation-container li.notifications-list-item');
  68. items.forEach(item => {
  69. const isVisible = getComputedStyle(item).display !== 'none';
  70. const isDone = item.querySelector(':not(.notification-list-item)').querySelector(doneSelectors);
  71. const shouldShow = showDoneOnly ? isDone : !isHidden || !isDone;
  72. if (isVisible && !shouldShow) {
  73. item.style.display = 'none'; // Hide if it shouldn't be displayed
  74. } else if (!isVisible && shouldShow) {
  75. item.style.display = ''; // Show if it isnt' visible but should be
  76. }
  77. });
  78.  
  79. updateButtonState(toggleVisibilityButton, isHidden);
  80. updateButtonState(toggleShowDoneButton, showDoneOnly);
  81. }
  82.  
  83. function saveFilterState() {
  84. sessionStorage.setItem('isHidden', isHidden);
  85. sessionStorage.setItem('showDoneOnly', showDoneOnly);
  86. }
  87.  
  88. // Initial call to update visibility
  89. updateVisibleNotifications();
  90.  
  91. // Observe for changes in the notification list
  92. const observer = new MutationObserver(() => {
  93. updateVisibleNotifications();
  94. });
  95.  
  96. const targetNode = document.querySelector('.js-navigation-container');
  97. if (targetNode) {
  98. observer.observe(targetNode, { childList: true, subtree: true });
  99. }
  100.  
  101. // Add a MutationObserver to catch changes in the document
  102. const pageObserver = new MutationObserver(() => {
  103. updateVisibleNotifications(); // Apply visibility immediately
  104. });
  105.  
  106. // Observe the body for when new notifications are loaded
  107. pageObserver.observe(document.body, { childList: true, subtree: true });
  108.  
  109. // Clear observers on unload
  110. window.addEventListener('beforeunload', () => {
  111. observer.disconnect();
  112. pageObserver.disconnect();
  113. });
  114. })();