GitHub汉化插件

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

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

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

QingJ © 2025

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