GitHub汉化插件

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

当前为 2023-09-18 提交的版本,查看 最新版本

  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.28
  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.github-zh.com/raw-githubusercontent/k1995/github-i18n-plugin/master/locales/zh-CN.json?v=20230918
  17. // @resource ja https://www.githubs.cn/raw-githubusercontent/k1995/github-i18n-plugin/master/locales/ja.json
  18. // @require https://cdn.staticfile.org/timeago.js/4.0.2/timeago.min.js
  19. // @require https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js
  20. // @license MIT
  21. // ==/UserScript==
  22.  
  23. (function() {
  24. 'use strict';
  25.  
  26. const SUPPORT_LANG = ["zh-CN", "ja"];
  27. const lang = (navigator.language || navigator.userLanguage);
  28. const locales = getLocales(lang)
  29.  
  30. translateByCssSelector();
  31. translateTime();
  32. traverseElement(document.body);
  33. watchUpdate();
  34.  
  35. // 翻译描述
  36. if(window.location.pathname.split('/').length == 3) {
  37. //translateDesc(".repository-content .f4"); //仓库简介翻译
  38. //translateDesc(".gist-content [itemprop='about']"); // Gist 简介翻译
  39. }
  40.  
  41.  
  42. function getLocales(lang) {
  43. if(lang.startsWith("zh")) { // zh zh-TW --> zh-CN
  44. lang = "zh-CN";
  45. }
  46. if(SUPPORT_LANG.includes(lang)) {
  47. return JSON.parse(GM_getResourceText(lang));
  48. }
  49. return {
  50. css: [],
  51. dict: {}
  52. };
  53. }
  54.  
  55. function translateRelativeTimeEl(el) {
  56. if(!$(el).attr('translated')) {
  57. const datetime = $(el).attr('datetime');
  58. $(el).attr('translated', true);
  59. let humanTime = timeago.format(datetime, lang.replace('-', '_'));
  60. el.shadowRoot.textContent = humanTime;
  61. }
  62. }
  63.  
  64. function translateElement(el) {
  65. // Get the text field name
  66. let k;
  67. if(el.tagName === "INPUT") {
  68. if (el.type === 'button' || el.type === 'submit') {
  69. k = 'value';
  70. } else {
  71. k = 'placeholder';
  72. }
  73. } else {
  74. k = 'data';
  75. }
  76.  
  77. if (isNaN(el[k])){
  78. const txtSrc = el[k].trim();
  79. const key = txtSrc.toLowerCase()
  80. .replace(/\xa0/g, ' ') // replace ' '
  81. .replace(/\s{2,}/g, ' ');
  82. if (locales.dict[key]) {
  83. el[k] = el[k].replace(txtSrc, locales.dict[key])
  84. }
  85. }
  86. translateElementAriaLabel(el)
  87. }
  88.  
  89. function translateElementAriaLabel(el) {
  90. if (el.ariaLabel) {
  91. const k = 'ariaLabel'
  92. const txtSrc = el[k].trim();
  93. const key = txtSrc.toLowerCase()
  94. .replace(/\xa0/g, ' ') // replace ' '
  95. .replace(/\s{2,}/g, ' ');
  96. if (locales.dict[key]) {
  97. el[k] = el[k].replace(txtSrc, locales.dict[key])
  98. }
  99. }
  100. }
  101.  
  102. function shouldTranslateEl(el) {
  103. const blockIds = [
  104. "readme",
  105. "file-name-editor-breadcrumb", "StickyHeader" // fix repo详情页文件路径breadcrumb
  106. ];
  107. const blockClass = [
  108. "CodeMirror",
  109. "js-navigation-container", // 过滤文件目录
  110. "blob-code",
  111. "topic-tag", // 过滤标签,
  112. // "text-normal", // 过滤repo name, 复现:https://github.com/search?q=explore
  113. "repo-list",//过滤搜索结果项目,解决"text-normal"导致的有些文字不翻译的问题,搜索结果以后可以考虑单独翻译
  114. "js-path-segment","final-path", //过滤目录,文件位置栏
  115. "markdown-body", // 过滤wiki页面,
  116. "search-input-container", //搜索框
  117. "search-match", //fix搜索结果页,repo name被翻译
  118. "cm-editor", //代码编辑框
  119. "PRIVATE_TreeView-item", // 文件树
  120. "repo", // 项目名称
  121. ];
  122. const blockTags = ["CODE", "SCRIPT", "LINK", "IMG", "svg", "TABLE", "ARTICLE", "PRE"];
  123. const blockItemprops = ["name"];
  124.  
  125. if (blockTags.includes(el.tagName)) {
  126. return false;
  127. }
  128.  
  129. if (el.id && blockIds.includes(el.id)) {
  130. return false;
  131. }
  132.  
  133. if (el.classList) {
  134. for (let clazz of blockClass) {
  135. if (el.classList.contains(clazz)) {
  136. return false;
  137. }
  138. }
  139. }
  140.  
  141. if (el.getAttribute) {
  142. let itemprops = el.getAttribute("itemprop");
  143. if (itemprops) {
  144. itemprops = itemprops.split(" ");
  145. for (let itemprop of itemprops) {
  146. if (blockItemprops.includes(itemprop)) {
  147. return false;
  148. }
  149. }
  150. }
  151. }
  152.  
  153. return true;
  154. }
  155.  
  156. function traverseElement(el) {
  157. translateElementAriaLabel(el)
  158. if (!shouldTranslateEl(el)) {
  159. return
  160. }
  161.  
  162. if (el.childNodes.length === 0) {
  163. if (el.nodeType === Node.TEXT_NODE) {
  164. translateElement(el);
  165. return;
  166. }
  167. else if(el.nodeType === Node.ELEMENT_NODE) {
  168. if (el.tagName === "INPUT") {
  169. translateElement(el);
  170. return;
  171. }
  172. }
  173. }
  174.  
  175. for (const child of el.childNodes) {
  176. if (child.nodeType === Node.TEXT_NODE) {
  177. translateElement(child);
  178. }
  179. else if(child.nodeType === Node.ELEMENT_NODE) {
  180. if (child.tagName === "INPUT") {
  181. translateElement(child);
  182. } else {
  183. traverseElement(child);
  184. }
  185. } else {
  186. // pass
  187. }
  188. }
  189. }
  190.  
  191. function watchUpdate() {
  192. const m = window.MutationObserver || window.WebKitMutationObserver;
  193. const observer = new m(function (mutations, observer) {
  194. var reTrans = false;
  195. for(let mutationRecord of mutations) {
  196. if (mutationRecord.addedNodes || mutationRecord.type === 'attributes') {
  197. reTrans = true;
  198. // traverseElement(mutationRecord.target);
  199. }
  200. }
  201. if(reTrans) {
  202. traverseElement(document.body);
  203. }
  204. });
  205.  
  206. observer.observe(document.body, {
  207. subtree: true,
  208. characterData: true,
  209. childList: true,
  210. attributeFilter: ['value', 'placeholder', 'aria-label', 'data', 'data-confirm'], // 仅观察特定属性变化(试验测试阶段,有问题再恢复)
  211. });
  212. }
  213.  
  214. // translate "about"
  215. function translateDesc(el) {
  216. $(el).append("<br/>");
  217. $(el).append("<a id='translate-me' href='#' style='color:rgb(27, 149, 224);font-size: small'>翻译</a>");
  218. $("#translate-me").click(function() {
  219. // get description text
  220. const desc = $(el)
  221. .clone()
  222. .children()
  223. .remove()
  224. .end()
  225. .text()
  226. .trim();
  227.  
  228. if(!desc) {
  229. return;
  230. }
  231.  
  232. GM_xmlhttpRequest({
  233. method: "GET",
  234. url: `https://www.github-zh.com/translate?q=`+ encodeURIComponent(desc),
  235. onload: function(res) {
  236. if (res.status === 200) {
  237. $("#translate-me").hide();
  238. // render result
  239. const text = res.responseText;
  240. $(el).append("<span style='font-size: small'>由 <a target='_blank' style='color:rgb(27, 149, 224);' href='https://www.githubs.cn'>GitHub中文社区</a> 翻译👇</span>");
  241. $(el).append("<br/>");
  242. $(el).append(text);
  243. } else {
  244. alert("翻译失败");
  245. }
  246. }
  247. });
  248. });
  249. }
  250.  
  251. function translateByCssSelector() {
  252. if(locales.css) {
  253. for(var css of locales.css) {
  254. if($(css.selector).length > 0) {
  255. if(css.key === '!html') {
  256. $(css.selector).html(css.replacement);
  257. } else {
  258. $(css.selector).attr(css.key, css.replacement);
  259. }
  260. }
  261. }
  262. }
  263. }
  264.  
  265. function translateTime() {
  266. $("relative-time").each(function() {
  267. translateRelativeTimeEl(this);
  268. })
  269. }
  270. })();

QingJ © 2025

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