Codeforces汉化

这是一个简单的Codeforces汉化脚本

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

  1. // ==UserScript==
  2. // @name Codeforces汉化
  3. // @version 0.01
  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 contributors', replace: '贡献者排行' },
  36. { match: 'Find user', replace: '查找用户' },
  37. { match: 'Recent actions', replace: '最近热帖' },
  38. { match: 'Training filter', replace: '过滤筛选' },
  39. { match: 'Find training', replace: '搜索比赛/问题' },
  40. ],
  41. '.personal-sidebar ': [
  42. { match: 'Contribution', replace: '贡献' },
  43. { match: 'Settings', replace: '设置' },
  44. { match: 'Blog', replace: '博客' },
  45. { match: 'Teams', replace: '队伍' },
  46. { match: 'Submissions', replace: '提交' },
  47. { match: 'Talks', replace: '私信' },
  48. { match: 'Contests', replace: '比赛' },
  49. ],
  50. '.contest-state-phase': [
  51. { match: 'Before contest', replace: '即将进行的比赛' },
  52. ],
  53. '.datatable': [
  54. { match: 'Virtual participation', replace: '参加模拟重现赛' },
  55. { match: 'Enter', replace: '进入' },
  56. { match: 'Final standings', replace: '榜单' },
  57. { match: 'School/University/City/', replace: '学校/大学/城市/区域比赛' },
  58. { match: 'Official ICPC Contest', replace: 'ICPC官方比赛' },
  59. { match: 'China', replace: '中国' },
  60. { match: 'Statements', replace: '题目描述' },
  61. { match: 'in Chinese', replace: '中文' },
  62. ],
  63. '.roundbox.sidebox.borderTopRound ': [
  64. { match: 'Season:', replace: '时间范围(年度)' },
  65. { match: 'Contest type', replace: '比赛类型' },
  66. { match: 'ICPC region', replace: 'ICPC地区' },
  67. { match: 'Contest format', replace: '比赛形式' },
  68. { match: 'Duration, hours', replace: '持续时间(小时)' },
  69. { match: 'Difficulty', replace: '难度' },
  70. { match: 'Order by', replace: '排序方式' },
  71. { match: 'Secondary order by', replace: '次要排序方式' },
  72. { match: 'Hide, if participated', replace: '隐藏我参与过的' },
  73. { match: 'Hide excluded gyms', replace: '隐藏排除的比赛' },
  74. ],
  75. '.icon-eye-close.icon-large': [
  76. { match: 'Add to exclusions', replace: '添加到排除列表' },
  77. ],
  78. '.second-level-menu ': [
  79. { match: 'Problems', replace: '问题' },
  80. { match: 'Submit Code', replace: '提交代码' },
  81. { match: 'My Submissions', replace: '我的提交' },
  82. { match: 'Status', replace: '状态' },
  83. { match: 'Standings', replace: '榜单' },
  84. { match: 'Custom Invocation', replace: '代码调试' },
  85. { match: 'Common standings', replace: '全部排行' },
  86. { match: 'Friends standings', replace: '只看关注' },
  87. ],
  88. '.topic-read-more': [
  89. { match: 'Full text and comments', replace: '阅读全文/评论' },
  90. ],
  91. };
  92.  
  93. // 将所有class名与之符合的元素的text中包含的匹配的文字均替换为对应的文字
  94. for (const className in classData) {
  95. const elements = document.querySelectorAll(className);
  96. elements.forEach((element) => {
  97. let html = element.outerHTML; // 获取元素的html代码
  98. const parent = element.parentNode;
  99. const childIndex = Array.from(parent.children).indexOf(element);
  100. classData[className].forEach((rule) => {
  101. html = html.replace(new RegExp(rule.match, 'g'), rule.replace); // 将其中匹配的文字替换为对应的文字
  102. const temp = document.createElement('div'); // 创建临时元素
  103. temp.innerHTML = html.trim();
  104. const newElement = temp.firstChild;
  105. parent.replaceChild(newElement, parent.children[childIndex]); // 替换元素
  106. });
  107. });
  108. }
  109. })();