GitHub Title Notification

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

当前为 2016-12-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Title Notification
  3. // @version 1.0.1
  4. // @description A userscript that changes the document title if there are unread messages
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace http://github.com/Mottie
  7. // @include https://github.com/*
  8. // @run-at document-idle
  9. // @grant GM_registerMenuCommand
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @author Rob Garrison
  13. // ==/UserScript==
  14. /* jshint unused:true, esnext:true */
  15. (function() {
  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 hasClass(el, name) {
  25. if (el) {
  26. return el.classList ? el.classList.contains(name) : new RegExp("\\b" + name + "\\b").test(el.className);
  27. }
  28. return false;
  29. }
  30.  
  31. function check() {
  32. let title = document.title,
  33. hasUnread = hasClass(document.querySelector(".mail-status"), "unread");
  34. //
  35. if (!/^\(\d+\)/.test(title)) {
  36. title = title.replace(/^\([^)]+\)\s/, "");
  37. }
  38. document.title = hasUnread ? "(" + indicator + ") " + title : title;
  39. }
  40.  
  41. function setTimer() {
  42. clearInterval(timer);
  43. if (document.querySelector(".mail-status")) {
  44. timer = setInterval(function() {
  45. check();
  46. }, interval * 1000);
  47. check();
  48. }
  49. }
  50.  
  51. // Add GM options
  52. GM_registerMenuCommand("Set GitHub Title Notification Indicator", function() {
  53. indicator = prompt("Indicator Value (it will be wrapped in parentheses)?", indicator);
  54. GM_setValue("indicator", indicator);
  55. check();
  56. });
  57. GM_registerMenuCommand("Set GitHub Title Notification Interval", function() {
  58. interval = prompt("Interval Value (in seconds)?", interval);
  59. GM_setValue("interval", interval);
  60. setTimer();
  61. });
  62.  
  63. setTimer();
  64.  
  65. })();