GitHub Notifier

Notifies the user whenever they have new unread notifications on GitHub.

Από την 06/07/2020. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name GitHub Notifier
// @namespace https://rafaelgssa.gitlab.io/monkey-scripts
// @version 5.0.0
// @author rafaelgssa
// @description Notifies the user whenever they have new unread notifications on GitHub.
// @match https://github.com/*
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// @require https://greasyfork.org/scripts/405813-monkey-utils/code/Monkey%20Utils.js?version=821710
// @require https://greasyfork.org/scripts/405802-monkey-dom/code/Monkey%20DOM.js?version=821769
// @require https://greasyfork.org/scripts/405935-iwc/code/IWC.js?version=819599
// @run-at document-end
// @grant GM.info
// @grant GM_info
// @noframes
// ==/UserScript==

/* global DOM, SJ */

(() => {
	'use strict';

	const scriptId = 'ghn';
	const scriptName = GM.info.script.name;

	const unreadIndicator = '(New) ';

	let doShowBrowserNotifications = false;

	let hasChecked = false;

	let hasUnread = false;

	/**
	 * Loads the script.
	 */
	const load = () => {
		const indicatorEl = document.querySelector('.mail-status');
		if (!indicatorEl) {
			// User is not logged in.
			return;
		}
		SJ.lock(`${scriptId}_lock_browserNotifications`, onBrowserNotificationsLock); // Locks browser notifications so that only one tab shows them.
		check(indicatorEl);
		DOM.observeNode(indicatorEl, { attributes: true }, /** @type {NodeCallback} */ (check));
	};

	/**
	 * Triggered when browser notifications are locked.
	 */
	const onBrowserNotificationsLock = () => {
		console.log(`[${scriptName}] Locked browser notifications!`);
		doShowBrowserNotifications = true;
	};

	/**
	 * Checks for unread notifications and notifies the user.
	 * @param {Element} indicatorEl The element that indicates the notification status.
	 */
	const check = (indicatorEl) => {
		const newValue = indicatorEl.classList.contains('unread');
		if (hasUnread !== newValue) {
			hasUnread = newValue;
			notifyUser();
		}
		hasChecked = true;
	};

	/**
	 * Notifies the user.
	 */
	const notifyUser = () => {
		if (hasUnread) {
			if (!document.title.startsWith(unreadIndicator)) {
				document.title = `${unreadIndicator}${document.title}`;
			}
		} else if (document.title.startsWith(unreadIndicator)) {
			document.title = document.title.slice(unreadIndicator.length);
		}
		if (doShowBrowserNotifications && hasChecked && hasUnread && document.hidden) {
			// Only show a browser notification for subsequent unread notifications if the user is away from the tab.
			showBrowserNotification('You have new unread notifications.');
		}
	};

	/**
	 * Shows a browser notification.
	 * @param {string} body The message to show.
	 * @return {Promise<void>}
	 */
	const showBrowserNotification = async (body) => {
		if (Notification.permission !== 'granted') {
			await Notification.requestPermission();
		}
		if (Notification.permission === 'granted') {
			new Notification(scriptName, { body });
		}
	};

	try {
		load();
	} catch (err) {
		console.log(`Failed to load ${scriptName}: `, err);
	}
})();