GitHub Notifier

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

当前为 2020-06-25 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name GitHub Notifier
// @namespace https://rafaelgssa.gitlab.io/monkey-scripts
// @version 4.1.2
// @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/405802-monkey-dom/code/Monkey%20DOM.js?version=820314
// @require https://greasyfork.org/scripts/405813-monkey-utils/code/Monkey%20Utils.js?version=820304
// @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 */

(async () => {
	'use strict';

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

	let doShowBrowserNotifications = false;
	let hasChecked = false;
	let hasUnread = false;

	/**
	 * Loads the script.
	 */
	const load = () => {
		const indicator = document.querySelector('.mail-status');
		if (!indicator) {
			// User is not logged in.
			return;
		}
		_lockBrowserNotifications();
		_check(indicator);
		DOM.observeNode(indicator, { attributes: true }, /** @type {NodeCallback} */ (_check));
	};

	/**
	 * Locks browser notifications so that only one tab shows them.
	 */
	const _lockBrowserNotifications = () => {
		SJ.lock(`${scriptId}_lock_browserNotifications`, () => {
			console.log(`[${scriptName}] Locked browser notifications!`);
			doShowBrowserNotifications = true;
		});
	};

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

	/**
	 * Notifies the user.
	 */
	const _notifyUser = () => {
		if (hasUnread) {
			if (!document.title.startsWith('(New) ')) {
				document.title = `(New) ${document.title}`;
			}
		} else if (document.title.startsWith('(New) ')) {
			document.title = document.title.slice(6);
		}
		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);
	}
})();