Twitter - add unread notifications count in the tab title

Adds unread notifications count in the tab title

当前为 2016-09-17 提交的版本,查看 最新版本

  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.08.20
  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. counter = parseInt(document.querySelector('.count-inner').innerHTML);
  16. if (counter > 0 && document.title.indexOf('|') === - 1) {
  17. document.title = counter + ' | ' + document.title;
  18. } else if (counter === 0) {
  19. document.title = /[0-9]*\\|(.*)/g.exec(document.title) [1];
  20. }
  21. }
  22.  
  23.  
  24.  
  25. // 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')
  26. document.arrive('div.js-account-summary:nth-child(1) > div:nth-child(2) > a:nth-child(1) > img:nth-child(1)', function () {
  27. addCounterInTitle();
  28. });
  29.  
  30.  
  31.  
  32. // Whenever there are new unread tweets in the timeline..
  33. document.arrive('.new-tweets-bar', function () {
  34. var target = document.querySelector('.new-tweets-bar'); // ..οbserve the unread counter for changes(increase)
  35. var observer = new MutationObserver(function (mutations) {
  36. mutations.forEach(function (mutation) {
  37. addCounterInTitle(); // Refresh the counter on every such change
  38. });
  39. });
  40. var config = {
  41. childList: true,
  42. };
  43. observer.observe(target, config);
  44. });
  45.  
  46.  
  47.  
  48. // Refresh the counter when there are no unread tweets
  49. document.leave('.new-tweets-bar', function () {
  50. addCounterInTitle();
  51. });
  52.  
  53.  
  54.  
  55. // Whenever viewing the 'Notifications' tab
  56. document.arrive('.NotificationsHeadingContent', function () {
  57. document.querySelector('.count-inner').innerHTML = 0; // ..reset the counter..
  58. document.title = document.title.match(/[0-9]*\ \|\ (.*)/)[1]; // ..and the tab title
  59. });
  60.  
  61.  
  62.  
  63. // Observe the 'Notifications' counter for changes
  64. var target2 = document.querySelector('.count-inner');
  65. var observer2 = new MutationObserver(function (mutations) {
  66. mutations.forEach(function (mutation) {
  67. addCounterInTitle();
  68. });
  69. });
  70. var config2 = {
  71. childList: true,
  72. };
  73. observer2.observe(target2, config2);