Github Clear Date

Add a clear date to the relative time in Github

当前为 2023-11-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Github Clear Date
  3. // @namespace https://github.com/wzshiming/userscripts
  4. // @version 0.4.1
  5. // @description Add a clear date to the relative time in Github
  6. // @author wzshiming
  7. // @match *://github.com/*
  8. // @grant none
  9. // @icon https://github.githubassets.com/pinned-octocat.svg
  10. // @supportURL https://github.com/wzshiming/userscripts/issues
  11. // @license MIT License
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. let MutationObserver = window.MutationObserver ||
  18. window.WebKitMutationObserver ||
  19. window.MozMutationObserver;
  20.  
  21. let config = {
  22. childList: true,
  23. subtree: true,
  24. attributes: true,
  25. };
  26. let observer = new MutationObserver(mutation);
  27. observer.observe(document.body, config);
  28. mutate(document.body);
  29. })();
  30.  
  31. function mutate(elem) {
  32. elem.querySelectorAll('relative-time').forEach(formatRelativeTime);
  33. }
  34.  
  35. function mutation(mutationsList) {
  36. for (let mutation of mutationsList) {
  37. mutate(mutation.target.parentNode);
  38. }
  39. }
  40.  
  41. function formatRelativeTime(item) {
  42. let text = item.shadowRoot.innerHTML;
  43. if (text.length == 0 || text.indexOf("(") >= 0) {
  44. return
  45. }
  46.  
  47. let datetime = new Date(item.datetime);
  48. let now = new Date();
  49.  
  50. let dateStr = formatTime(datetime, now)
  51. if (dateStr.length == 0) {
  52. return
  53. }
  54. item.shadowRoot.innerHTML += "(" + dateStr + ")";
  55. return
  56. }
  57.  
  58. function formatDate(datetime, now) {
  59. let hour = datetime.getHours();
  60. let minute = datetime.getMinutes();
  61. let second = datetime.getSeconds();
  62. let nowHour = now.getHours();
  63. let nowMinute = now.getMinutes();
  64. let nowSecond = now.getSeconds();
  65.  
  66. if (hour == nowHour &&
  67. minute == nowMinute &&
  68. second == nowSecond) {
  69. return "";
  70. }
  71.  
  72. return formatNumber(hour) + ":" + formatNumber(minute) + ":" + formatNumber(second);
  73. }
  74.  
  75. function formatTime(datetime, now) {
  76. let year = datetime.getFullYear();
  77. let month = datetime.getMonth();
  78. let day = datetime.getDate();
  79. let nowYear = now.getFullYear();
  80. let nowMonth = now.getMonth();
  81. let nowDay = now.getDate();
  82.  
  83. if (year == nowYear &&
  84. month == nowMonth &&
  85. day == nowDay) {
  86. return formatDate(datetime, now);
  87. }
  88.  
  89. // append date
  90. if (year == nowYear) { // this year will be omitted
  91. return formatNumber(month + 1) + "/" + formatNumber(day);
  92. }
  93.  
  94. let century = Math.round(year / 100);
  95. let nowCentury = Math.round(nowYear / 100);
  96. if (century == nowCentury) { // this century will be omitted
  97. return formatNumber(year - 2000) + "/" + formatNumber(month + 1) + "/" + formatNumber(day);
  98. }
  99.  
  100. return formatNumber(year) + "/" + formatNumber(month + 1) + "/" + formatNumber(day);
  101. }
  102.  
  103. function formatNumber(num) {
  104. return num < 10 ? "0" + num : "" + num;
  105. }