Codeforces汉化

一个简单的Codeforces汉化脚本

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

  1. // ==UserScript==
  2. // @name Codeforces汉化
  3. // @version 0.03
  4. // @description 一个简单的Codeforces汉化脚本
  5. // @author 北极小狐
  6. // @match https://codeforces.com/*
  7. // @icon https://www.google.com/s2/favicons?sz=64&domain=codeforces.com
  8. // @grant none
  9. // @license MIT
  10. // @namespace https://greasyfork.org/users/747162
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. // 设置语言为zh
  15. var htmlTag = document.getElementsByTagName("html")[0];
  16. htmlTag.setAttribute("lang", "zh-CN");
  17.  
  18. // 定义classData,存放元素的class名和对应的替换规则
  19. const classData = {
  20. '.menu-list.main-menu-list': [
  21. { match: 'Help', replace: '帮助' },
  22. { match: 'Calendar', replace: '日历' },
  23. { match: 'Edu', replace: '培训' },
  24. { match: 'Rating', replace: '积分榜' },
  25. { match: 'Groups', replace: '团体' },
  26. { match: 'Problemset', replace: '题单' },
  27. { match: 'Gym', replace: '训练营(过去的大型比赛)' },
  28. { match: 'Contests', replace: '比赛' },
  29. { match: 'Catalog', replace: '指南目录' },
  30. { match: 'Top', replace: '热门' },
  31. { match: 'Home', replace: '主页' },
  32. ],
  33. '.caption.titled': [
  34. { match: 'Pay attention', replace: '注意' },
  35. { match: 'Top rated', replace: '积分排行' },
  36. { match: 'Top contributors', replace: '贡献者排行' },
  37. { match: 'Find user', replace: '查找用户' },
  38. { match: 'Recent actions', replace: '最近热帖' },
  39. { match: 'Training filter', replace: '过滤筛选' },
  40. { match: 'Find training', replace: '搜索比赛/问题' },
  41. { match: 'Virtual participation', replace: '什么是虚拟参赛' },
  42. { match: 'Contest materials', replace: '比赛相关资料' },
  43. { match: 'Settings', replace: '设置' },
  44. { match: 'Clone Contest to Mashup', replace: 'Clone 比赛到组合混搭' },
  45. ],
  46. '.personal-sidebar ': [
  47. { match: 'Contribution', replace: '贡献' },
  48. { match: 'Settings', replace: '设置' },
  49. { match: 'Blog', replace: '博客' },
  50. { match: 'Teams', replace: '队伍' },
  51. { match: 'Submissions', replace: '提交' },
  52. { match: 'Talks', replace: '私信' },
  53. { match: 'Contests', replace: '比赛' },
  54. ],
  55. '.contest-state-phase': [
  56. { match: 'Before contest', replace: '即将进行的比赛' },
  57. ],
  58. '.act-item': [
  59. { match: 'Add to favourites', replace: '添加到收藏' },
  60. { match: 'Submit', replace: '提交' },
  61. ],
  62. '.datatable': [
  63. { match: 'Virtual participation', replace: '参加虚拟重现赛' },
  64. { match: 'Enter', replace: '进入' },
  65. { match: 'Final standings', replace: '榜单' },
  66. { match: 'School/University/City/Region Championship', replace: '学校/大学/城市/区域比赛' },
  67. { match: 'Official School Contest', replace: '官方学校比赛' },
  68. { match: 'Training Contest', replace: '训练赛' },
  69. { match: 'Training Camp Contest', replace: '训练营比赛' },
  70. { match: 'Official ICPC Contest', replace: 'ICPC官方比赛' },
  71. { match: 'Official International Personal Contest', replace: '官方国际个人赛' },
  72. { match: 'China', replace: '中国' },
  73. { match: 'Statements', replace: '题目描述' },
  74. { match: 'in Chinese', replace: '中文' },
  75. { match: 'Trainings', replace: '训练' },
  76. { match: 'Prepared by', replace: '编写人' },
  77. { match: 'Current or upcoming contests', replace: '当前或即将举行的比赛' },
  78. { match: 'Past contests', replace: '过去的比赛' },
  79. { match: 'Exclusions', replace: '排除' },
  80. { match: 'Before start', replace: '还有' },
  81. { match: 'Before registration', replace: '报名还有' },
  82. { match: 'Until closing ', replace: '结束报名' },
  83. { match: 'Register', replace: '报名' },
  84. { match: 'Registration completed', replace: '已报名' },
  85. { match: 'Questions about problems', replace: '关于题目的提问' },
  86. ],
  87. '.ask-question-link': [
  88. { match: 'Ask a question', replace: '提一个问题' },
  89. ],
  90. '.contests-table': [
  91. { match: 'Contest history', replace: '比赛历史' },
  92. ],
  93. '.roundbox.sidebox.borderTopRound ': [
  94. { match: 'Season:', replace: '时间范围(年度)' },
  95. { match: 'Contest type', replace: '比赛类型' },
  96. { match: 'ICPC region', replace: 'ICPC地区' },
  97. { match: 'Contest format', replace: '比赛形式' },
  98. { match: 'Duration, hours', replace: '持续时间(小时)' },
  99. { match: 'Order by', replace: '排序方式' },
  100. { match: 'Secondary order by', replace: '次要排序方式' },
  101. { match: 'Hide, if participated', replace: '隐藏我参与过的' },
  102. { match: 'Hide excluded gyms', replace: '隐藏排除的比赛' },
  103. { match: 'Register now', replace: '现在报名' },
  104. { match: 'Show tags for unsolved problems', replace: '显示未解决问题的标签' },
  105. { match: 'Hide solved problems', replace: '隐藏已解决的问题' },
  106. ],
  107. '.icon-eye-close.icon-large': [
  108. { match: 'Add to exclusions', replace: '添加到排除列表' },
  109. ],
  110. '._ContestFilterExclusionsManageFrame_addExclusionLink': [
  111. { match: 'Add to exclusions for gym contests filter', replace: '添加训练营过滤器的排除项' },
  112. ],
  113. '.roundbox.sidebox.sidebar-menu.borderTopRound ': [
  114. { match: 'Statements', replace: '统计报表' },
  115. { match: 'Tutorial', replace: '题解' },
  116. ],
  117. '.second-level-menu ': [
  118. { match: 'Problems', replace: '问题' },
  119. { match: 'Submit Code', replace: '提交代码' },
  120. { match: 'My Submissions', replace: '我的提交' },
  121. { match: 'Status', replace: '状态' },
  122. { match: 'Standings', replace: '榜单' },
  123. { match: 'Custom Invocation', replace: '自定义调试' },
  124. { match: 'Common standings', replace: '全部排行' },
  125. { match: 'Friends standings', replace: '只看朋友' },
  126. { match: 'Submit', replace: '提交' },
  127. { match: 'Custom test', replace: '自定义测试' },
  128. { match: 'Blog', replace: '博客' },
  129. { match: 'Teams', replace: '队伍' },
  130. { match: 'Submissions', replace: '提交' },
  131. { match: 'Groups', replace: '团体' },
  132. { match: 'Contests', replace: '比赛' },
  133. { match: '问题etting', replace: '参与编写的问题' },
  134. { match: 'Gym', replace: '训练营' },
  135. { match: 'Mashups', replace: '组合混搭' },
  136. { match: 'Posts', replace: '帖子' },
  137. { match: 'Comments', replace: '回复' },
  138. { match: 'Main', replace: '主要' },
  139. { match: 'Settings', replace: '设置' },
  140. { match: 'Lists', replace: '列表' },
  141. ],
  142. '.topic-toggle-collapse': [
  143. { match: 'Expand', replace: '展开' },
  144. ],
  145. '.topic-read-more': [
  146. { match: 'Full text and comments', replace: '阅读全文/评论' },
  147. ],
  148. '.toggleEditorCheckboxLabel': [
  149. { match: 'Switch off editor', replace: '关闭编辑器语法高亮' },
  150. ],
  151. '.content-with-sidebar': [
  152. { match: 'Notice', replace: '注意' },
  153. { match: 'virtual participation', replace: '虚拟参与' },
  154. { match: 'Registration for the contest', replace: '比赛报名' },
  155. { match: 'Terms of agreement', replace: '协议条款' },
  156. { match: 'Take part', replace: '参与' },
  157. { match: 'as individual participant', replace: '作为个人参与者' },
  158. { match: 'as a team member', replace: '作为团队成员' },
  159. { match: 'Virtual start time', replace: '虚拟开始时间' },
  160. { match: 'Complete problemset', replace: '完整的问题集' },
  161. ],
  162. '.submit': [
  163. { match: 'Registration for the contest', replace: '比赛报名' },
  164. ],
  165. '.table-form': [
  166. { match: 'Problem', replace: '题目' },
  167. { match: 'Language', replace: '语言' },
  168. { match: 'Source code', replace: '源代码' },
  169. { match: 'Or choose file', replace: '或者选择文件' },
  170. ],
  171. };
  172.  
  173. // 将所有 class 名与之符合的元素的 text 中包含的匹配的文字均替换为对应的文字
  174. for (const className in classData) {
  175. const elements = document.querySelectorAll(className);
  176. elements.forEach((element) => {
  177. let html = element.outerHTML; // 获取元素的 html 代码
  178. const parent = element.parentNode;
  179. const childIndex = Array.from(parent.children).indexOf(element);
  180. let matched = false; // 标记该元素是否匹配了规则
  181. classData[className].forEach((rule) => {
  182. if (html.match(new RegExp(rule.match, 'g'))) {
  183. // 如果匹配成功,则将 matched 设置为 true
  184. matched = true;
  185. html = html.replace(new RegExp(rule.match, 'g'), rule.replace); // 将其中匹配的文字替换为对应的文字
  186. const temp = document.createElement('div'); // 创建临时元素
  187. temp.innerHTML = html.trim();
  188. const newElement = element.cloneNode(true);
  189. newElement.innerHTML = temp.firstChild.innerHTML;
  190. parent.replaceChild(newElement, parent.children[childIndex]); // 替换元素
  191. }
  192. });
  193. });
  194. }
  195.  
  196. })();