GitHub汉化插件

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

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

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

QingJ © 2025

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