Skip to content

Instantly share code, notes, and snippets.

@simonwep
Last active January 15, 2020 15:59
Show Gist options
  • Save simonwep/19a271c47f1ca1fed35ecea46aba8f89 to your computer and use it in GitHub Desktop.
Save simonwep/19a271c47f1ca1fed35ecea46aba8f89 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name GitHub notification-count
// @version 1.0
// @namespace http://tampermonkey.net/
// @description Shows the amount of notifications
// @match https://*.github.com/*
// @grant none
// @run-at document-start
// ==/UserScript==
!async function () {
async function queryAsync(query) {
return new Promise(resolve => {
const interval = setInterval(() => {
const element = document.querySelector(query);
if (element) {
clearInterval(interval);
return resolve(element);
}
}, 1000);
});
}
queryAsync('.logged-in').then(() => {
return fetch('https://github.com/notifications', {
credentials: 'include'
});
}).then(v => v.text()).then(body => {
const dom = (new DOMParser()).parseFromString(body, 'text/html');
return dom.querySelector('.filter-item .count').innerHTML;
}).then(async count => {
if (!Number(count)) {
return;
}
// Update notification knob
const notificationIcon = await queryAsync('.mail-status.unread');
notificationIcon.innerHTML = count;
Object.assign(notificationIcon.style, {
width: 'auto',
height: 'auto',
fontSize: '9px',
fontWeight: '700',
padding: '0.1em 0.45em',
display: 'inline-block',
borderRadius: '100em'
});
// Update title
const [, , raw] = document.title.match(/^(\([\d ]+\)|)(.*)/);
document.title = `(${count}) ${raw}`;
});
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment