GitHub汉化插件

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

当前为 2021-09-11 提交的版本,查看 最新版本

  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.17
  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.bootcss.com/timeago.js/4.0.2/timeago.full.min.js
  19. // @require https://cdn.bootcss.com/jquery/3.4.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. traverseElement(document.body);
  31. watchUpdate();
  32.  
  33. // 翻译描述
  34. if(window.location.pathname.split('/').length == 3) {
  35. translateDesc(".repository-content .f4"); //仓库简介翻译
  36. translateDesc(".gist-content [itemprop='about']"); // Gist 简介翻译
  37. }
  38.  
  39.  
  40. function getLocales(lang) {
  41. if(lang.startsWith("zh")) { // zh zh-TW --> zh-CN
  42. lang = "zh-CN";
  43. }
  44. if(SUPPORT_LANG.includes(lang)) {
  45. return JSON.parse(GM_getResourceText(lang));
  46. }
  47. return {
  48. css: [],
  49. dict: {}
  50. };
  51. }
  52.  
  53. function translateRelativeTimeEl(el) {
  54. const datetime = $(el).attr('datetime');
  55. $(el).text(timeago.format(datetime, lang.replace('-', '_')));
  56. }
  57.  
  58. function translateElement(el) {
  59. // Get the text field name
  60. let k;
  61. if(el.tagName === "INPUT") {
  62. if (el.type === 'button' || el.type === 'submit') {
  63. k = 'value';
  64. } else {
  65. k = 'placeholder';
  66. }
  67. } else {
  68. k = 'data';
  69. }
  70.  
  71. const txtSrc = el[k].trim();
  72. const key = txtSrc.toLowerCase()
  73. .replace(/\xa0/g, ' ') // replace ' '
  74. .replace(/\s{2,}/g, ' ');
  75. if (locales.dict[key]) {
  76. el[k] = el[k].replace(txtSrc, locales.dict[key])
  77. }
  78. translateElementAriaLabel(el)
  79. }
  80.  
  81. function translateElementAriaLabel(el) {
  82. if (el.ariaLabel) {
  83. const k = 'ariaLabel'
  84. const txtSrc = el[k].trim();
  85. const key = txtSrc.toLowerCase()
  86. .replace(/\xa0/g, ' ') // replace ' '
  87. .replace(/\s{2,}/g, ' ');
  88. if (locales.dict[key]) {
  89. el[k] = el[k].replace(txtSrc, locales.dict[key])
  90. }
  91. }
  92. }
  93.  
  94. function shouldTranslateEl(el) {
  95. const blockIds = ["readme", "wiki-content"];
  96. const blockClass = [
  97. "CodeMirror",
  98. "css-truncate", // 过滤文件目录
  99. "blob-code",
  100. "topic-tag", // 过滤标签,
  101. // "text-normal", // 过滤repo name, 复现:https://github.com/search?q=explore
  102. "repo-list"//过滤搜索结果项目,解决"text-normal"导致的有些文字不翻译的问题,搜索结果以后可以考虑单独翻译
  103. ];
  104. const blockTags = ["CODE", "SCRIPT", "LINK", "IMG", "svg", "TABLE", "ARTICLE", "PRE"];
  105. const blockItemprops = ["name"];
  106.  
  107. if (blockTags.includes(el.tagName)) {
  108. return false;
  109. }
  110.  
  111. if (el.id && blockIds.includes(el.id)) {
  112. return false;
  113. }
  114.  
  115. if (el.classList) {
  116. for (let clazz of blockClass) {
  117. if (el.classList.contains(clazz)) {
  118. return false;
  119. }
  120. }
  121. }
  122.  
  123. if (el.getAttribute) {
  124. let itemprops = el.getAttribute("itemprop");
  125. if (itemprops) {
  126. itemprops = itemprops.split(" ");
  127. for (let itemprop of itemprops) {
  128. console.log(itemprop)
  129. if (blockItemprops.includes(itemprop)) {
  130. return false;
  131. }
  132. }
  133. }
  134. }
  135.  
  136. return true;
  137. }
  138.  
  139. function traverseElement(el) {
  140. translateElementAriaLabel(el)
  141. if (!shouldTranslateEl(el)) {
  142. return
  143. }
  144.  
  145. for (const child of el.childNodes) {
  146. if (["RELATIVE-TIME", "TIME-AGO"].includes(el.tagName)) {
  147. translateRelativeTimeEl(el);
  148. return;
  149. }
  150.  
  151. if (child.nodeType === Node.TEXT_NODE) {
  152. translateElement(child);
  153. }
  154. else if(child.nodeType === Node.ELEMENT_NODE) {
  155. if (child.tagName === "INPUT") {
  156. translateElement(child);
  157. } else {
  158. traverseElement(child);
  159. }
  160. } else {
  161. // pass
  162. }
  163. }
  164. }
  165.  
  166. function watchUpdate() {
  167. const m = window.MutationObserver || window.WebKitMutationObserver;
  168. const observer = new m(function (mutations, observer) {
  169. for(let mutationRecord of mutations) {
  170. for(let node of mutationRecord.addedNodes) {
  171. traverseElement(node);
  172. }
  173. }
  174. });
  175.  
  176. observer.observe(document.body, {
  177. subtree: true,
  178. characterData: true,
  179. childList: true,
  180. });
  181. }
  182.  
  183. // translate "about"
  184. function translateDesc(el) {
  185. $(el).append("<br/>");
  186. $(el).append("<a id='translate-me' href='#' style='color:rgb(27, 149, 224);font-size: small'>翻译</a>");
  187. $("#translate-me").click(function() {
  188. // get description text
  189. const desc = $(el)
  190. .clone()
  191. .children()
  192. .remove()
  193. .end()
  194. .text()
  195. .trim();
  196.  
  197. if(!desc) {
  198. return;
  199. }
  200.  
  201. GM_xmlhttpRequest({
  202. method: "GET",
  203. url: `https://www.githubs.cn/translate?q=`+ encodeURIComponent(desc),
  204. onload: function(res) {
  205. if (res.status === 200) {
  206. $("#translate-me").hide();
  207. // render result
  208. const text = res.responseText;
  209. $(el).append("<span style='font-size: small'>由 <a target='_blank' style='color:rgb(27, 149, 224);' href='https://www.githubs.cn'>GitHub中文社区</a> 翻译👇</span>");
  210. $(el).append("<br/>");
  211. $(el).append(text);
  212. } else {
  213. alert("翻译失败");
  214. }
  215. }
  216. });
  217. });
  218. }
  219.  
  220. function translateByCssSelector() {
  221. if(locales.css) {
  222. for(var css of locales.css) {
  223. if($(css.selector).length > 0) {
  224. if(css.key === '!html') {
  225. $(css.selector).html(css.replacement);
  226. } else {
  227. $(css.selector).attr(css.key, css.replacement);
  228. }
  229. }
  230. }
  231. }
  232. }
  233. })();

QingJ © 2025

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