GitHub Static Time

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

当前为 2019-01-29 提交的版本,查看 最新版本

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