Skip to content

Instantly share code, notes, and snippets.

@fallaciousreasoning
Last active November 18, 2019 23:13
Show Gist options
  • Save fallaciousreasoning/d134c76eb8b72b7688f1ae92a3caf8d9 to your computer and use it in GitHub Desktop.
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
// ==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