Twitter Favicon No Red Notifcation Dot

Don't let Twitter put a red dot on its Favicon just because you got a little notification

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Twitter Favicon No Red Notifcation Dot
// @namespace    https://greasyfork.org/en/users/8615-joeytwiddle
// @version      0.1
// @license      ISC
// @description  Don't let Twitter put a red dot on its Favicon just because you got a little notification
// @author       joeytwiddle
// @match        https://twitter.com/*
// @grant        none
// ==/UserScript==

// TODO: Option to allow the red dot IFF the user has pending messages (but not if they have pending notifications)

(function () {
	'use strict';

	const debugLogging = false;

	const head = document.getElementsByTagName('head')[0];

	function checkTheFavicon() {
		debugLogging && console.log('Checking favicon...');

		const currentIcons = head.querySelectorAll('[rel="shortcut icon"]');
		debugLogging && Array.from(currentIcons).forEach(icon => console.log('icon:', icon.href));

		const currentIcon = currentIcons[currentIcons.length - 1];
		if (!currentIcon) {
			console.warn('Twitter_Favicon_No_Red_Notification_Dot: No currentIcon to check');
			return;
		}

		if (currentIcon.href.includes('twitter-pip')) {
			debugLogging && console.log('Unwanted pip icon detected.  Exterminate!');
			setFavicon(currentIcon, false);
		}
	}

	function setFavicon(currentIcon, withPip) {
		// Create an entirely new element
		/*
		const newIcon = document.createElement('link');
		newIcon.setAttribute('rel', 'shortcut icon');
		//newIcon.setAttribute('type', 'image/x-icon');
		//newIcon.setAttribute('href', 'data:image/x-icon;base64,______');
		newIcon.setAttribute('href', 'https://abs.twimg.com/favicons/twitter.ico');

		for (let i = currentIcons.length; i--;) {
			currentIcons[i].parentNode.removeChild(currentIcons[i]);
		}
		head.appendChild(newIcon);
		*/

		// Reuse the existing element
		currentIcon.setAttribute('href', 'https://abs.twimg.com/favicons/twitter.ico');
	}

	checkTheFavicon();
	new MutationObserver(mutations => checkTheFavicon()).observe(head, { childList: true, subtree: true });
})();