GitHub Title Notification

A userscript that changes the document title if there are unread messages

目前為 2017-05-16 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub Title Notification
  3. // @version 1.0.3
  4. // @description A userscript that changes the document title if there are unread messages
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @include https://github.com/*
  9. // @run-at document-idle
  10. // @grant GM_registerMenuCommand
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @icon https://github.com/fluidicon.png
  14. // ==/UserScript==
  15. (() => {
  16. "use strict";
  17.  
  18. let timer,
  19. // indicator added to document title (it will be wrapped in parentheses)
  20. indicator = GM_getValue("indicator", "♥"),
  21. // check every 30 seconds
  22. interval = GM_getValue("interval", 30);
  23.  
  24. function check() {
  25. let title = document.title,
  26. mail = document.querySelector(".mail-status"),
  27. hasUnread = mail ? mail.classList.contains("unread") : false;
  28. //
  29. if (!/^\(\d+\)/.test(title)) {
  30. title = title.replace(/^(\([^)]+\)\s)*/g, "");
  31. }
  32. document.title = hasUnread ? "(" + indicator + ") " + title : title;
  33. }
  34.  
  35. function setTimer() {
  36. clearInterval(timer);
  37. if (document.querySelector(".mail-status")) {
  38. timer = setInterval(() => {
  39. check();
  40. }, interval * 1000);
  41. check();
  42. }
  43. }
  44.  
  45. // Add GM options
  46. GM_registerMenuCommand("Set GitHub Title Notification Indicator", () => {
  47. indicator = prompt("Indicator Value (it will be wrapped in parentheses)?", indicator);
  48. GM_setValue("indicator", indicator);
  49. check();
  50. });
  51. GM_registerMenuCommand("Set GitHub Title Notification Interval", () => {
  52. interval = prompt("Interval Value (in seconds)?", interval);
  53. GM_setValue("interval", interval);
  54. setTimer();
  55. });
  56.  
  57. setTimer();
  58.  
  59. })();