GitHub Title Notification

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

目前为 2017-09-02 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Title Notification
  3. // @version 1.0.4
  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. const val = prompt("Indicator Value (it will be wrapped in parentheses)?", indicator);
  48. if (val !== null) {
  49. GM_setValue("indicator", indicator);
  50. check();
  51. }
  52. });
  53. GM_registerMenuCommand("Set GitHub Title Notification Interval", () => {
  54. const val = prompt("Interval Value (in seconds)?", interval);
  55. if (val !== null) {
  56. GM_setValue("interval", interval);
  57. setTimer();
  58. }
  59. });
  60.  
  61. setTimer();
  62.  
  63. })();