Github Clear Date

Add a clear date to the relative time in Github

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

  1. // ==UserScript==
  2. // @name Github Clear Date
  3. // @namespace https://github.com/wzshiming/userscripts
  4. // @version 0.3.0
  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(mutationForce);
  27. observer.observe(document.body, config);
  28. mutate(document.body);
  29. })();
  30.  
  31. function mutate(elem) {
  32. elem.querySelectorAll('relative-time').forEach(formatTime);
  33. }
  34.  
  35. function mutationForce(mutationsList, observer) {
  36. mutation(mutationsList);
  37. setTimeout(function () {
  38. mutation(mutationsList);
  39. }, 1000 + Math.round(Math.random() * 1000));
  40. }
  41.  
  42. function mutation(mutationsList) {
  43. for (let mutation of mutationsList) {
  44. mutate(mutation.target.parentNode);
  45. }
  46. }
  47.  
  48. function formatTime(item) {
  49. let text = item.shadowRoot.innerHTML;
  50. if (text.length == 0 || text.indexOf("(") >= 0) {
  51. return
  52. }
  53. let date = item.datetime.split("T")[0].replaceAll("-", "/")
  54. if (date.length < 8) {
  55. return
  56. }
  57.  
  58. let now = new Date();
  59. let year = now.getFullYear();
  60. if (date.indexOf(year) == 0) {
  61. date = date.substr(5);
  62. } else if (date[0] == "2" && date[1] == "0") {
  63. date = date.substr(2);
  64. }
  65.  
  66. item.shadowRoot.innerHTML += "(" + date + ")";
  67. }