GitHub Static Time

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

当前为 2017-10-08 提交的版本,查看 最新版本

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