Twitter - adds unread notifications count in the tab title

Adds unread notifications count in the tab title

当前为 2015-09-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter - adds unread notifications count in the tab title
  3. // @author darkred
  4. // @description Adds unread notifications count in the tab title
  5. // @include https://twitter.com/
  6. // @version 2
  7. // @grant none
  8. //
  9. // Thanks a lot to wOxxOm for his valuable help
  10. // @namespace rikkie
  11. // ==/UserScript==
  12.  
  13. /// ---------------------------------
  14. // Initial display of counter
  15. /// ---------------------------------
  16. var nCount = document.querySelector('.count > span:nth-child(1)').innerHTML;
  17. if (nCount != '0') {
  18. document.title = nCount + '|' + document.title;
  19. document.querySelector('.count > span:nth-child(1)').unread = true;
  20. };
  21.  
  22. /// ---------------------------------
  23. /// 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)
  24. /// ---------------------------------
  25. var target1 = document.querySelector('head > title');
  26.  
  27. var observer1 = new MutationObserver(function (mutations) {
  28. mutations.forEach(function (mutation) {
  29. nCount = document.querySelector('.count > span:nth-child(1)').innerHTML;
  30. if (document.title.indexOf('|') == -1
  31. && nCount != '0'
  32. && document.querySelector('.count > span:nth-child(1)').unread) {
  33. document.title = nCount + '|' + document.title;
  34. }
  35. });
  36. })
  37.  
  38. var config = { attributes: true, childList: true, characterData: true }
  39.  
  40. observer1.observe(target1, config);
  41.  
  42.  
  43. /// ---------------------------------
  44. /// 2st mutation observer -monitors the HTML notification counter value-
  45. /// (on every value change, add a class Attribute with the name "unread")
  46. /// ---------------------------------
  47. var target2 = document.querySelector('.count > span:nth-child(1)');
  48.  
  49. var observer2 = new MutationObserver(function (mutations) {
  50. mutations.forEach(function (mutation) {
  51. if (!target2.unread)
  52. target2.unread = true;
  53. });
  54. })
  55.  
  56. // var config = { attributes: true, childList: true, characterData: true }
  57.  
  58. observer2.observe(target2, config);
  59.  
  60.  
  61. /// ---------------------------------
  62. /// Three "click" event listeners, attached on the "Notifications" button (one for each of it's 3 parts)
  63. /// (if the clicked element has the "unread" attribute, then clear the attribute and decrement the unread count-make it 0 again)
  64. /// ---------------------------------
  65. var target3a = document.querySelector('.Icon--notifications');
  66. var target3b = document.querySelector('.people > a:nth-child(1) > span:nth-child(2)');
  67. var target3c = document.querySelector('.people > a:nth-child(1)');
  68. target3a.addEventListener("click", decrementCounter, false);
  69. target3b.addEventListener("click", decrementCounter, false);
  70. target3c.addEventListener("click", decrementCounter, false);
  71.  
  72. function decrementCounter(){
  73. counter = document.querySelector('.count > span:nth-child(1)');
  74. if (counter.unread)
  75. delete counter.unread;
  76. document.querySelector('.count > span:nth-child(1)').innerHTML = '0';
  77. document.title = "Twitter";
  78. }