GitHub Static Time

A userscript that replaces relative times with a static time formatted as you like it

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

  1. // ==UserScript==
  2. // @name GitHub Static Time
  3. // @version 1.0.8
  4. // @description A userscript that replaces relative times with a static time formatted as you like it
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @include https://github.com/*
  9. // @run-at document-end
  10. // @grant GM_addStyle
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @grant GM_registerMenuCommand
  14. // @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js
  15. // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=882023
  16. // @icon https://github.githubassets.com/pinned-octocat.svg
  17. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  18. // ==/UserScript==
  19. (() => {
  20. /* global moment */
  21. "use strict";
  22.  
  23. let busy = false,
  24. timeFormat = GM_getValue("ghst-format", "LLL"),
  25. locale = GM_getValue("ghst-locale", "en");
  26.  
  27. // list copied from
  28. // https://github.com/moment/momentjs.com/blob/master/data/locale.js
  29. const locales = [
  30. { "abbr": "af", "name": "Afrikaans" },
  31. { "abbr": "sq", "name": "Albanian" },
  32. { "abbr": "ar", "name": "Arabic" },
  33. { "abbr": "ar-dz", "name": "Arabic (Algeria)" },
  34. { "abbr": "ar-kw", "name": "Arabic (Kuwait)" },
  35. { "abbr": "ar-ly", "name": "Arabic (Lybia)" },
  36. { "abbr": "ar-ma", "name": "Arabic (Morocco)" },
  37. { "abbr": "ar-sa", "name": "Arabic (Saudi Arabia)" },
  38. { "abbr": "ar-tn", "name": "Arabic (Tunisia)" },
  39. { "abbr": "hy-am", "name": "Armenian" },
  40. { "abbr": "az", "name": "Azerbaijani" },
  41. { "abbr": "eu", "name": "Basque" },
  42. { "abbr": "be", "name": "Belarusian" },
  43. { "abbr": "bn", "name": "Bengali" },
  44. { "abbr": "bs", "name": "Bosnian" },
  45. { "abbr": "br", "name": "Breton" },
  46. { "abbr": "bg", "name": "Bulgarian" },
  47. { "abbr": "my", "name": "Burmese" },
  48. { "abbr": "km", "name": "Cambodian" },
  49. { "abbr": "ca", "name": "Catalan" },
  50. { "abbr": "tzm", "name": "Central Atlas Tamazight" },
  51. { "abbr": "tzm-latn", "name": "Central Atlas Tamazight Latin" },
  52. { "abbr": "zh-cn", "name": "Chinese (China)" },
  53. { "abbr": "zh-hk", "name": "Chinese (Hong Kong)" },
  54. { "abbr": "zh-tw", "name": "Chinese (Taiwan)" },
  55. { "abbr": "cv", "name": "Chuvash" },
  56. { "abbr": "hr", "name": "Croatian" },
  57. { "abbr": "cs", "name": "Czech" },
  58. { "abbr": "da", "name": "Danish" },
  59. { "abbr": "nl", "name": "Dutch" },
  60. { "abbr": "nl-be", "name": "Dutch (Belgium)" },
  61. { "abbr": "en-au", "name": "English (Australia)" },
  62. { "abbr": "en-ca", "name": "English (Canada)" },
  63. { "abbr": "en-ie", "name": "English (Ireland)" },
  64. { "abbr": "en-nz", "name": "English (New Zealand)" },
  65. { "abbr": "en-gb", "name": "English (United Kingdom)" },
  66. { "abbr": "en", "name": "English (United States)" },
  67. { "abbr": "eo", "name": "Esperanto" },
  68. { "abbr": "et", "name": "Estonian" },
  69. { "abbr": "fo", "name": "Faroese" },
  70. { "abbr": "fi", "name": "Finnish" },
  71. { "abbr": "fr", "name": "French" },
  72. { "abbr": "fr-ca", "name": "French (Canada)" },
  73. { "abbr": "fr-ch", "name": "French (Switzerland)" },
  74. { "abbr": "fy", "name": "Frisian" },
  75. { "abbr": "gl", "name": "Galician" },
  76. { "abbr": "ka", "name": "Georgian" },
  77. { "abbr": "de", "name": "German" },
  78. { "abbr": "de-at", "name": "German (Austria)" },
  79. { "abbr": "de-ch", "name": "German (Switzerland)" },
  80. { "abbr": "el", "name": "Greek" },
  81. { "abbr": "he", "name": "Hebrew" },
  82. { "abbr": "hi", "name": "Hindi" },
  83. { "abbr": "hu", "name": "Hungarian" },
  84. { "abbr": "is", "name": "Icelandic" },
  85. { "abbr": "id", "name": "Indonesian" },
  86. { "abbr": "it", "name": "Italian" },
  87. { "abbr": "ja", "name": "Japanese" },
  88. { "abbr": "jv", "name": "Javanese" },
  89. { "abbr": "kn", "name": "Kannada" },
  90. { "abbr": "kk", "name": "Kazakh" },
  91. { "abbr": "tlh", "name": "Klingon" },
  92. { "abbr": "gom-latn", "name": "Konkani Latin script" },
  93. { "abbr": "ko", "name": "Korean" },
  94. { "abbr": "ky", "name": "Kyrgyz" },
  95. { "abbr": "lo", "name": "Lao" },
  96. { "abbr": "lv", "name": "Latvian" },
  97. { "abbr": "lt", "name": "Lithuanian" },
  98. { "abbr": "lb", "name": "Luxembourgish" },
  99. { "abbr": "mk", "name": "Macedonian" },
  100. { "abbr": "ms-my", "name": "Malay" },
  101. { "abbr": "ms", "name": "Malay" },
  102. { "abbr": "ml", "name": "Malayalam" },
  103. { "abbr": "dv", "name": "Maldivian" },
  104. { "abbr": "mi", "name": "Maori" },
  105. { "abbr": "mr", "name": "Marathi" },
  106. { "abbr": "me", "name": "Montenegrin" },
  107. { "abbr": "ne", "name": "Nepalese" },
  108. { "abbr": "se", "name": "Northern Sami" },
  109. { "abbr": "nb", "name": "Norwegian Bokmål" },
  110. { "abbr": "nn", "name": "Nynorsk" },
  111. { "abbr": "fa", "name": "Persian" },
  112. { "abbr": "pl", "name": "Polish" },
  113. { "abbr": "pt", "name": "Portuguese" },
  114. { "abbr": "pt-br", "name": "Portuguese (Brazil)" },
  115. { "abbr": "x-pseudo", "name": "Pseudo" },
  116. { "abbr": "pa-in", "name": "Punjabi (India)" },
  117. { "abbr": "ro", "name": "Romanian" },
  118. { "abbr": "ru", "name": "Russian" },
  119. { "abbr": "gd", "name": "Scottish Gaelic" },
  120. { "abbr": "sr", "name": "Serbian" },
  121. { "abbr": "sr-cyrl", "name": "Serbian Cyrillic" },
  122. { "abbr": "sd", "name": "Sindhi" },
  123. { "abbr": "si", "name": "Sinhalese" },
  124. { "abbr": "sk", "name": "Slovak" },
  125. { "abbr": "sl", "name": "Slovenian" },
  126. { "abbr": "es", "name": "Spanish" },
  127. { "abbr": "es-do", "name": "Spanish (Dominican Republic)" },
  128. { "abbr": "sw", "name": "Swahili" },
  129. { "abbr": "sv", "name": "Swedish" },
  130. { "abbr": "tl-ph", "name": "Tagalog (Philippines)" },
  131. { "abbr": "tzl", "name": "Talossan" },
  132. { "abbr": "ta", "name": "Tamil" },
  133. { "abbr": "te", "name": "Telugu" },
  134. { "abbr": "tet", "name": "Tetun Dili (East Timor)" },
  135. { "abbr": "th", "name": "Thai" },
  136. { "abbr": "bo", "name": "Tibetan" },
  137. { "abbr": "tr", "name": "Turkish" },
  138. { "abbr": "uk", "name": "Ukrainian" },
  139. { "abbr": "ur", "name": "Urdu" },
  140. { "abbr": "uz", "name": "Uzbek" },
  141. { "abbr": "uz-latn", "name": "Uzbek Latin" },
  142. { "abbr": "vi", "name": "Vietnamese" },
  143. { "abbr": "cy", "name": "Welsh" },
  144. { "abbr": "yo", "name": "Yoruba Nigeria" },
  145. { "abbr": "ss", "name": "siSwati" }
  146. ],
  147. block = document.createElement("span");
  148. block.className = "ghst-time time";
  149.  
  150. function staticTime(tempFormat) {
  151. if (busy) {
  152. return;
  153. }
  154. busy = true;
  155. let selector = typeof tempFormat === "string"
  156. // update existing timestamps
  157. ? ".ghst-time"
  158. // process html elements
  159. : "relative-time, time-ago";
  160. if ($(selector)) {
  161. let indx = 0;
  162. const els = $$(selector),
  163. len = els.length;
  164.  
  165. // loop with delay to allow user interaction
  166. const loop = () => {
  167. let el, time, node, formatted,
  168. // max number of DOM insertions per loop
  169. max = 0;
  170. while (max < 20 && indx < len) {
  171. if (indx >= len) {
  172. return;
  173. }
  174. el = els[indx];
  175. time = el.getAttribute("datetime") || "";
  176. if (el && time) {
  177. if (tempFormat) {
  178. formatted = moment(time).format(tempFormat);
  179. el.textContent = formatted;
  180. el.title = formatted;
  181. } else {
  182. formatted = moment(time).format(timeFormat);
  183. node = block.cloneNode(true);
  184. node.setAttribute("datetime", time);
  185. node.textContent = formatted;
  186. node.title = formatted;
  187. // el.parentElement may be null sometimes when using browser
  188. // back arrow
  189. if (el.parentElement) {
  190. // replace relative-time/time-ago element
  191. el.parentElement.replaceChild(node, el);
  192. }
  193. }
  194. max++;
  195. }
  196. indx++;
  197. }
  198. if (indx < len) {
  199. setTimeout(() => {
  200. loop();
  201. }, 200);
  202. }
  203. };
  204. loop();
  205. }
  206. busy = false;
  207. }
  208.  
  209. function addPanel() {
  210. const div = document.createElement("div");
  211. GM_addStyle(`
  212. #ghst-settings { opacity:0; visibility:hidden; }
  213. #ghst-settings.ghst-open { position:fixed; z-index:65535; top:0; bottom:0;
  214. left:0; right:0; opacity:1; visibility:visible;
  215. background:rgba(0, 0, 0, .5); }
  216. #ghst-settings-inner { position:fixed; left:50%; top:50%; width:25rem;
  217. transform:translate(-50%,-50%); box-shadow:0 .5rem 1rem #111;
  218. color:#c0c0c0 }
  219. #ghst-settings-inner .boxed-group-inner { height: 205px; }
  220. #ghst-footer { clear:both; border-top:1px solid rgba(68, 68, 68, .3);
  221. padding-top:5px; }
  222. `);
  223. div.id = "ghst-settings";
  224. let options = "";
  225. locales.forEach(loc => {
  226. let sel = loc.abbr === locale ? " selected" : "";
  227. options += `<option value="${loc.abbr}"${sel}>${loc.name}</option>`;
  228. });
  229. div.innerHTML = `
  230. <div id="ghst-settings-inner" class="boxed-group">
  231. <h3>GitHub Static Time Settings</h3>
  232. <div class="boxed-group-inner">
  233. <dl class="form-group flattened">
  234. <dt>
  235. <label for="ghst-locale">Select a locale</label>
  236. </dt>
  237. <dd>
  238. <select id="ghst-locale" class="form-select float-right" value="${locale}">
  239. ${options}
  240. </select>
  241. <br>
  242. </dd>
  243. </dl>
  244. <dl class="form-group flattened">
  245. <dt>
  246. <label for="ghst-format">
  247. <p>Set <a href="https://momentjs.com/docs/#/displaying/format/">
  248. MomentJS
  249. </a> format (e.g. "MMMM Do YYYY, h:mm A"):
  250. </p>
  251. </label>
  252. </dt>
  253. <dd>
  254. <input id="ghst-format" type="text" class="form-control" value="${timeFormat}"/>
  255. </dd>
  256. </dl>
  257. <div id="ghst-footer">
  258. <button type="button" id="ghst-cancel" class="btn btn-sm float-right">Cancel</button>
  259. <button type="button" id="ghst-save" class="btn btn-sm float-right">Save</button>
  260. </div>
  261. </div>
  262. </div>`;
  263. $("body").appendChild(div);
  264. on("#ghst-settings", "click", closePanel);
  265. on("body", "keyup", event => {
  266. if (
  267. event.key === "Escape" &&
  268. $("#ghst-settings").classList.contains("ghst-open")
  269. ) {
  270. closePanel(event);
  271. return false;
  272. } else if (event.key === "Enter" && event.shiftKey) {
  273. closePanel();
  274. update("save");
  275. }
  276. });
  277. on("#ghst-settings-inner", "click", event => {
  278. event.stopPropagation();
  279. event.preventDefault();
  280. });
  281. on("#ghst-save", "click", () => {
  282. closePanel();
  283. update("save");
  284. });
  285. on("#ghst-locale", "change", update);
  286. on("#ghst-format", "change", update);
  287. on("#ghst-cancel", "click", closePanel);
  288. }
  289.  
  290. function closePanel(event) {
  291. $("#ghst-settings").classList.remove("ghst-open");
  292. if (event) {
  293. return update("revert");
  294. }
  295. }
  296.  
  297. function update(mode) {
  298. if (mode === "revert") {
  299. $("#ghst-locale").value = locale;
  300. $("#ghst-format").value = timeFormat;
  301. }
  302. let loc = $("#ghst-locale").value || "en",
  303. time = $("#ghst-format").value || "LLL";
  304. if (mode === "save") {
  305. timeFormat = time;
  306. locale = loc;
  307. GM_setValue("ghst-format", timeFormat);
  308. GM_setValue("ghst-locale", locale);
  309. }
  310. moment.locale(loc);
  311. staticTime(time);
  312. return false;
  313. }
  314.  
  315. function $(str, el) {
  316. return (el || document).querySelector(str);
  317. }
  318.  
  319. function $$(str, el) {
  320. return Array.from((el || document).querySelectorAll(str));
  321. }
  322.  
  323. function on(el, name, handler) {
  324. $(el).addEventListener(name, handler);
  325. }
  326.  
  327. function init() {
  328. addPanel();
  329. moment.locale(locale);
  330. staticTime();
  331. }
  332.  
  333. // Add GM options
  334. GM_registerMenuCommand("Set GitHub static time format", () => {
  335. $("#ghst-settings").classList.add("ghst-open");
  336. });
  337.  
  338. // repo file list needs additional time to render
  339. document.addEventListener("ghmo:container", () => {
  340. setTimeout(() => {
  341. staticTime();
  342. }, 100);
  343. });
  344. document.addEventListener("ghmo:preview", staticTime);
  345. init();
  346.  
  347. })();