|
1 | 1 | // ==UserScript== |
2 | | -// @name Mark All Merged Notifications Done |
| 2 | +// @name Mark All Merged/Closed Notifications Done |
3 | 3 | // @namespace typpi.online |
4 | | -// @version 1.7 |
5 | | -// @description Marks all merged notifications as "done" on GitHub (client-side) and only shows UI when needed. Includes console logging and error handling. |
| 4 | +// @version 1.8 |
| 5 | +// @description Marks all merged and closed notifications as "done" on GitHub (client-side) and only shows UI when needed. Includes console logging and error handling. |
6 | 6 | // @author Nick2bad4u |
7 | 7 | // @match https://github.com/notifications |
8 | 8 | // @grant GM_addStyle |
|
19 | 19 | // ==/UserScript== |
20 | 20 |
|
21 | 21 | (function() { |
22 | | - 'use strict'; |
23 | | - |
24 | | - const DONE_BUTTON_SELECTOR = 'button[aria-label="Done"]'; |
25 | | - const NOTIFICATION_SELECTOR = '.notifications-list-item'; |
26 | | - const MERGED_ICON_SELECTOR = 'svg.octicon-git-merge'; |
27 | | - const DELAY_MS = 500; // Delay after each action (adjust as needed) |
28 | | - |
29 | | - let markAsDoneButton; // Declare the button outside the function |
30 | | - |
31 | | - function addButton() { |
32 | | - try { |
33 | | - markAsDoneButton = document.createElement('button'); |
34 | | - markAsDoneButton.textContent = 'Mark All Merged Done'; |
35 | | - markAsDoneButton.classList.add('mark-merged-done-button'); |
36 | | - markAsDoneButton.addEventListener('click', markAllMergedAsDone); |
37 | | - markAsDoneButton.style.display = 'none'; // Initially hide the button |
38 | | - |
39 | | - const notificationsToolbar = document.querySelector('.js-socket-channel.js-updatable-content'); |
40 | | - if (notificationsToolbar) { |
41 | | - notificationsToolbar.appendChild(markAsDoneButton); |
42 | | - console.log('Mark All Merged Done button added to notifications toolbar.'); |
43 | | - } else { |
44 | | - console.warn('Could not find notifications toolbar. Button may not be visible.'); |
45 | | - document.body.appendChild(markAsDoneButton); // Fallback |
46 | | - console.log('Mark All Merged Done button added to document body as fallback.'); |
47 | | - } |
48 | | - |
49 | | - // Check for merged notifications and show the button if needed |
50 | | - checkForMergedNotifications(); |
51 | | - } catch (error) { |
52 | | - console.error('Error in addButton function:', error); |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - function checkForMergedNotifications() { |
57 | | - try { |
58 | | - const notifications = document.querySelectorAll(NOTIFICATION_SELECTOR); |
59 | | - let hasMergedNotifications = false; |
60 | | - |
61 | | - for (const notification of notifications) { |
62 | | - if (notification.querySelector(MERGED_ICON_SELECTOR)) { |
63 | | - hasMergedNotifications = true; |
64 | | - break; // No need to continue checking |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - if (hasMergedNotifications) { |
69 | | - markAsDoneButton.style.display = 'block'; // Show the button |
70 | | - console.log('Merged notifications found. Showing Mark All Merged Done button.'); |
71 | | - } else { |
72 | | - markAsDoneButton.style.display = 'none'; // Hide the button |
73 | | - console.log('No merged notifications found. Hiding Mark All Merged Done button.'); |
74 | | - } |
75 | | - } catch (error) { |
76 | | - console.error('Error in checkForMergedNotifications function:', error); |
77 | | - } |
78 | | - } |
79 | | - |
80 | | - async function markAllMergedAsDone() { |
81 | | - try { |
82 | | - const notifications = document.querySelectorAll(NOTIFICATION_SELECTOR); |
83 | | - console.log(`Found ${notifications.length} notifications.`); |
84 | | - |
85 | | - for (const notification of notifications) { |
86 | | - if (notification.querySelector(MERGED_ICON_SELECTOR)) { |
87 | | - console.log('Marking merged notification as done'); |
88 | | - const doneButton = notification.querySelector(DONE_BUTTON_SELECTOR); |
89 | | - |
90 | | - if (doneButton) { |
91 | | - doneButton.click(); |
92 | | - await delay(DELAY_MS); // Wait for the UI to update |
93 | | - // notification.remove(); // Remove the notification after clicking "Done" |
94 | | - } else { |
95 | | - console.warn('Could not find "Done" button for merged notification.'); |
96 | | - } |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - console.log('Finished processing notifications.'); |
101 | | - checkForMergedNotifications(); // Recheck after marking |
102 | | - } catch (error) { |
103 | | - console.error('Error in markAllMergedAsDone function:', error); |
104 | | - } |
105 | | - } |
106 | | - |
107 | | - // Helper function to introduce a delay |
108 | | - function delay(ms) { |
109 | | - return new Promise(resolve => setTimeout(resolve, ms)); |
110 | | - } |
111 | | - |
112 | | - const style = document.createElement('style'); |
113 | | - document.head.appendChild(style); |
114 | | - style.textContent = ` |
115 | | - .mark-merged-done-button { |
116 | | - position: fixed; |
117 | | - bottom: 50px; /* Adjusted bottom position */ |
118 | | - right: 10px; |
119 | | - z-index: 999; /* Ensure it's below the other button if necessary */ |
120 | | - background-color: #2ea44f; /* Same color as the other script */ |
121 | | - color: #ffffff; |
122 | | - border: none; |
123 | | - padding: 10px; |
124 | | - border-radius: 5px; |
125 | | - cursor: pointer; |
126 | | - } |
127 | | - .mark-merged-done-button:hover { |
128 | | - background-color: #79e4f2; /* Same hover color as the other script */ |
129 | | - } |
130 | | - `; |
131 | | - |
132 | | - window.addEventListener('load', addButton); |
| 22 | + 'use strict'; |
| 23 | + |
| 24 | + const DONE_BUTTON_SELECTOR = 'button[aria-label="Done"]'; |
| 25 | + const NOTIFICATION_SELECTOR = '.notifications-list-item'; |
| 26 | + const MERGED_ICON_SELECTOR = 'svg.octicon-git-merge'; |
| 27 | + const CLOSED_ICON_SELECTOR = 'svg.octicon-git-pull-request-closed'; |
| 28 | + const DELAY_MS = 500; // Delay after each action (adjust as needed) |
| 29 | + |
| 30 | + let markAsDoneButton; // Declare the button outside the function |
| 31 | + |
| 32 | + function addButton() { |
| 33 | + try { |
| 34 | + markAsDoneButton = document.createElement('button'); |
| 35 | + markAsDoneButton.textContent = 'Mark All Merged/Closed Done'; |
| 36 | + markAsDoneButton.classList.add('mark-merged-done-button'); |
| 37 | + markAsDoneButton.addEventListener('click', markAllMergedAndClosedAsDone); |
| 38 | + markAsDoneButton.style.display = 'none'; // Initially hide the button |
| 39 | + |
| 40 | + const notificationsToolbar = document.querySelector('.js-socket-channel.js-updatable-content'); |
| 41 | + if (notificationsToolbar) { |
| 42 | + notificationsToolbar.appendChild(markAsDoneButton); |
| 43 | + console.log('Mark All Merged/Closed Done button added to notifications toolbar.'); |
| 44 | + } else { |
| 45 | + console.warn('Could not find notifications toolbar. Button may not be visible.'); |
| 46 | + document.body.appendChild(markAsDoneButton); // Fallback |
| 47 | + console.log('Mark All Merged/Closed Done button added to document body as fallback.'); |
| 48 | + } |
| 49 | + |
| 50 | + // Check for relevant notifications and show the button if needed |
| 51 | + checkForMergedAndClosedNotifications(); |
| 52 | + } catch (error) { |
| 53 | + console.error('Error in addButton function:', error); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + function checkForMergedAndClosedNotifications() { |
| 58 | + try { |
| 59 | + const notifications = document.querySelectorAll(NOTIFICATION_SELECTOR); |
| 60 | + let hasRelevantNotifications = false; |
| 61 | + |
| 62 | + for (const notification of notifications) { |
| 63 | + if (notification.querySelector(MERGED_ICON_SELECTOR) || notification.querySelector(CLOSED_ICON_SELECTOR)) { |
| 64 | + hasRelevantNotifications = true; |
| 65 | + break; // No need to continue checking |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (hasRelevantNotifications) { |
| 70 | + markAsDoneButton.style.display = 'block'; // Show the button |
| 71 | + console.log('Relevant notifications found. Showing Mark All Merged/Closed Done button.'); |
| 72 | + } else { |
| 73 | + markAsDoneButton.style.display = 'none'; // Hide the button |
| 74 | + console.log('No relevant notifications found. Hiding Mark All Merged/Closed Done button.'); |
| 75 | + } |
| 76 | + } catch (error) { |
| 77 | + console.error('Error in checkForMergedAndClosedNotifications function:', error); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + async function markAllMergedAndClosedAsDone() { |
| 82 | + try { |
| 83 | + const notifications = document.querySelectorAll(NOTIFICATION_SELECTOR); |
| 84 | + console.log(`Found ${notifications.length} notifications.`); |
| 85 | + |
| 86 | + for (const notification of notifications) { |
| 87 | + const isMerged = notification.querySelector(MERGED_ICON_SELECTOR); |
| 88 | + const isClosed = notification.querySelector(CLOSED_ICON_SELECTOR); |
| 89 | + |
| 90 | + if (isMerged || isClosed) { |
| 91 | + console.log(`Marking ${isMerged ? 'merged' : 'closed'} notification as done`); |
| 92 | + const doneButton = notification.querySelector(DONE_BUTTON_SELECTOR); |
| 93 | + |
| 94 | + if (doneButton) { |
| 95 | + doneButton.click(); |
| 96 | + await delay(DELAY_MS); // Wait for the UI to update |
| 97 | + } else { |
| 98 | + console.warn('Could not find "Done" button for notification.'); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + console.log('Finished processing notifications.'); |
| 104 | + checkForMergedAndClosedNotifications(); // Recheck after marking |
| 105 | + } catch (error) { |
| 106 | + console.error('Error in markAllMergedAndClosedAsDone function:', error); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Helper function to introduce a delay |
| 111 | + function delay(ms) { |
| 112 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 113 | + } |
| 114 | + |
| 115 | + const style = document.createElement('style'); |
| 116 | + document.head.appendChild(style); |
| 117 | + style.textContent = ` |
| 118 | + .mark-merged-done-button { |
| 119 | + position: fixed; |
| 120 | + bottom: 50px; /* Adjusted bottom position */ |
| 121 | + right: 10px; |
| 122 | + z-index: 999; /* Ensure it's below the other button if necessary */ |
| 123 | + background-color: #2ea44f; /* Same color as the other script */ |
| 124 | + color: #ffffff; |
| 125 | + border: none; |
| 126 | + padding: 10px; |
| 127 | + border-radius: 5px; |
| 128 | + cursor: pointer; |
| 129 | + } |
| 130 | + .mark-merged-done-button:hover { |
| 131 | + background-color: #79e4f2; /* Same hover color as the other script */ |
| 132 | + } |
| 133 | + `; |
| 134 | + |
| 135 | + window.addEventListener('load', addButton); |
133 | 136 | })(); |
0 commit comments