GitHub Title Notification

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

目前為 2021-02-21 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub Title Notification
  3. // @version 1.0.6
  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.githubassets.com/pinned-octocat.svg
  14. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  15. // ==/UserScript==
  16. (() => {
  17. "use strict";
  18.  
  19. let timer,
  20. // indicator added to document title (it will be wrapped in parentheses)
  21. indicator = GM_getValue("indicator", "♥"),
  22. // check every 30 seconds
  23. interval = GM_getValue("interval", 30);
  24.  
  25. function check() {
  26. let title = document.title,
  27. mail = document.querySelector(".mail-status"),
  28. hasUnread = mail ? mail.classList.contains("unread") : false;
  29. //
  30. if (!/^\(\d+\)/.test(title)) {
  31. title = title.replace(/^(\([^)]+\)\s)*/g, "");
  32. }
  33. document.title = hasUnread ? "(" + indicator + ") " + title : title;
  34. }
  35.  
  36. function setTimer() {
  37. clearInterval(timer);
  38. if (document.querySelector(".mail-status")) {
  39. timer = setInterval(() => {
  40. check();
  41. }, interval * 1000);
  42. check();
  43. }
  44. }
  45.  
  46. // Add GM options
  47. GM_registerMenuCommand("Set GitHub Title Notification Indicator", () => {
  48. const val = prompt("Indicator Value (it will be wrapped in parentheses)?", indicator);
  49. if (val !== null) {
  50. GM_setValue("indicator", indicator);
  51. check();
  52. }
  53. });
  54. GM_registerMenuCommand("Set GitHub Title Notification Interval", () => {
  55. const val = prompt("Interval Value (in seconds)?", interval);
  56. if (val !== null) {
  57. GM_setValue("interval", interval);
  58. setTimer();
  59. }
  60. });
  61.  
  62. setTimer();
  63.  
  64. })();