Twitter - add unread notifications count in the tab title

Adds unread notifications count in the tab title

当前为 2016-10-22 提交的版本,查看 最新版本

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