// ==UserScript==
// @name Codeforces汉化
// @version 0.01
// @description 这是一个简单的Codeforces汉化脚本
// @author 北极小狐
// @match https://codeforces.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=codeforces.com
// @grant none
// @license MIT
// @namespace https://greasyfork.org/users/747162
// ==/UserScript==
(function() {
// 设置语言为zh
var htmlTag = document.getElementsByTagName("html")[0];
htmlTag.setAttribute("lang", "zh-CN");
// 定义classData,存放元素的class名和对应的替换规则
const classData = {
'.menu-list.main-menu-list': [
{ match: 'Help', replace: '帮助' },
{ match: 'Calendar', replace: '日历' },
{ match: 'Edu', replace: '培训' },
{ match: 'Rating', replace: '积分榜' },
{ match: 'Groups', replace: '团体' },
{ match: 'Problemset', replace: '题单' },
{ match: 'Gym', replace: '训练营(过去的大型比赛)' },
{ match: 'Contests', replace: '比赛' },
{ match: 'Catalog', replace: '指南目录' },
{ match: 'Top', replace: '热门' },
{ match: 'Home', replace: '主页' },
],
'.caption.titled': [
{ match: 'Pay attention', replace: '注意' },
{ match: 'Top contributors', replace: '贡献者排行' },
{ match: 'Find user', replace: '查找用户' },
{ match: 'Recent actions', replace: '最近热帖' },
{ match: 'Training filter', replace: '过滤筛选' },
{ match: 'Find training', replace: '搜索比赛/问题' },
],
'.personal-sidebar ': [
{ match: 'Contribution', replace: '贡献' },
{ match: 'Settings', replace: '设置' },
{ match: 'Blog', replace: '博客' },
{ match: 'Teams', replace: '队伍' },
{ match: 'Submissions', replace: '提交' },
{ match: 'Talks', replace: '私信' },
{ match: 'Contests', replace: '比赛' },
],
'.contest-state-phase': [
{ match: 'Before contest', replace: '即将进行的比赛' },
],
'.datatable': [
{ match: 'Virtual participation', replace: '参加模拟重现赛' },
{ match: 'Enter', replace: '进入' },
{ match: 'Final standings', replace: '榜单' },
{ match: 'School/University/City/', replace: '学校/大学/城市/区域比赛' },
{ match: 'Official ICPC Contest', replace: 'ICPC官方比赛' },
{ match: 'China', replace: '中国' },
{ match: 'Statements', replace: '题目描述' },
{ match: 'in Chinese', replace: '中文' },
],
'.roundbox.sidebox.borderTopRound ': [
{ match: 'Season:', replace: '时间范围(年度)' },
{ match: 'Contest type', replace: '比赛类型' },
{ match: 'ICPC region', replace: 'ICPC地区' },
{ match: 'Contest format', replace: '比赛形式' },
{ match: 'Duration, hours', replace: '持续时间(小时)' },
{ match: 'Difficulty', replace: '难度' },
{ match: 'Order by', replace: '排序方式' },
{ match: 'Secondary order by', replace: '次要排序方式' },
{ match: 'Hide, if participated', replace: '隐藏我参与过的' },
{ match: 'Hide excluded gyms', replace: '隐藏排除的比赛' },
],
'.icon-eye-close.icon-large': [
{ match: 'Add to exclusions', replace: '添加到排除列表' },
],
'.second-level-menu ': [
{ match: 'Problems', replace: '问题' },
{ match: 'Submit Code', replace: '提交代码' },
{ match: 'My Submissions', replace: '我的提交' },
{ match: 'Status', replace: '状态' },
{ match: 'Standings', replace: '榜单' },
{ match: 'Custom Invocation', replace: '代码调试' },
{ match: 'Common standings', replace: '全部排行' },
{ match: 'Friends standings', replace: '只看关注' },
],
'.topic-read-more': [
{ match: 'Full text and comments', replace: '阅读全文/评论' },
],
};
// 将所有class名与之符合的元素的text中包含的匹配的文字均替换为对应的文字
for (const className in classData) {
const elements = document.querySelectorAll(className);
elements.forEach((element) => {
let html = element.outerHTML; // 获取元素的html代码
const parent = element.parentNode;
const childIndex = Array.from(parent.children).indexOf(element);
classData[className].forEach((rule) => {
html = html.replace(new RegExp(rule.match, 'g'), rule.replace); // 将其中匹配的文字替换为对应的文字
const temp = document.createElement('div'); // 创建临时元素
temp.innerHTML = html.trim();
const newElement = temp.firstChild;
parent.replaceChild(newElement, parent.children[childIndex]); // 替换元素
});
});
}
})();