Twitter - add unread notifications count in the tab title

Adds unread notifications count in the tab title

当前为 2016-08-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter - add 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 2016.08.07
  7. // @grant none
  8. // @require https://code.jquery.com/jquery-3.0.0.min.js
  9. // @require https://greasyfork.org/scripts/21927-arrive-js/code/arrivejs.js?version=139586
  10. // @namespace rikkie
  11. // ==/UserScript==
  12.  
  13. var counter;
  14.  
  15. function addCounterInTitle() {
  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(); // ... and 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 ('the '.toggle-item-4' is the selector for the 'People you follow' link in the 'Notifications' tab)...
  57. document.arrive('.toggle-item-4', function () {
  58. document.querySelector('.count-inner').innerHTML = 0; // ... reset the counter
  59. document.title = /[0-9]*\\|(.*)/g.exec(document.title) [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 target2 = document.querySelector('.count');
  67. var observer2 = new MutationObserver(function (mutations) {
  68. mutations.forEach(function (mutation) {
  69. addCounterInTitle();
  70. });
  71. });
  72. var config2 = {
  73. childList: true,
  74. };
  75. observer2.observe(target2, config2);