Twitter - adds unread notifications count in the tab title

Adds unread notifications count in the tab title

目前為 2015-04-18 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Twitter - adds unread notifications count in the tab title
// @namespace   rikkie
// @description Adds unread notifications count in the tab title
// @include     https://twitter.com/
// @version     2
// @grant       none
//
// Thanks a lot to wOxxOm for his valuable help 
// ==/UserScript==

/// ---------------------------------
// Initial display of counter
/// ---------------------------------
var nCount = document.querySelector('.count > span:nth-child(1)').innerHTML;
if (nCount != '0') {
  document.title = nCount + '|' + document.title;
  document.querySelector('.count > span:nth-child(1)').unread = true;
};

/// ---------------------------------
/// 1st mutation observer -monitors the tab title- (if tab title doesn't contain '|' and nCount not 0 and unread flag is true, then display counter)
/// ---------------------------------
var target1 = document.querySelector('head > title');

var observer1 = new MutationObserver(function (mutations) {
  mutations.forEach(function (mutation) {
    nCount = document.querySelector('.count > span:nth-child(1)').innerHTML;
    if (document.title.indexOf('|') == -1 
        && nCount != '0'
        && document.querySelector('.count > span:nth-child(1)').unread) {
      document.title = nCount + '|' + document.title;
    }
  });
})

var config = { attributes: true, childList: true, characterData: true }

observer1.observe(target1, config);


/// ---------------------------------
/// 2st mutation observer -monitors the HTML notification counter value- 
/// (on every value change, add a class Attribute with the name "unread")
/// ---------------------------------
var target2 = document.querySelector('.count > span:nth-child(1)');

var observer2 = new MutationObserver(function (mutations) {
  mutations.forEach(function (mutation) {
    if (!target2.unread)
      target2.unread = true;      
  });
})

// var config = { attributes: true, childList: true, characterData: true }

observer2.observe(target2, config);


/// ---------------------------------
/// Three "click" event listeners, attached on the "Notifications" button (one for each of it's 3 parts)
/// (if the clicked element has the "unread" attribute, then clear the attribute and decrement the unread count-make it 0 again)
/// ---------------------------------
var target3a = document.querySelector('.Icon--notifications');
var target3b = document.querySelector('.people > a:nth-child(1) > span:nth-child(2)');
var target3c = document.querySelector('.people > a:nth-child(1)');
target3a.addEventListener("click", decrementCounter, false);
target3b.addEventListener("click", decrementCounter, false);
target3c.addEventListener("click", decrementCounter, false);

function decrementCounter(){
  counter = document.querySelector('.count > span:nth-child(1)');
  if (counter.unread)    
     delete counter.unread; 
  document.querySelector('.count > span:nth-child(1)').innerHTML = '0';
  document.title = "Twitter";    
}