GitHub汉化插件

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

当前为 2020-07-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Internationalization
  3. // @name:zh-CN GitHub汉化插件
  4. // @namespace https://github.com/k1995/github-i18n-plugin/
  5. // @version 0.5
  6. // @description Translate GitHub.com
  7. // @description:zh GitHub汉化插件,包含人机翻译
  8. // @description:zh-CN GitHub汉化插件,包含人机翻译
  9. // @author k1995
  10. // @match https://github.com/*
  11. // @grant GM_xmlhttpRequest
  12. // @require https://gf.qytechs.cn/scripts/407481-github-i18n-plugin-locales-zh-cn/code/github-i18n-plugin-locales-zh-CN.js?version=830277
  13. // @require https://cdn.bootcdn.net/ajax/libs/timeago.js/4.0.2/timeago.full.min.js
  14. // @require http://code.jquery.com/jquery-2.1.1.min.js
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. translateByCssSelector();
  21. translateDesc();
  22. traverseElement(document.body);
  23. watchUpdate();
  24.  
  25. function translateRelativeTimeEl(el) {
  26. const datetime = $(el).attr('datetime');
  27. $(el).text(timeago.format(datetime, 'zh_CN'));
  28. }
  29.  
  30. function translateElement(el) {
  31. // Get the text field name
  32. let k;
  33. if(el.tagName === "INPUT") {
  34. if (el.type === 'button' || el.type === 'submit') {
  35. k = 'value';
  36. } else {
  37. k = 'placeholder';
  38. }
  39. } else {
  40. k = 'data';
  41. }
  42.  
  43. const txtSrc = el[k].trim();
  44. const key = txtSrc.toLowerCase()
  45. .replace(/\xa0/g, ' ') // replace ' '
  46. .replace(/\s{2,}/g, ' ');
  47.  
  48. if(locales.dict[key]) {
  49. el[k] = el[k].replace(txtSrc, locales.dict[key])
  50. }
  51. }
  52.  
  53. function shoudTranslateEl(el) {
  54. const blockIds = ["readme"];
  55. const blockTags = ["CODE", "SCRIPT", "LINK", "IMG", "svg"];
  56.  
  57. return !(el.id && blockIds.includes(el.id))
  58. && !(blockTags.includes(el.tagName));
  59. }
  60.  
  61. function traverseElement(el) {
  62. if(!shoudTranslateEl(el)) {
  63. return
  64. }
  65.  
  66. for(const child of el.childNodes) {
  67. if(el.tagName === "RELATIVE-TIME") {
  68. translateRelativeTimeEl(el);
  69. return;
  70. }
  71.  
  72. if(child.nodeType === Node.TEXT_NODE) {
  73. translateElement(child);
  74. }
  75. else if(child.nodeType === Node.ELEMENT_NODE) {
  76. if(child.tagName === "INPUT") {
  77. translateElement(child);
  78. } else {
  79. traverseElement(child);
  80. }
  81. } else {
  82. // pass
  83. }
  84. }
  85. }
  86.  
  87. function watchUpdate() {
  88. const m = window.MutationObserver || window.WebKitMutationObserver;
  89. const observer = new m(function (mutations, observer) {
  90. for(let mutationRecord of mutations) {
  91. for(let node of mutationRecord.addedNodes) {
  92. traverseElement(node);
  93. }
  94. }
  95. });
  96.  
  97. observer.observe(document.body, {
  98. subtree: true,
  99. characterData: true,
  100. childList: true,
  101. });
  102. }
  103.  
  104. // translate "about"
  105. function translateDesc() {
  106. $(".repository-content .f4").append("<br/>");
  107. $(".repository-content .f4").append("<a id='translate-me' href='#' style='color:rgb(27, 149, 224);font-size: small'>翻译</a>");
  108. $("#translate-me").click(function() {
  109. // get description text
  110. const desc = $(".repository-content .f4")
  111. .clone()
  112. .children()
  113. .remove()
  114. .end()
  115. .text()
  116. .trim();
  117.  
  118. if(!desc) {
  119. return;
  120. }
  121.  
  122. GM_xmlhttpRequest({
  123. method: "GET",
  124. url: `https://www.githubs.cn/translate?q=`+ encodeURIComponent(desc),
  125. onload: function(res) {
  126. if (res.status === 200) {
  127. $("#translate-me").hide();
  128. // render result
  129. const text = res.responseText;
  130. $(".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>");
  131. $(".repository-content .f4").append("<br/>");
  132. $(".repository-content .f4").append(text);
  133. } else {
  134. alert("翻译失败");
  135. }
  136. }
  137. });
  138. });
  139. }
  140.  
  141. function translateByCssSelector() {
  142. if(locales.css) {
  143. for(var css of locales.css) {
  144. if($(css.selector).length > 0) {
  145. if(css.key === '!html') {
  146. $(css.selector).html(css.replacement);
  147. } else {
  148. $(css.selector).attr(css.key, css.replacement);
  149. }
  150. }
  151. }
  152. }
  153. }
  154. })();

QingJ © 2025

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