Last active
November 18, 2019 23:13
-
-
Save fallaciousreasoning/d134c76eb8b72b7688f1ae92a3caf8d9 to your computer and use it in GitHub Desktop.
A ViolentMonkey/TamperMonkey/GreaseMonkey script for setting unread notification badges for Github.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name GitHub Notification Badges | |
// @namespace Violentmonkey Scripts | |
// @match https://github.com/* | |
// @grant none | |
// ==/UserScript== | |
// | |
const getNotificationsCount = async () => { | |
const response = await fetch('https://github.com/notifications'); | |
const text = await response.text(); | |
const doc = document.createElement('html'); | |
doc.innerHTML = text; | |
const notifications = doc.querySelectorAll('.js-notification.unread'); | |
return notifications.length; | |
} | |
const hasUnread = () => { | |
return !!document.querySelector('.mail-status.unread'); | |
} | |
const updateBadge = async () => { | |
const Badge = navigator.setAppBadge || navigator.setExperimentalAppBadge; | |
if (!Badge) | |
return; | |
const notificationCount = await getNotificationsCount(); | |
Badge.call(navigator, notificationCount); | |
} | |
const poll = async (promise, regularity) => { | |
await promise(); | |
setTimeout(() => { | |
poll(promise, regularity); | |
}, regularity) | |
}; | |
poll(updateBadge, 5000); | |
getNotificationsCount(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment