Twitter - adds unread notifications count in the tab title

Adds unread notifications count in the tab title

当前为 2016-08-06 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Twitter - adds unread notifications count in the tab title
// @author      darkred
// @description Adds unread notifications count in the tab title
// @include     https://twitter.com/*
// @version     3
// @grant       none
// @require     https://greasyfork.org/scripts/21927-arrive-js/code/arrivejs.js?version=139586
// @namespace rikkie
// ==/UserScript==


var counter;


function addCounterInTitle(){
	counter = parseInt(document.querySelector('.count-inner').innerHTML);
	if (counter > 0 && document.title.indexOf('|') === -1) {
		document.title = counter + ' | ' + document.title;
	} else if (counter === 0) {
		document.title = /[0-9]*\\|(.*)/g.exec(document.title)[1];
	}
}


document.arrive('div.js-account-summary:nth-child(1) > div:nth-child(2) > a:nth-child(1) > img:nth-child(1)', function() {		// 'the 1st avatar thumbnail in the "Who to follow" panel'
	addCounterInTitle();
});




document.arrive('.new-tweets-bar', function(){					// Whenever there are new unread tweets in the timeline
	// alert('aLLAKSE');
	var target = document.querySelector('.new-tweets-bar');

	var observer = new MutationObserver(function (mutations) {
		mutations.forEach(function (mutation) {					// Disconnect the observer on the 1st mutation
			observer.disconnect();
		});
	});

	addCounterInTitle();

	var config = {
		attributes: true,
		// childList: true,
		// characterData: true,
		// subtree: true
		// attributeOldValue: true,
		attributeFilter: ['data-item-count'],		// this is required in order to count only mutations of the unread posts number		--> document.querySelector('.new-tweets-bar').getAttribute('data-item-count')
	};

	observer.observe(target, config);

});




document.leave('.new-tweets-bar', function(){
	addCounterInTitle();
});





// Reset the counter when viewing Notifications tab
document.arrive('#timeline', function(){
	document.querySelector('.count-inner').innerHTML = 0;
	document.title = /[0-9]*\\|(.*)/g.exec(document.title)[1];
});








// When there's change in the Notifications counter
// var target2 = document.querySelector('.count-inner');
var target2 = document.querySelector('.count');

var observer2 = new MutationObserver(function (mutations) {
	mutations.forEach(function (mutation) {
		// alert('egine');
		observer2.disconnect();
	});
});

addCounterInTitle();

var config2 = {
	attributes: true,
	//  characterData: true,
};

observer2.observe(target2, config2);