Facebook YOUR Notifications Highlighter

Highlights the more interesting notifications for YOU.

当前为 2017-03-27 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Facebook YOUR Notifications Highlighter
  3. // @namespace http://www.JamesKoss.com/
  4. // @version 1.3.4
  5. // @description Highlights the more interesting notifications for YOU.
  6. // @author James Koss
  7. // @match https://www.facebook.com/*
  8. // @supportURL https://greasyfork.org/en/scripts/27189-facebook-your-notifications-highlighter/feedback
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. var first = true;
  14. var timed = false;
  15. var scrolled = false;
  16. // Update notifications on scrolling.
  17. // Not too often. Arbitrary delay.
  18. function onScrollNots() {
  19. if (scrolled === false) {
  20. scrolled = true;
  21. updateNotifications();
  22. // Delay until next check from scroll. Arbitrary delay.
  23. setTimeout(function() { scrolled = false; }, 1000);
  24. }
  25. }
  26. // After clicking the notification, remove highlight color and eventListener.
  27. function removeHighlight(e) {
  28. e.currentTarget.removeEventListener("click", removeHighlight, false);
  29. e.currentTarget.style.backgroundColor = "";
  30. if (e.currentTarget.paramA !== null) {
  31. e.currentTarget.paramA.style.backgroundColor = "";
  32. }
  33. }
  34. // Update relevant notifications with highlight.
  35. function updateNotifications() {
  36. // Delay before element updates, so they load first. Arbitrary delay!
  37. if (timed === false) {
  38. setTimeout(function(){ timed = true; updateNotifications(); }, 1000);
  39. return;
  40. }
  41. // On first viewing, add updating when scrolling the notifications.
  42. if (first === true) {
  43. first = false;
  44. var scrolledArea = document.querySelector('div[class="uiScrollableAreaWrap scrollable"]');
  45. scrolledArea.addEventListener("scroll", onScrollNots);
  46. }
  47. var notificationsHolder = document.querySelector('div[class="_33p"]');
  48. // Only check new notifications.
  49. var notificationsNew = notificationsHolder.querySelectorAll('li[class="_33c jewelItemNew"]');
  50.  
  51. for (var i = 0; i < notificationsNew.length; i++) {
  52. var current = notificationsNew[i];
  53. var notificationParent = current.querySelector('div[class="_4l_v"]');
  54. var notificationYour = notificationParent.querySelectorAll('span');
  55. // match 1 for interest highlight, 2 for "Like" highlight.
  56. var match = false;
  57. for (var j=0; j < notificationYour.length; j++) {
  58. var cur = notificationYour[j];
  59. var t = cur.textContent;
  60. // Relevant text inside notification element.
  61. if (t.indexOf("replied to your") !== -1 ||
  62. t.indexOf("commented on your") !== -1 ||
  63. t.indexOf("shared your") !== -1 ||
  64. t.indexOf("mentioned you") !== -1 ||
  65. t.indexOf("tagged you") !== -1 ||
  66. t.indexOf("you're tagged in") !== -1 ||
  67. t.indexOf("made you") !== -1 ||
  68. t.indexOf("also replied") !== -1 ||
  69. (t.indexOf("replied to") !== -1 && t.indexOf("on your") !== -1)) {
  70. match = 1;
  71. break;
  72. } else if (t.indexOf("likes your") !== -1 ||
  73. t.indexOf("like your") !== -1 ||
  74. t.indexOf("liked your") !== -1) {
  75. match = 2;
  76. break;
  77. } else if (t.indexOf("approved your") !== -1 ||
  78. t.indexOf("changed the name of the") !== -1 ||
  79. t.indexOf("changed the type of the") !== -1) {
  80. match = 3;
  81. break;
  82. } else if (t.indexOf("needs review") !== -1 ||
  83. t.indexOf("your Timeline") !== -1 ||
  84. t.indexOf("flagged as possible spam") !== -1) {
  85. match = 4;
  86. break;
  87. }
  88. }
  89. // No match.
  90. if (match === false) {
  91. continue;
  92. }
  93. // Select color by matching value.
  94. var color;
  95. switch(match) {
  96. case 1:
  97. color = "#c3d4ef"; // darker blue
  98. break;
  99. case 2:
  100. color = "#faedf8"; // light pink
  101. break;
  102. case 3:
  103. color = "#d7f4d7"; // light green
  104. break;
  105. case 4:
  106. color = "#dec3ef"; // darker purple
  107. break;
  108. }
  109. // Update the li & a elements backgrounds.
  110. var a = current.querySelector('a[class="_33e _1_0e"]');
  111. if (a !== null) {
  112. a.style.backgroundColor = color;
  113. }
  114. current.style.backgroundColor = color;
  115. current.paramA = a; // Pass to object, for highlight removal after clicked.
  116. current.addEventListener("click", removeHighlight, false);
  117. }
  118. timed = false;
  119. }
  120. // Right-Clicking the notifications button on FB.
  121. function onclickJewel(e) {
  122. if (e.which === 1) {
  123. updateNotifications();
  124. }
  125. }
  126. // Listen to a click on the notifications button on FB.
  127. var fbNotificationsJewel = document.getElementById("fbNotificationsJewel");
  128. fbNotificationsJewel.addEventListener("click", onclickJewel);
  129. })();