Twitter - add unread notifications count in the tab title

Adds unread notifications count in the tab title

当前为 2018-05-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter - add unread notifications count in the tab title
  3. // @namespace darkred
  4. // @version 2018.2.27
  5. // @description Adds unread notifications count in the tab title
  6. // @author darkred
  7. // @license MIT
  8. // @include https://twitter.com/*
  9. // @grant none
  10. // @require https://greasyfork.org/scripts/21927-arrive-js/code/arrivejs.js
  11. // ==/UserScript==
  12.  
  13.  
  14.  
  15. // .count > .count-inner
  16. // Notifications counter
  17.  
  18. // .dm-new > .count-inner
  19. // DM counter
  20.  
  21.  
  22. var notificationsCounter;
  23.  
  24. function addCounterInTitle() {
  25. // alert(0);
  26. // counter = parseInt(document.querySelector('.count-inner').innerHTML); // the Notifications counter value
  27. notificationsCounter = parseInt(document.querySelector('.count > .count-inner').innerHTML); // the Notifications counter value
  28. // if (counter > 0 && document.title.indexOf('|') > 3) { // if the '|' symbol is the default separator of username and 'Twitter' when viewing profiles, e.g.: Twitter Support (@Support) | Twitter. In here the position of `|` is 27.
  29. if (notificationsCounter > 0) { // if the '|' symbol is the default separator of username and 'Twitter' when viewing profiles, e.g.: Twitter Support (@Support) | Twitter. In here the position of `|` is 27.
  30. if (/[0-9]+\ \|\ .*/.test(document.title)){ // if our counter is already added to title
  31. var defaultTitle = document.title.match(/[0-9]+\ \|\ (.*)/)[1];
  32. document.title = notificationsCounter + ' | ' + defaultTitle;
  33. return;
  34. } else {
  35. document.title = notificationsCounter + ' | ' + document.title; // add the counter to the title
  36. return;
  37. }
  38. } else if (notificationsCounter === 0) {
  39. document.title = /[0-9]+\ \|\ (.*)/g.exec(document.title)[1]; // remove title's added counter
  40. }
  41. }
  42.  
  43.  
  44.  
  45. // After the 'Notifications' counter is first visible in the page (= the selector below is for the element: 'the 1st avatar thumbnail in the "Who to follow" panel')
  46. document.arrive('div.js-account-summary:nth-child(1) > div:nth-child(2) > a:nth-child(1) > img:nth-child(1)', function () {
  47. addCounterInTitle();
  48. });
  49.  
  50.  
  51.  
  52. // Whenever there are new unread tweets in the timeline..
  53. document.arrive('.new-tweets-bar', function () {
  54. var target = document.querySelector('.new-tweets-bar'); // ..οbserve the unread counter for changes(increase)
  55. var observer = new MutationObserver(function (mutations) {
  56. addCounterInTitle(); // Refresh the counter on every such change
  57. });
  58. var config = {
  59. childList: true,
  60. };
  61. observer.observe(target, config);
  62. });
  63.  
  64.  
  65.  
  66. // Refresh the counter when there are no unread tweets
  67. document.leave('.new-tweets-bar', function () {
  68. addCounterInTitle();
  69. });
  70.  
  71.  
  72.  
  73. // Whenever viewing the 'Notifications' tab
  74. document.arrive('.NotificationsHeadingContent', function () {
  75. // document.querySelector('.count-inner').innerHTML = 0; // ..reset the counter..
  76. document.querySelector('.count > .count-inner').innerHTML = 0; // ..reset the counter..
  77. notificationsCounter = 0;
  78. document.title = document.title.match(/[0-9]+\ \|\ (.*)/)[1]; // ..and the tab title
  79. });
  80.  
  81.  
  82.  
  83. // Observe the 'Notifications' counter for changes
  84. var target2 = document.querySelector('.count-inner');
  85. var observer2 = new MutationObserver(function (mutations) {
  86. addCounterInTitle();
  87. });
  88. var config2 = {
  89. childList: true,
  90. };
  91. observer2.observe(target2, config2);
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. function resetCounter(){
  99. // document.querySelector('.new-count').className = 'count';
  100. notificationsCounter = 0;
  101. document.querySelector('.count-inner').innerHTML = '';
  102. document.title = /[0-9]+\ \|\ (.*)/g.exec(document.title)[1];
  103. }
  104.  
  105. /// A "click" event listener attached on the "Notifications" button:
  106. // if the user clicks, rightclicks or middle-clicks the button, then reset the counter and the tab title.
  107. var target3 = document.querySelector('.people');
  108. target3.addEventListener('mousedown', resetCounter, false);