Twitter - add unread notifications count in the tab title

Adds unread notifications count in the tab title

当前为 2016-11-27 提交的版本,查看 最新版本

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