Facebook YOUR Notifications Highlighter

Highlights more interesting notifications for YOU.

当前为 2017-02-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Facebook YOUR Notifications Highlighter
  3. // @namespace http://www.JamesKoss.com/
  4. // @version 1.2
  5. // @description Highlights more interesting notifications for YOU.
  6. // @author James Koss
  7. // @match https://www.facebook.com/*
  8. // ==/UserScript==
  9.  
  10. (function() {
  11. 'use strict';
  12. var first = true;
  13. var timed = false;
  14. var opened = 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; }, 3000);
  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. e.currentTarget.paramA.style.backgroundColor = "";
  31. }
  32. // Update relevant notifications with highlight.
  33. function updateNotifications() {
  34. // Delay before element updates, so they load first. Arbitrary delay!
  35. if (timed === false) {
  36. setTimeout(function(){ timed = true; updateNotifications(); }, 1000);
  37. return;
  38. }
  39. // Ignore this call, if just clicked to close notifications view.
  40. if (opened === true && scrolled === false) {
  41. opened = false;
  42. return;
  43. }
  44. opened = true;
  45. // On first viewing, add updating when scrolling the notifications.
  46. if (first === true) {
  47. first = false;
  48. var scrolledArea = document.querySelector('div[class="uiScrollableAreaWrap scrollable"]');
  49. scrolledArea.addEventListener("scroll", onScrollNots);
  50. }
  51. var notificationsHolder = document.querySelector('div[class="_33p"]');
  52. // Only check new notifications.
  53. var notificationsNew = notificationsHolder.querySelectorAll('li[class="_33c jewelItemNew"]');
  54.  
  55. for (var i = 0; i < notificationsNew.length; i++) {
  56. var current = notificationsNew[i];
  57. var notificationParent = current.querySelector('div[class="_4l_v"]');
  58. var notificationYour = notificationParent.querySelectorAll('span');
  59. // match 1 for interest highlight, 2 for "Like" highlight.
  60. var match = false;
  61. for (var j=0; j < notificationYour.length; j++) {
  62. var cur = notificationYour[j];
  63. var t = cur.textContent;
  64. // Relevant text inside notification element.
  65. if (t.indexOf("replied to your") !== -1 ||
  66. t.indexOf("commented on your") !== -1 ||
  67. t.indexOf("shared your") !== -1 ||
  68. t.indexOf("mentioned you") !== -1 ||
  69. t.indexOf("tagged you") !== -1 ||
  70. t.indexOf("made you") !== -1 ||
  71. t.indexOf("also replied") !== -1 ||
  72. (t.indexOf("replied to") !== -1 && t.indexOf("on your") !== -1)) {
  73. match = 1;
  74. break;
  75. } else if (t.indexOf("likes your") !== -1 ||
  76. t.indexOf("like your") !== -1) {
  77. match = 2;
  78. break;
  79. }
  80. }
  81. // No match.
  82. if (match === false) {
  83. continue;
  84. }
  85. // Select color by match value.
  86. var color = "#d0dff6";
  87. switch(match) {
  88. case 1:
  89. color = "#d0dff6";
  90. break;
  91. case 2:
  92. color = "#faedf8";
  93. break;
  94. }
  95. // Update the li & a elements backgrounds.
  96. var a = current.querySelector('a[class="_33e _1_0e"]');
  97. a.style.backgroundColor = color;
  98. current.style.backgroundColor = color;
  99. current.paramA = a; // Pass to object, for highlight removal after clicked.
  100. current.addEventListener("click", removeHighlight, false);
  101. }
  102. timed = false;
  103. }
  104. // Right-Clicking the notifications button on FB.
  105. function onclickJewel(e) {
  106. if (e.which === 1) {
  107. updateNotifications();
  108. }
  109. }
  110. // Listen to a click on the notifications button on FB.
  111. var fbNotificationsJewel = document.getElementById("fbNotificationsJewel");
  112. fbNotificationsJewel.addEventListener("click", onclickJewel);
  113. })();