GitHub汉化插件

GitHub汉化插件,包含人机翻译

当前为 2021-04-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Internationalization
  3. // @name:zh-CN GitHub汉化插件
  4. // @name:ja GitHub日本語
  5. // @namespace https://github.com/k1995/github-i18n-plugin/
  6. // @version 0.11
  7. // @description Translate GitHub.com
  8. // @description:zh GitHub汉化插件,包含人机翻译
  9. // @description:zh-CN GitHub汉化插件,包含人机翻译
  10. // @description:ja GitHub日本語プラグイン
  11. // @author k1995
  12. // @match https://github.com/*
  13. // @match https://gist.github.com/*
  14. // @grant GM_xmlhttpRequest
  15. // @grant GM_getResourceText
  16. // @resource zh-CN https://www.githubs.cn/raw-githubusercontent/k1995/github-i18n-plugin/master/locales/zh-CN.json?v=20210407
  17. // @resource ja https://www.githubs.cn/raw-githubusercontent/k1995/github-i18n-plugin/master/locales/ja.json
  18. // @require https://cdn.bootcdn.net/ajax/libs/timeago.js/4.0.2/timeago.full.min.js
  19. // @require https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js
  20. // ==/UserScript==
  21.  
  22. (function() {
  23. 'use strict';
  24.  
  25. const SUPPORT_LANG = ["zh-CN", "ja"];
  26. const lang = (navigator.language || navigator.userLanguage);
  27. const locales = getLocales(lang)
  28.  
  29. translateByCssSelector();
  30. translateDesc();
  31. traverseElement(document.body);
  32. watchUpdate();
  33.  
  34. function getLocales(lang) {
  35. if(lang.startsWith("zh")) { // zh zh-TW --> zh-CN
  36. lang = "zh-CN";
  37. }
  38. if(SUPPORT_LANG.includes(lang)) {
  39. return JSON.parse(GM_getResourceText(lang));
  40. }
  41. return {
  42. css: [],
  43. dict: {}
  44. };
  45. }
  46.  
  47. function translateRelativeTimeEl(el) {
  48. const datetime = $(el).attr('datetime');
  49. $(el).text(timeago.format(datetime, lang.replace('-', '_')));
  50. }
  51.  
  52. function translateElement(el) {
  53. // Get the text field name
  54. let k;
  55. if(el.tagName === "INPUT") {
  56. if (el.type === 'button' || el.type === 'submit') {
  57. k = 'value';
  58. } else {
  59. k = 'placeholder';
  60. }
  61. } else {
  62. k = 'data';
  63. }
  64.  
  65. const txtSrc = el[k].trim();
  66. const key = txtSrc.toLowerCase()
  67. .replace(/\xa0/g, ' ') // replace ' '
  68. .replace(/\s{2,}/g, ' ');
  69.  
  70. if(locales.dict[key]) {
  71. el[k] = el[k].replace(txtSrc, locales.dict[key])
  72. }
  73. }
  74.  
  75. function shoudTranslateEl(el) {
  76. const blockIds = ["readme"];
  77. const blockClass = ["CodeMirror"];
  78. const blockTags = ["CODE", "SCRIPT", "LINK", "IMG", "svg", "TABLE", "ARTICLE"];
  79.  
  80. if(blockTags.includes(el.tagName)) {
  81. return false;
  82. }
  83.  
  84. if(el.id && blockIds.includes(el.id)) {
  85. return false;
  86. }
  87.  
  88. if(el.classList) {
  89. for(let clazz of blockClass) {
  90. if(el.classList.contains(clazz)) {
  91. return false;
  92. }
  93. }
  94. }
  95.  
  96. return true;
  97. }
  98.  
  99. function traverseElement(el) {
  100. if(!shoudTranslateEl(el)) {
  101. return
  102. }
  103.  
  104. for(const child of el.childNodes) {
  105. if(["RELATIVE-TIME", "TIME-AGO"].includes(el.tagName)) {
  106. translateRelativeTimeEl(el);
  107. return;
  108. }
  109.  
  110. if(child.nodeType === Node.TEXT_NODE) {
  111. translateElement(child);
  112. }
  113. else if(child.nodeType === Node.ELEMENT_NODE) {
  114. if(child.tagName === "INPUT") {
  115. translateElement(child);
  116. } else {
  117. traverseElement(child);
  118. }
  119. } else {
  120. // pass
  121. }
  122. }
  123. }
  124.  
  125. function watchUpdate() {
  126. const m = window.MutationObserver || window.WebKitMutationObserver;
  127. const observer = new m(function (mutations, observer) {
  128. for(let mutationRecord of mutations) {
  129. for(let node of mutationRecord.addedNodes) {
  130. traverseElement(node);
  131. }
  132. }
  133. });
  134.  
  135. observer.observe(document.body, {
  136. subtree: true,
  137. characterData: true,
  138. childList: true,
  139. });
  140. }
  141.  
  142. // translate "about"
  143. function translateDesc() {
  144. $(".repository-content .f4").append("<br/>");
  145. $(".repository-content .f4").append("<a id='translate-me' href='#' style='color:rgb(27, 149, 224);font-size: small'>翻译</a>");
  146. $("#translate-me").click(function() {
  147. // get description text
  148. const desc = $(".repository-content .f4")
  149. .clone()
  150. .children()
  151. .remove()
  152. .end()
  153. .text()
  154. .trim();
  155.  
  156. if(!desc) {
  157. return;
  158. }
  159.  
  160. GM_xmlhttpRequest({
  161. method: "GET",
  162. url: `https://www.githubs.cn/translate?q=`+ encodeURIComponent(desc),
  163. onload: function(res) {
  164. if (res.status === 200) {
  165. $("#translate-me").hide();
  166. // render result
  167. const text = res.responseText;
  168. $(".repository-content .f4").append("<span style='font-size: small'>由 <a target='_blank' style='color:rgb(27, 149, 224);' href='https://www.githubs.cn'>GitHub中文社区</a> 翻译👇</span>");
  169. $(".repository-content .f4").append("<br/>");
  170. $(".repository-content .f4").append(text);
  171. } else {
  172. alert("翻译失败");
  173. }
  174. }
  175. });
  176. });
  177. }
  178.  
  179. function translateByCssSelector() {
  180. if(locales.css) {
  181. for(var css of locales.css) {
  182. if($(css.selector).length > 0) {
  183. if(css.key === '!html') {
  184. $(css.selector).html(css.replacement);
  185. } else {
  186. $(css.selector).attr(css.key, css.replacement);
  187. }
  188. }
  189. }
  190. }
  191. }
  192. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址