Github-time-format-changer

Change Github time format.

  1. // ==UserScript==
  2. // @id github.com-ff599db1-47d8-b14f-83b4-3e345f6d67e3@http://efcl.info/
  3. // @name Github-time-format-changer
  4. // @version 1.1
  5. // @namespace http://efcl.info/
  6. // @author azu
  7. // @license MIT
  8. // @description Change Github time format.
  9. // @include https://github.com/*
  10. // @run-at document-end
  11. // @grant none
  12. // @require https://cdn.jsdelivr.net/momentjs/2.6.0/moment.min.js
  13. // ==/UserScript==
  14.  
  15. var $ = unsafeWindow.$;
  16. var toArray = Function.prototype.call.bind(Array.prototype.slice);
  17. var relative = /ago/i;
  18. function _update(body) {
  19. var times = body.getElementsByTagName("time");
  20. toArray(times).forEach(function (timeElement) {
  21. if (!relative.test(timeElement.textContent)) {
  22. timeElement.textContent = moment(timeElement.getAttribute("datetime")).fromNow();
  23. }
  24. });
  25. }
  26. function update(body) {
  27. requestAnimationFrame(function () {
  28. _update(body);
  29. });
  30. }
  31. (function () {
  32. // pjax
  33. $(document).on('pjax:popstate pjax:end', function pjaxEnd() {
  34. update(document.body);
  35. });
  36. // AutoPagerize
  37. var addFilterHandler = function (evt) {
  38. var node = evt.target;
  39. update(node);
  40. };
  41. document.body.addEventListener('AutoPagerize_DOMNodeInserted', addFilterHandler, false);
  42. // mutation chane update
  43. var target = document.querySelector('body');
  44. var config = {
  45. childList: true,
  46. subtree: true
  47. };
  48.  
  49. var observer = new MutationObserver(function (mutations, self) {
  50. mutations.some(function (mutation) {
  51. if (mutation.type === 'childList' && mutation.target.nodeName === "TIME") {
  52. update(document.body);
  53. return true;
  54. }
  55. });
  56. });
  57.  
  58. observer.observe(target, config);
  59. })();
  60. // MAIN =
  61. update(document.body);