C++// ==UserScript==
// @name Atcoder Better!
// @namespace https://greasyfork.org.cn/users/747162
// @version 1.20.0
// @description 一个适用于 AtCoder 的 Tampermonkey 脚本,增强功能与界面。
// @author 北极小狐
// @match *://atcoder.jp/*
// @run-at document-start
// @connect www2.deepl.com
// @connect api-free.deepl.com
// @connect api.deepl.com
// @connect api.deeplx.org
// @connect www.iflyrec.com
// @connect dict.youdao.com
// @connect api.interpreter.caiyunai.com
// @connect translate.google.com
// @connect openai.api2d.net
// @connect api.openai.com
// @connect www.luogu.com.cn
// @connect vjudge.net
// @connect clist.by
// @connect greasyfork.org.cn
// @connect sustech.edu.cn
// @connect aowuucdn.oss-cn-beijing.aliyuncs.com
// @connect aowuucdn.oss-accelerate.aliyuncs.com
// @connect 127.0.0.1
// @connect *
// @grant GM_xmlhttpRequest
// @grant GM_info
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_listValues
// @grant GM_deleteValue
// @grant GM_addStyle
// @grant GM_setClipboard
// @grant GM_getResourceText
// @icon https://aowuucdn.oss-accelerate.aliyuncs.com/atcoder.png
// @require https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/turndown/7.2.0/turndown.min.js#sha512-sJzEecN5Nk8cq81zKtGq6/z9Z/r3q38zV9enY75IVxiG7ybtlNUt864sL4L1Kf36bYIwxTMVKQOtU4VhD7hGrw==
// @require https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/markdown-it/13.0.2/markdown-it.js#sha512-2LtYcLGnCbAWz9nDIrfG2pHFiFu9n+3oGecQlzLuYsLgen/oxiYscGWnDST9J9EZanlsQkDD0ZP2n/6peDuALQ==
// @require https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/crypto-js/4.2.0/crypto-js.min.js#sha512-a+SUDuwNzXDvz4XrIcXHuCf089/iJAoN4lmrXJg18XnduKK6YlDHNRalv4yd1N40OKI80tFidF+rqTFKGPoWFQ==
// @require https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/chroma-js/2.4.2/chroma.min.js#sha512-zInFF17qBFVvvvFpIfeBzo7Tj7+rQxLeTJDmbxjBz5/zIr89YVbTNelNhdTT+/DCrxoVzBeUPVFJsczKbB7sew==
// @require https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/xterm/5.5.0/xterm.js#sha512-Gujw5GajF5is3nMoGv9X+tCMqePLL/60qvAv1LofUZTV9jK8ENbM9L+maGmOsNzuZaiuyc/fpph1KT9uR5w3CQ==
// @require https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/dexie/4.0.7/dexie.min.js#sha512-882VotT07mOQRzqIxsyxHzJX0XUaoeee3qXp4THg1A0KI0XFnWFAaLFQm0x6OW3pHSIipVZW+gzQ1w9b6uvkVw==
// @require https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/i18next/23.11.5/i18next.min.js#sha512-3RSGkmT48HnO+hlmzGYDx5/w2LIBX0O5hSuYX6KWAxmvVlSjFgoxIaWa2tlMExheGvt3lLyxeTsXfpC47yb8CQ==
// @require https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/i18next-http-backend/2.5.2/i18nextHttpBackend.min.js#sha512-bBb+wrGRTx4MvHpksYb1Iv5oJ1o8ineCqpc0cnTgdJQhuAFJJ93SEVXxUOCptvt0vAqYdjzWO5emorYUBt6Ceg==
// @require https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/jquery-i18next/1.2.1/jquery-i18next.min.js#sha512-79RgNpOyaf8AvNEUdanuk1x6g53UPoB6Fh2uogMkOMGADBG6B0DCzxc+dDktXkVPg2rlxGvPeAFKoZxTycVooQ==
// @require https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/highlight.js/11.9.0/highlight.min.js#sha512-D9gUyxqja7hBtkWpPWGt9wfbfaMGVt9gnyCvYa+jojwwPHLCzUm5i8rpk7vD7wNee9bA35eYIjobYPaQuKS1MQ==
// @require https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/dialog-polyfill/0.5.6/dialog-polyfill.min.js#sha512-qUIG93zKzcLBVD5RGRbx2PBmbVRu+tJIl+EPLTus0z8I1AMru9sQYdlf6cBacSzYmZVncB9rcc8rYBnazqgrxA==
// @resource acwing_cpp_code_completer https://aowuucdn.oss-accelerate.aliyuncs.com/acwing_cpp_code_completer-0.0.11.json#sha512-DQVpao4qMMExToRdid0g/S0nbO/C9hwCECjI5aW8A0g7nvi8hEcD2Lw3QIqdJBV7haP15oJOocfwuiw7ryTO9w==
// @resource wandboxlist https://wandbox.org/api/list.json
// @resource xtermcss https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/xterm/5.5.0/xterm.min.css#sha512-XpXUuzg5afNt1bsgnrOesXP70TLH8tXYYK5sK+Y0UV+YBvJn9EfRFYWy4HT3TVDfH0nl1CO0lwOxIrt2gk9qjg==
// @resource selectpagecss https://aowuucdn.oss-accelerate.aliyuncs.com/css/selectpage.css#sha512-cRXJfA2tEcAxHEKylJfxteY17N7j9fia3waahHOVnvl63uVZT9OQ7jjjpofZMVZ4JSX3BRET+mI8UvKnsXd3NA==
// @resource dialogpolyfillcss https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/dialog-polyfill/0.5.6/dialog-polyfill.min.css#sha512-J2+1q+RsZuJXabBfH1q/fgRr6jMy9By5SwVLk7bScEW7NFJkMUXxfeOyyxtDe6fsaJ4jsciexSlGrPYn9YbBIg==
// @license GPL3
// @compatible Chrome
// @compatible Firefox
// @compatible Edge
// @incompatible safari
// @supportURL https://github.com/beijixiaohu/OJBetter/issues
// @name:zh-TW AtCoder Better!
// @name:en AtCoder Better!
// @name:de AtCoder Better!
// @name:fr AtCoder Better!
// @name:ko AtCoder Better!
// @name:pt AtCoder Better!
// @name:ja AtCoder Better!
// @name:es AtCoder Better!
// @name:it AtCoder Better!
// @name:hi AtCoder Better!
// @description 一个适用于 AtCoder 的 Tampermonkey 脚本,增强功能与界面。
// @description:zh-TW 一個適用於 AtCoder 的 Tampermonkey 腳本,增強功能與界面。
// @description:en A Tampermonkey script for AtCoder that enhances functionality and interface.
// @description:de Ein Tampermonkey-Skript für AtCoder, das Funktionalität und Benutzeroberfläche verbessert.
// @description:fr Un script Tampermonkey pour AtCoder qui améliore les fonctionnalités et l'interface.
// @description:ko AtCoder를 위한 Tampermonkey 스크립트로 기능과 인터페이스를 개선합니다.
// @description:pt Um script Tampermonkey para AtCoder que aprimora a funcionalidade e a interface.
// @description:ja AtCoder用のTampermonkeyスクリプトで機能とインターフェースを強化します。
// @description:es Un script Tampermonkey para AtCoder que mejora la funcionalidad y la interfaz.
// @description:it Uno script Tampermonkey per AtCoder che migliora la funzionalità e l'interfaccia.
// @description:hi AtCoder के लिए एक Tampermonkey स्क्रिप्ट जो कार्यक्षमता और इंटरफ़ेस को बेहतर बनाता है।
// @downloadURL https://update.greasyfork.org.cn/scripts/471106/Atcoder%20Better%21.user.js
// @updateURL https://update.greasyfork.org.cn/scripts/471106/Atcoder%20Better%21.meta.js
// ==/UserScript==
/**
* @namespace OJBetter
* @desc 主命名空间
*/
const OJBetter = {};
/**
* @namespace state
* @desc 描述脚本的当前状态。
* @memberof OJBetter
*/
OJBetter.state = {
/** @type {string} 脚本名*/
name: GM_info.script.name,
/** @type {string} 格式化后的脚本名*/
formatName: undefined,
/** @type {string} 版本号*/
version: GM_info.script.version,
/** @type {boolean?} 是否跳过页面加载等待 */
notWaiteLoaded: undefined,
/** @type {string} 最后公告版本,用于标识版本更新完成提示 */
lastAnnounceVer: undefined,
/** @type {string} 最后读取的有效公告版本 */
lastReadAnnounceVer: undefined,
/** @type {number} 当前已打开的模态对话框数量*/
openDialogCount: 0
};
/**
* @namespace common
* @desc 通用设置和属性。
* @memberof OJBetter
*/
OJBetter.common = {
/** @type {string} 网站的主机地址 */
hostAddress: location.origin,
/** @type {string} 网站当前真实的黑暗模式 */
realDarkMode: undefined,
/** @type {string?} AtCoder的CSRF令牌 */
at_csrf_token: undefined,
/** @type {Array?} 任务队列 */
taskQueue: undefined,
/** @type {object} OJBetter数据库连接实例*/
database: undefined,
/** @type {object} turndownService实例*/
turndownService: undefined,
};
/**
* @namespace basic
* @desc 基本的用户界面设置。
* @memberof OJBetter
*/
OJBetter.basic = {
/** @type {string} 黑暗模式设置 */
darkMode: undefined,
/** @type {boolean?} 是否展开折叠块 */
expandFoldingblocks: undefined,
/** @type {boolean?} 是否开启折叠块渲染性能优化 */
renderPerfOpt: undefined,
/** @type {boolean?} 是否开启下拉选择框性能优化 */
selectElementPerfOpt: undefined,
/** @type {boolean?} 评论区分页 */
commentPaging: undefined,
/** @type {boolean?} 显示跳转到Luogu按钮 */
showJumpToLuogu: undefined,
/** @type {boolean?} 显示跳转到Virtual Judge按钮 */
showCF2vjudge: undefined,
/** @type {boolean?} 比赛排行榜重新着色 */
standingsRecolor: undefined
};
/**
* @namespace typeOfPage
* @desc 页面类型判断。
* @memberof OJBetter
*/
OJBetter.typeOfPage = {
/** @type {boolean?} 是否是轻量站 */
is_mSite: false,
/** @type {boolean?} 是否是acmsguru页面 */
is_acmsguru: false,
/** @type {boolean?} 是否是旧版LaTeX页面 */
is_oldLatex: false,
/** @type {boolean?} 是否是题目集页面 */
is_contest: undefined,
/** @type {boolean?} 是否是题目页面 */
is_problem: undefined,
/** @type {boolean?} 是否是完整的问题集页面 */
is_completeProblemset: false,
/** @type {boolean?} 是否是问题集中的问题页面 */
is_problemset_problem: false,
/** @type {boolean?} 是否是问题集页面 */
is_problemset: false,
/** @type {boolean?} 是否是Codeforces排名页面 */
is_cfStandings: false,
/** @type {boolean?} 是否是提交页面 */
is_submitPage: false,
/** @type {boolean?} 是否是代码状态页面 */
is_statePage: false,
/** @type {boolean?} 是否是提交记录页面 */
is_submissions: false,
/** @type {boolean?} 是否是主页 */
is_homepage: undefined,
/** @type {boolean?} 是否选择的是英语页面 */
isEnglishLanguage: undefined,
/** @type {boolean?} 是否是题解页面 */
isEditorial: undefined,
};
/**
* @namespace localization
* @desc 本地化设置。
* @memberof OJBetter
*/
OJBetter.localization = {
/** @type {string?} 网站语言 */
websiteLang: undefined,
/** @type {string?} 脚本语言 */
scriptLang: undefined
};
/**
* @namespace translation
* @desc 翻译设置。
* @memberof OJBetter
*/
OJBetter.translation = {
/** @type {string?} 翻译服务选择 */
choice: undefined,
/** @type {string?} 目标语言 */
targetLang: undefined,
comment: {
/** @type {string?} 评论翻译服务选择 */
choice: undefined,
/** @type {string?} 评论翻译模式 */
transMode: undefined
},
auto: {
/** @type {boolean?} 自动翻译开关 */
enabled: undefined,
/** @type {number?} 短文本长度限制 */
shortTextLength: undefined,
mixTrans: {
/** @type {boolean?} 混合翻译开关 */
enabled: undefined,
/** @type {Array?} 混合翻译服务列表 */
servers: undefined
}
},
memory: {
/** @type {boolean?} 翻译记忆开关 */
enabled: undefined,
/** @type {Object?} 翻译记忆树 */
ttTree: undefined
},
/** @type {string?} 重翻译时的行为 */
retransAction: undefined,
/** @type {number?} 等待时间 */
waitTime: undefined,
/** @type {boolean?} 替换符 */
replaceSymbol: undefined,
/** @type {boolean?} 过滤文本中的*号 */
filterTextWithoutEmphasis: undefined
};
/**
* @namespace clist
* @desc Clist相关设置。
* @memberof OJBetter
*/
OJBetter.clist = {
enabled: {
/** @type {boolean?} 比赛页面开关 */
contest: undefined,
/** @type {boolean?} 问题页面开关 */
problem: undefined,
/** @type {boolean?} 问题集页面开关 */
problemset: undefined
},
/** @type {boolean?} Rating数据防剧透 */
ratingHidden: undefined,
/** @type {string?} Clist key */
authorization: undefined
};
/**
* @namespace monaco
* @desc Monaco编辑器配置。
* @memberof OJBetter
*/
OJBetter.monaco = {
/** @type {boolean?} 在问题页面上启用Monaco编辑器 */
enableOnProblemPage: undefined,
/** @type {boolean?} 美化pre代码块 */
beautifyPreBlocks: undefined,
/** @type {boolean} Monaco编辑器加载完成标志 */
loaderOnload: false,
lsp: {
/** @type {Array?} LSP套接字数组 */
socket: [],
/** @type {boolean?} 是否启用LSP */
enabled: undefined,
/** @type {string?} 工作路径 */
workUri: undefined,
/** @type {string?} 套接字URL */
socketUrl: undefined
},
complet: {
/** @type {boolean?} 是否启用C++代码补全模板 */
cppCodeTemplate: undefined,
/** @type {Object?} 自定义配置 */
customConfig: undefined
},
/** @type {Object?} Monaco编辑器实例 */
editor: null,
/** @type {Array?} 代码块美化的Monaco编辑器实例 */
beautifyEditor: [],
/** @type {string?} 在线编译器选择 */
onlineCompilerChoice: undefined,
/** @type {string?} 记忆编译器语言选择 */
compilerSelection: undefined,
/** @type {string?} 当前选择的语言 */
nowLangSelect: undefined,
setting: {
/** @type {Array?} 语言设置数组 */
language: [],
/** @type {string?} 位置 */
position: undefined,
/** @type {boolean} 位置初始化标志 */
position_initialized: false,
/** @type {number?} 字体大小 */
fontsize: undefined,
/** @type {boolean?} 鼠标滚动锁定 */
alwaysConsumeMouseWheel: undefined,
/** @type {boolean?} 提交代码二次确认 */
isCodeSubmitDoubleConfirm: undefined,
/** @type {boolean?} 测试通过后自动提交 */
autoSubmitAfterPass: undefined,
/** @type {string?} 提交按钮位置 */
submitButtonPosition: undefined,
/** @type {boolean?} 自动保存代码 */
autoMemoryCode: undefined
}
};
/**
* @namespace deepl
* @desc DeepL翻译服务配置。
* @memberof OJBetter
*/
OJBetter.deepl = {
/** @type {Object?} DeepL配置对象 */
configs: undefined,
config: {
/** @type {string?} 类型 */
type: undefined,
/** @type {string?} 名称 */
name: undefined,
/** @type {string?} API类型 */
apiGenre: undefined,
/** @type {string?} API密钥 */
key: undefined,
/** @type {string?} 代理 */
proxy: undefined,
/** @type {Object?} 额外请求头 */
header: undefined,
/** @type {Object?} 额外请求数据 */
data: undefined,
quota: {
/** @type {string?} 余额URL */
url: undefined,
/** @type {string?} 余额请求方法 */
method: undefined,
/** @type {Object?} 余额请求头 */
header: undefined,
/** @type {Object?} 余额请求数据 */
data: undefined,
/** @type {number?} 剩余配额 */
surplus: undefined
}
},
/** @type {boolean?} 启用重点保护 */
enableEmphasisProtection: undefined,
/** @type {boolean?} 启用链接保护 */
enableLinkProtection: undefined
};
/**
* @namespace chatgpt
* @desc ChatGPT服务配置。
* @memberof OJBetter
*/
OJBetter.chatgpt = {
/** @type {Object?} ChatGPT配置对象 */
configs: undefined,
config: {
/** @type {string?} 名称 */
name: undefined,
/** @type {string?} 模型 */
model: undefined,
/** @type {string?} API密钥 */
key: undefined,
/** @type {string?} 代理 */
proxy: undefined,
/** @type {Object?} 额外请求头 */
header: undefined,
/** @type {Object?} 额外请求数据 */
data: undefined,
quota: {
/** @type {string?} 余额URL */
url: undefined,
/** @type {string?} 余额请求方法 */
method: undefined,
/** @type {Object?} 余额请求头 */
header: undefined,
/** @type {Object?} 余额请求数据 */
data: undefined,
/** @type {number?} 剩余配额 */
surplus: undefined
}
},
/** @type {boolean?} 是否为流式传输 */
isStream: undefined,
/** @type {string?} 是否使用自定义Prompt */
customPrompt: undefined,
/** @type {boolean?} 是否作为系统Prompt */
asSystemPrompt: undefined
};
/**
* @namespace preference
* @desc 偏好设置
* @memberof OJBetter
*/
OJBetter.preference = {
/** @type {boolean?} 是否显示加载动画 */
showLoading: undefined,
/** @type {boolean?} 是否显示悬停目标区域 */
hoverTargetAreaDisplay: undefined,
/** @type {string?} 按钮图标大小 */
iconButtonSize: undefined,
/** @type {boolean?} 是否显示同比赛题目列表*/
showSameContestProblems: undefined,
/** @type {string?} 翻译文本颜色 */
TranslateTextColor: undefined
};
/**
* @namespace dev
* @desc 维护
* @memberof OJBetter
*/
OJBetter.dev = {
/** @type {boolean?} 是否显示规则标记 */
isRuleMarkingEnabled: undefined,
};
/**
* @namespace about
* @desc 关于页信息
* @memberof OJBetter
*/
OJBetter.about = {
/** @type {string?} 更新通道 */
updateChannel: undefined,
/** @type {string?} 更新源 */
updateSource: undefined
};
/**
* @namespace supportList
* @desc 支持列表
* @memberof OJBetter
*/
OJBetter.supportList = {
/** @type {object} 翻译支持列表和对应语言代码*/
translationSupport: {
'deepl': { 'zh': 'ZH', 'de': 'DE', 'fr': 'FR', 'ko': 'KO', 'pt': 'PT', 'ja': 'JA', 'es': 'ES', 'it': 'IT' },
'iflyrec': { 'zh': '1' },
'youdao': { 'zh': 'zh-CHS', 'zh-Hant': 'zh-CHT', 'de': 'de', 'fr': 'fr', 'ko': 'ko', 'pt': 'pt', 'ja': 'ja', 'es': 'es', 'it': 'it', 'hi': 'hi' },
'google': { 'zh': 'zh-CN', 'zh-Hant': 'zh-TW', 'de': 'de', 'fr': 'fr', 'ko': 'ko', 'pt': 'pt', 'ja': 'ja', 'es': 'es', 'it': 'it', 'hi': 'hi' },
'caiyun': { 'zh': 'auto2zh', 'ja': 'auto2ja', 'ko': 'auto2ko', 'es': 'auto2es', 'fr': 'auto2fr' },
'openai': { 'zh': 'Chinese', 'zh-Hant': 'Traditional Chinese', 'de': 'German', 'fr': 'French', 'ko': 'Korean', 'pt': 'Portuguese', 'ja': 'Japanese', 'es': 'Spanish', 'it': 'Italian', 'hi': 'Hindi' }
},
/** @type {object} 更新源支持列表*/
updateSourceSupportList: {
'greasyfork': {
'release': true,
'dev': false
},
'github': {
'release': true,
'dev': true
},
'aliyunoss': {
'release': true,
'dev': true
}
}
}
// ------------------------------
// 一些工具函数
// ------------------------------
/**
* 延迟函数
* @param {number} ms 延迟时间(毫秒)
* @returns {Promise<void>}
*/
function OJB_delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* 等待直到指定的条件函数返回true。
*
* @param {() => boolean} conditionCheck 一个无参数的函数,用于检查条件是否满足。当函数返回true时,表示条件已满足。
* @param {number} [interval=100] 检查条件的间隔时间,单位为毫秒。默认为100毫秒。
* @returns {Promise<void>} 返回一个Promise,在条件满足时解决。
*/
async function OJB_waitUntilTrue(conditionCheck, interval = 100) {
return new Promise((resolve) => {
const checkCondition = async () => {
if (conditionCheck()) {
resolve();
} else {
await OJB_delay(interval);
checkCondition();
}
};
checkCondition();
});
}
/**
* 动态加载JavaScript库并返回一个Promise,该Promise在脚本加载完成后解决。
*
* @param {string} url - 要加载的JavaScript库的URL地址。
* @param {string} [expectedHash] - 可选的Base64编码的SHA-512哈希值,用于校验脚本内容。格式为 "sha512-<Base64编码的哈希值>"。
* @returns {Promise<void>} 一个Promise,它在脚本加载并执行完成后解决。
*/
async function OJB_LoadJS(url, expectedHash) {
/**
* 计算给定数据的SHA-512哈希值,并将其转换为十六进制字符串。
*
* @param {string} data - 要计算哈希值的数据。
* @returns {Promise<string>} 一个Promise,它解析为数据的SHA-512哈希值的十六进制字符串。
*/
const calculateHash = async (data) => {
const encoder = new TextEncoder();
const dataBuffer = encoder.encode(data);
const hashBuffer = await crypto.subtle.digest('SHA-512', dataBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
};
/**
* 将Base64编码的字符串转换为十六进制字符串。
*
* @param {string} base64 - Base64编码的字符串。
* @returns {string} 转换后的十六进制字符串。
*/
const base64ToHex = (base64) => {
const binaryString = atob(base64);
const byteArray = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
byteArray[i] = binaryString.charCodeAt(i);
}
return Array.from(byteArray).map(b => b.toString(16).padStart(2, '0')).join('');
};
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`Failed to fetch script: ${response.statusText}`);
const scriptContent = await response.text();
if (expectedHash) {
// 去掉前缀 "sha512-"
const base64Hash = expectedHash.replace(/^sha512-/, '');
const actualHash = await calculateHash(scriptContent);
const expectedHashHex = base64ToHex(base64Hash);
if (actualHash !== expectedHashHex) throw new Error('SHA-512 hash mismatch');
}
const scriptElement = document.createElement("script");
scriptElement.textContent = scriptContent;
document.head.prepend(scriptElement);
return Promise.resolve();
} catch (error) {
return Promise.reject(error);
}
}
/**
* 安全地创建JQuery对象
* @description 通过字符串创建JQuery对象时,如果字符串以空格开头,在某些Jquery版本中会发生错误,过滤空格以安全的创建元素。
* @param {string} string - 字符串。
* @returns {JQuery} JQuery对象
*/
const OJB_safeCreateJQElement = function (string) {
return $(string.replace(/^\s+/, ""));
}
/**
* 将数字或者字符串解析为数字。
* @memberof OJBetter.common
* @param {string} val 要解析的字符串
* @param {boolean} [strict=false] 是否进行严格类型检查
* @returns {number} 解析结果
* @throws {Error} 如果解析失败,则抛出错误
*/
const OJB_parseNumber = (val, strict = false) => {
const num = Number(val);
if (isNaN(num) || (strict && val.toString() !== num.toString())) {
throw new Error('Invalid number');
}
return num;
};
/**
* 将字符串解析为布尔值
* @param {string} val - 要解析的字符串
* @param {boolean} strict - 是否进行严格类型检查
* @returns {boolean} - 解析结果
* @throws {Error} - 如果解析失败,则抛出错误
*/
const OJB_parseBoolean = (val, strict) => {
if (strict) {
if (val === true || val === false) return val;
throw new Error('Invalid boolean');
}
return val === 'true' ? true : val === 'false' ? false : val;
};
/**
* 将字符串解析为对象
* @param {string} val - 要解析的字符串
* @returns {Object} - 解析结果
* @throws {Error} - 如果解析失败,则抛出错误
*/
const OJB_parseObject = val => {
try {
return JSON.parse(val);
} catch {
throw new Error('Invalid JSON');
}
};
/**
* 将字符串解析为键值对数组
* @param {string} val - 要解析的字符串
* @returns {Object[]} - 解析结果
* @throws {Error} - 如果解析失败,则抛出错误
*/
const OJB_parseLinePairArray = val => {
if (typeof val !== 'string' || val.trim() === '') return [];
return val.split("\n").filter(line => line.trim() !== '').map(line => {
const indexOfFirstColon = line.indexOf(":");
if (indexOfFirstColon === -1) throw new Error('Invalid LinePairArray format: ":" is missing');
const key = line.substring(0, indexOfFirstColon).trim();
const value = line.substring(indexOfFirstColon + 1).trim();
return { [key]: value };
});
};
/**
* 移除文本中的HTML标签
* @param {string} text - 包含HTML标签的文本
* @returns {string} - 移除HTML标签后的文本
*/
const OJB_removeHTMLTags = function (text) {
return text.replace(/<\/?[a-zA-Z]+("[^"]*"|'[^']*'|[^'">])*>/g, '');
}
/**
* 解码被转义的字符串为普通字符
* @param {string} text - 包含 <、> 的字符串
* @returns {string} - 解码后的字符串
*/
const OJB_unescapeHtml = (function() {
const textarea = document.createElement("textarea");
return function(text) {
textarea.innerHTML = text;
return textarea.value;
};
})();
/**
* 获取对象中指定路径表达式的值
* @param {Object} obj - 要计算的对象
* @param {string} pathOrExpression - 要计算的路径表达式
* @returns {any} - 计算结果
* @example
* const obj = {
* "a": {
* "b": 1
* },
* "c": 2
* };
* OJB_evaluatePathOrExpression(obj, "a.b"); // 1
* OJB_evaluatePathOrExpression(obj, "a.b + c"); // 3
* OJB_evaluatePathOrExpression(obj, "a.b + a.c"); // 1
*/
function OJB_evaluatePathOrExpression(obj, pathOrExpression) {
const hasOperator = /[\+\-\*\/]/.test(pathOrExpression);
const getPathValue = (obj, path) => {
return path.split('.').reduce((acc, part) => {
return acc !== undefined && acc !== null && acc.hasOwnProperty(part) ? acc[part] : undefined;
}, obj);
};
const evaluateExpression = (obj, expression) => {
const tokens = expression.split(/([\+\-\*\/])/).map(token => token.trim());
const values = tokens.map(token => {
if (/[\+\-\*\/]/.test(token)) {
return token;
} else {
const value = getPathValue(obj, token);
return value !== undefined ? value : 0;
}
});
const evaluatedExpression = values.join(' ');
try {
return Function(`'use strict'; return (${evaluatedExpression});`)();
} catch (e) {
console.error('Expression evaluation error:', e);
return undefined;
}
};
return hasOperator ? evaluateExpression(obj, pathOrExpression) : getPathValue(obj, pathOrExpression);
}
/**
* 获取 GM 存储的值并根据类型进行处理
* @param {string} key - 要检索的值的键。
* @param {any} defaultValue - 如果值未找到,则返回的默认值。
* @param {Object} [options={}] - 配置选项对象。
* @param {string} [options.type='string'] - 期望的值的类型。可选值:'string', 'number', 'boolean', 'object', 'array', 'linePairArray'。
* @param {boolean} [options.strict=false] - 用于数字和布尔类型,表示是否进行严格类型检查。
* @param {string} [options.pathOrExpression=''] - 用于对象或数组类型,表示路径表达式或获取元素的索引。
* @returns {any} - 检索到的值。
*/
const OJB_getGMValue = (key, defaultValue, { type = 'string', strict = false, pathOrExpression = '' } = {}) => {
let value = GM_getValue(key);
if (value === undefined || value === null || value === "") {
GM_setValue?.(key, defaultValue);
return defaultValue;
}
const parsers = {
string: val => val,
number: (val) => OJB_parseNumber(val, strict),
boolean: (val) => OJB_parseBoolean(val, strict),
object: OJB_parseObject,
array: OJB_parseObject,
linePairArray: OJB_parseLinePairArray
};
if (!(type in parsers)) {
console.error(`Unsupported type: ${type}`);
return defaultValue;
}
try {
value = parsers[type](value);
} catch (e) {
console.error('Error:', e.message);
return defaultValue;
}
// The pathOrExpression processing is not applicable to linePairArray type
if ((type === 'object' || type === 'array') && pathOrExpression) {
const evaluated = OJB_evaluatePathOrExpression(value, pathOrExpression);
if (evaluated === undefined) {
console.error('Path or expression evaluation returned undefined');
return defaultValue;
}
value = evaluated;
}
return value;
};
/**
* 版本号比较方法
* @param {string} version1 版本号1
* @param {string} version2 版本号2
* @returns {number} -1: version1 < version2, 0: version1 = version2, 1: version1 > version2
*/
const OJB_compareVersions = function (version1 = "0", version2 = "0") {
const v1Array = version1.split(".").map(Number);
const v2Array = version2.split(".").map(Number);
const length = Math.max(v1Array.length, v2Array.length);
for (let i = 0; i < length; i++) {
const diff = (v1Array[i] || 0) - (v2Array[i] || 0);
if (diff) return Math.sign(diff);
}
return 0;
}
/**
* 获取上一个主版本号
* @param {string} currentVersion 当前版本号
* @returns {string} 上一个主版本号
*/
const OJB_getPreviousVersion = function (currentVersion) {
const versionArray = currentVersion.split(".").map(Number);
let lastNonZeroIndex = versionArray.length - 1;
while (lastNonZeroIndex >= 0 && versionArray[lastNonZeroIndex] === 0) {
lastNonZeroIndex--;
}
if (lastNonZeroIndex >= 0) {
versionArray[lastNonZeroIndex]--;
for (let i = lastNonZeroIndex + 1; i < versionArray.length; i++) {
versionArray[i] = 0;
}
}
return versionArray.join(".");
};
/**
* 在指定根节点下观察指定选择器的元素,当元素存在时,执行回调函数
* @param {Object} options - 配置对象
* @param {string} options.selector - CSS选择器文本
* @param {Function} options.callback - 回调函数,接收变动的节点作为参数
* @param {Boolean} [options.triggerOnExist=true] - 如果为true,元素已存在时立即触发一次回调
* @param {Element} [options.root=document.body] - 在哪个根节点下监听变化
* @param {Boolean} [options.subtree=false] - 是否监听子树变化(即非直接子元素)
*/
function OJB_observeElement({
selector,
callback,
triggerOnExist = true,
root = document.body,
subtree = false
}) {
// 尝试获取选择器指定的元素
const targetNode = root.querySelector(selector);
if (targetNode) {
// 如果元素已存在,直接开始观察
observeAndReport(targetNode, callback);
// 如果triggerOnExist为true,则立即触发一次回调
if (triggerOnExist) {
callback(targetNode);
}
} else {
// 如果元素不存在,监听DOM变化直到该元素被添加
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE && node.matches(selector)) {
observeAndReport(node, callback);
if (triggerOnExist) {
callback(node);
}
observer.disconnect(); // 停止监听
}
});
});
});
observer.observe(root, { childList: true, subtree, attributes: false });
}
function observeAndReport(node, callback) {
const childObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((addedNode) => {
if (addedNode.nodeType === Node.ELEMENT_NODE) {
callback(addedNode); // 执行回调函数
}
});
});
});
childObserver.observe(node, { childList: true, subtree: true, attributes: false });
}
}
/**
* 初始化全局变量
*/
async function initVar() {
const { hostname, href } = window.location;
OJBetter.state.formatName = (() => OJBetter.state.name
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^a-z0-9-]/g, ''))();
OJBetter.state.lastAnnounceVer = OJB_getGMValue("lastAnnounceVer", "0");
OJBetter.state.lastReadAnnounceVer = OJB_getGMValue("lastReadAnnounceVer", "0");
OJBetter.typeOfPage.is_contest = /\/contests\/[^\/]+\/tasks\/?$/.test(href);
OJBetter.typeOfPage.is_problem = href.includes('/tasks/');
OJBetter.typeOfPage.is_homepage = (href === 'https://atcoder.jp/' || href === 'https://atcoder.jp/?lang=ja');
OJBetter.typeOfPage.isEnglishLanguage = $('meta[http-equiv="Content-Language"]').attr('content') === 'en';
OJBetter.typeOfPage.isEditorial = href.includes("editorial");
OJBetter.localization.websiteLang = OJB_getGMValue("localizationLanguage", "zh");
OJBetter.localization.scriptLang = OJB_getGMValue("scriptL10nLanguage", "zh");
OJBetter.basic.renderPerfOpt = OJB_getGMValue("renderPerfOpt", false);
OJBetter.basic.selectElementPerfOpt = OJB_getGMValue("selectElementPerfOpt", true);
OJBetter.basic.commentPaging = OJB_getGMValue("commentPaging", true);
OJBetter.basic.showJumpToLuogu = OJB_getGMValue("showJumpToLuogu", true);
OJBetter.basic.showCF2vjudge = OJB_getGMValue("showCF2vjudge", true);
OJBetter.basic.standingsRecolor = OJB_getGMValue("standingsRecolor", true);
OJBetter.state.notWaiteLoaded = OJB_getGMValue("notWaiteLoaded", false);
OJBetter.translation.targetLang = OJB_getGMValue("transTargetLang", "zh");
OJBetter.translation.choice = OJB_getGMValue("translation", "deepl");
OJBetter.translation.comment.transMode = OJB_getGMValue("commentTranslationMode", "0");
OJBetter.translation.comment.choice = OJB_getGMValue("commentTranslationChoice", "0");
OJBetter.translation.memory.enabled = OJB_getGMValue("memoryTranslateHistory", true);
OJBetter.translation.auto.enabled = OJB_getGMValue("autoTranslation", false);
OJBetter.translation.auto.shortTextLength = OJB_getGMValue("shortTextLength", "2000");
OJBetter.translation.retransAction = OJB_getGMValue("retransAction", "0");
OJBetter.translation.waitTime = OJB_getGMValue("transWaitTime", "200");
OJBetter.translation.auto.mixTrans.enabled = OJB_getGMValue("allowMixTrans", true);
OJBetter.translation.auto.mixTrans.servers = OJB_getGMValue("mixedTranslation", ['deepl', 'iflyrec', 'youdao', 'caiyun']);
OJBetter.common.taskQueue = new TaskQueue();
OJBetter.translation.replaceSymbol = OJB_getGMValue("replaceSymbol", "2");
OJBetter.translation.filterTextWithoutEmphasis = OJB_getGMValue("filterTextWithoutEmphasis", false);
OJBetter.clist.enabled.contest = OJB_getGMValue("showClistRating_contest", false);
OJBetter.clist.enabled.problem = OJB_getGMValue("showClistRating_problem", false);
OJBetter.clist.enabled.problemset = OJB_getGMValue("showClistRating_problemset", false);
OJBetter.clist.ratingHidden = OJB_getGMValue("RatingHidden", false);
OJBetter.clist.authorization = OJB_getGMValue("clist_Authorization", "");
//deepl
OJBetter.deepl.config.type = OJB_getGMValue("deepl_type", "free");
OJBetter.deepl.configs = OJB_getGMValue("deepl_config", {
"choice": "",
"configurations": []
});
if (OJBetter.deepl.configs.choice !== "" && OJBetter.deepl.configs.configurations.length !== 0) {
const choice = OJBetter.deepl.configs.choice;
const configuration = OJBetter.deepl.configs.configurations.find(obj => obj.name === choice);;
if (configuration == undefined) {
let existingConfig = GM_getValue('deepl_config');
existingConfig.choice = "";
GM_setValue('deepl_config', existingConfig);
location.reload();
}
OJBetter.deepl.config.name = configuration.name;
OJBetter.deepl.config.apiGenre = configuration.apiGenre;
OJBetter.deepl.config.key = configuration.key;
OJBetter.deepl.config.proxy = configuration.proxy;
OJBetter.deepl.config.header = OJB_parseLinePairArray(configuration._header);
OJBetter.deepl.config.data = OJB_parseLinePairArray(configuration._data);
OJBetter.deepl.config.quota.url = configuration.quota_url;
OJBetter.deepl.config.quota.method = configuration.quota_method;
OJBetter.deepl.config.quota.header = OJB_parseLinePairArray(configuration.quota_header);
OJBetter.deepl.config.quota.data = OJB_parseLinePairArray(configuration.quota_data);
OJBetter.deepl.config.quota.surplus = configuration.quota_surplus;
}
OJBetter.deepl.enableEmphasisProtection = OJB_getGMValue("enableEmphasisProtection", true);
OJBetter.deepl.enableLinkProtection = OJB_getGMValue("enableLinkProtection", true);
//openai
OJBetter.chatgpt.isStream = OJB_getGMValue("openai_isStream", true);
OJBetter.chatgpt.customPrompt = OJB_getGMValue("openai_customPrompt", '');
OJBetter.chatgpt.asSystemPrompt = OJB_getGMValue("openai_asSystemPrompt", false);
OJBetter.chatgpt.configs = OJB_getGMValue("chatgpt_config", {
"choice": "",
"configurations": []
});
if (OJBetter.chatgpt.configs.choice !== "" && OJBetter.chatgpt.configs.configurations.length !== 0) {
const choice = OJBetter.chatgpt.configs.choice;
const configuration = OJBetter.chatgpt.configs.configurations.find(obj => obj.name === choice);;
if (configuration == undefined) {
let existingConfig = GM_getValue('chatgpt_config');
existingConfig.choice = "";
GM_setValue('chatgpt_config', existingConfig);
location.reload();
}
OJBetter.chatgpt.config.name = configuration.name;
OJBetter.chatgpt.config.model = configuration.model;
OJBetter.chatgpt.config.key = configuration.key;
OJBetter.chatgpt.config.proxy = configuration.proxy;
OJBetter.chatgpt.config.header = OJB_parseLinePairArray(configuration._header);
OJBetter.chatgpt.config.data = OJB_parseLinePairArray(configuration._data);
OJBetter.chatgpt.config.quota.url = configuration.quota_url;
OJBetter.chatgpt.config.quota.method = configuration.quota_method;
OJBetter.chatgpt.config.quota.header = OJB_parseLinePairArray(configuration.quota_header);
OJBetter.chatgpt.config.quota.data = OJB_parseLinePairArray(configuration.quota_data);
OJBetter.chatgpt.config.quota.surplus = configuration.quota_surplus;
}
// 编辑器
// if (!OJBetter.typeOfPage.is_mSite) OJBetter.common.cf_csrf_token = Codeforces.getCsrfToken();
// else OJBetter.common.cf_csrf_token = "";
OJBetter.common.at_csrf_token = csrfToken;
// OJBetter.monaco.compilerSelection = OJB_getGMValue("compilerSelection", "61");
OJBetter.monaco.compilerSelection = OJB_getGMValue("compilerSelection", "5001");
OJBetter.monaco.setting.fontsize = OJB_getGMValue("editorFontSize", "15");
OJBetter.monaco.enableOnProblemPage = OJB_getGMValue("problemPageCodeEditor", true);
OJBetter.monaco.beautifyPreBlocks = OJB_getGMValue("beautifyPreBlocks", true);
OJBetter.monaco.complet.cppCodeTemplate = OJB_getGMValue("cppCodeTemplateComplete", true);
OJBetter.monaco.onlineCompilerChoice = OJB_getGMValue("onlineCompilerChoice", "official");
OJBetter.monaco.setting.isCodeSubmitDoubleConfirm = OJB_getGMValue("isCodeSubmitConfirm", true);
OJBetter.monaco.setting.autoSubmitAfterPass = OJB_getGMValue("autoSubmitAfterPass", false);
OJBetter.monaco.setting.alwaysConsumeMouseWheel = OJB_getGMValue("alwaysConsumeMouseWheel", true);
OJBetter.monaco.setting.submitButtonPosition = OJB_getGMValue("submitButtonPosition", "bottom");
OJBetter.monaco.setting.autoMemoryCode = OJB_getGMValue("autoMemoryCode", true);
//自定义补全
OJBetter.monaco.complet.customConfig = OJB_getGMValue("Complet_config", {
"choice": -1,
"configurations": []
});
// monaco
OJBetter.monaco.lsp.enabled = OJB_getGMValue("useLSP", false);
OJBetter.monaco.setting.position = OJB_getGMValue("monacoEditor_position", "initial");
OJBetter.monaco.lsp.workUri = OJB_getGMValue("OJBetter_Bridge_WorkUri", "C:/OJBetter_Bridge");
OJBetter.monaco.lsp.socketUrl = OJB_getGMValue("OJBetter_Bridge_SocketUrl", "ws://127.0.0.1:2323/");
OJBetter.preference.showLoading = OJB_getGMValue("showLoading", true);
OJBetter.preference.hoverTargetAreaDisplay = OJB_getGMValue("hoverTargetAreaDisplay", false);
OJBetter.preference.TranslateTextColor = OJB_getGMValue("TranslateTextColor","");
OJBetter.preference.showSameContestProblems = OJB_getGMValue("showSameContestProblems", false);
OJBetter.basic.expandFoldingblocks = OJB_getGMValue("expandFoldingblocks", true);
OJBetter.preference.iconButtonSize = OJB_getGMValue("iconButtonSize", "16");
OJBetter.dev.isRuleMarkingEnabled = OJB_getGMValue("isRuleMarkingEnabled", false);
OJBetter.about.updateChannel = OJB_getGMValue("updateChannel", "release");
OJBetter.about.updateSource = OJB_getGMValue("updateSource", "aliyunoss");
}
/**
* 显示警告消息
*/
function showWarnMessage() {
if (OJBetter.typeOfPage.is_oldLatex) {
const loadingMessage = new LoadingMessage();
loadingMessage.updateStatus(`${OJBetter.state.name} —— ${i18next.t('warning.is_oldLatex', { ns: 'alert' })}`, 'warning');
}
if (OJBetter.typeOfPage.is_acmsguru) {
const loadingMessage = new LoadingMessage();
loadingMessage.updateStatus(`${OJBetter.state.name} —— ${i18next.t('warning.is_acmsguru', { ns: 'alert' })}`, 'warning');
}
if (OJBetter.translation.comment.transMode == "1") {
const loadingMessage = new LoadingMessage();
loadingMessage.updateStatus(`${OJBetter.state.name} —— ${i18next.t('warning.trans_segment', { ns: 'alert' })}`, 'warning');
}
if (OJBetter.translation.comment.transMode == "2") {
const loadingMessage = new LoadingMessage();
loadingMessage.updateStatus(`${OJBetter.state.name} —— ${i18next.t('warning.trans_select', { ns: 'alert' })}`, 'warning');
}
if (OJBetter.typeOfPage.is_submitPage && OJBetter.monaco.enableOnProblemPage) {
const loadingMessage = new LoadingMessage();
loadingMessage.updateStatus(`${OJBetter.state.name} —— ${i18next.t('warning.is_submitPage', { ns: 'alert' })}`, 'warning');
}
}
// 常量
const helpCircleHTML = '<div class="help-icon"><svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path fill="currentColor" d="M512 64a448 448 0 1 1 0 896 448 448 0 0 1 0-896zm23.744 191.488c-52.096 0-92.928 14.784-123.2 44.352-30.976 29.568-45.76 70.4-45.76 122.496h80.256c0-29.568 5.632-52.8 17.6-68.992 13.376-19.712 35.2-28.864 66.176-28.864 23.936 0 42.944 6.336 56.32 19.712 12.672 13.376 19.712 31.68 19.712 54.912 0 17.6-6.336 34.496-19.008 49.984l-8.448 9.856c-45.76 40.832-73.216 70.4-82.368 89.408-9.856 19.008-14.08 42.24-14.08 68.992v9.856h80.96v-9.856c0-16.896 3.52-31.68 10.56-45.76 6.336-12.672 15.488-24.64 28.16-35.2 33.792-29.568 54.208-48.576 60.544-55.616 16.896-22.528 26.048-51.392 26.048-86.592 0-42.944-14.08-76.736-42.24-101.376-28.16-25.344-65.472-37.312-111.232-37.312zm-12.672 406.208a54.272 54.272 0 0 0-38.72 14.784 49.408 49.408 0 0 0-15.488 38.016c0 15.488 4.928 28.16 15.488 38.016A54.848 54.848 0 0 0 523.072 768c15.488 0 28.16-4.928 38.72-14.784a51.52 51.52 0 0 0 16.192-38.72 51.968 51.968 0 0 0-15.488-38.016 55.936 55.936 0 0 0-39.424-14.784z"></path></svg></div>';
const closeIcon = `<svg t="1696693011050" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4322" width="14" height="14"><path d="M0 0h1024v1024H0z" fill-opacity="0" p-id="4323"></path><path d="M240.448 168l2.346667 2.154667 289.92 289.941333 279.253333-279.253333a42.666667 42.666667 0 0 1 62.506667 58.026666l-2.133334 2.346667-279.296 279.210667 279.274667 279.253333a42.666667 42.666667 0 0 1-58.005333 62.528l-2.346667-2.176-279.253333-279.253333-289.92 289.962666a42.666667 42.666667 0 0 1-62.506667-58.005333l2.154667-2.346667 289.941333-289.962666-289.92-289.92a42.666667 42.666667 0 0 1 57.984-62.506667z" p-id="4324"></path></svg>`;
const translateIcon = `<svg t="1696837407077" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6325" width="22" height="22"><path d="M536.380952 121.904762a73.142857 73.142857 0 0 1 73.142858 73.142857v219.428571h219.428571a73.142857 73.142857 0 0 1 73.142857 73.142858v341.333333a73.142857 73.142857 0 0 1-73.142857 73.142857H487.619048a73.142857 73.142857 0 0 1-73.142858-73.142857v-219.428571H195.047619a73.142857 73.142857 0 0 1-73.142857-73.142858V195.047619a73.142857 73.142857 0 0 1 73.142857-73.142857h341.333333zM243.809524 682.666667v97.523809h97.523809v73.142857h-97.523809a73.142857 73.142857 0 0 1-73.142857-73.142857v-97.523809h73.142857z m585.142857-195.047619h-219.428571v48.761904a73.142857 73.142857 0 0 1-73.142858 73.142858h-48.761904v219.428571h341.333333V487.619048z m-115.760762 89.526857L787.21219 780.190476h-62.025142l-14.043429-42.715428h-76.068571L620.739048 780.190476h-60.854858l74.605715-203.044571h78.701714z m-38.034286 50.029714h-3.510857l-21.065143 63.488h45.348572l-20.772572-63.488zM536.380952 195.047619H195.047619v341.333333h341.333333V195.047619z
m-195.072 49.883429l44.78781 1.072762v37.278476h87.698286v145.359238h-87.698286v65.974857h-44.78781v-65.974857h-87.698285v-145.359238h87.698285v-38.351238z m0 83.139047h-44.787809v56.05181h44.787809v-56.05181z m89.307429 0h-44.519619v56.05181h44.519619v-56.05181zM780.190476 170.666667a73.142857 73.142857 0 0 1 73.142857 73.142857v97.523809h-73.142857v-97.523809h-97.523809V170.666667h97.523809z" p-id="6326"></path></svg>`;
const clistIcon = `<svg width="37.7pt" height="10pt" viewBox="0 0 181 48" version="1.1" xmlns="http://www.w3.org/2000/svg"><g id="#0057b8ff"><path fill="#0057b8" opacity="1.00" d=" M 17.36 0.00 L 18.59 0.00 C 23.84 6.49 30.28 11.92 36.01 17.98 C 34.01 19.99 32.01 21.99 30.00 23.99 C 26.02 19.97 22.02 15.98 18.02 11.99 C 14.01 15.98 10.01 19.99 6.00 23.99 C 4.16 22.04 2.30 20.05 0.00 18.61 L 0.00 17.37 C 3.44 15.11 6.00 11.84 8.96 9.03 C 11.79 6.05 15.09 3.47 17.36 0.00 Z" /></g><g id="#a0a0a0ff"><path fill="#a0a0a0" opacity="1.00" d=" M 56.76 13.74 C 61.48 4.80 76.07 3.90 81.77 12.27 C 83.09 13.94 83.44 16.10 83.91 18.12 C 81.53 18.23 79.16 18.24 76.78 18.23 C 75.81 15.72 73.99 13.31 71.14 12.95 C 67.14 12.02 63.45 15.29 62.48 18.99 C 61.30 23.27 61.71 28.68 65.34 31.70 C 67.82 34.05 72.19 33.93 74.61 31.55 C 75.97 30.18 76.35 28.23 76.96 26.48 C 79.36 26.43 81.77 26.44 84.17 26.56 C 83.79 30.09 82.43 33.49 79.89 36.02 C 74.14 41.35 64.17 40.80 58.77 35.25 C 53.52 29.56 53.18 20.38 56.76 13.74 Z" />
<path fill="#a0a0a0" opacity="1.00" d=" M 89.01 7.20 C 91.37 7.21 93.74 7.21 96.11 7.22 C 96.22 15.71 96.10 24.20 96.18 32.69 C 101.25 32.76 106.32 32.63 111.39 32.79 C 111.40 34.86 111.41 36.93 111.41 39.00 C 103.94 39.00 96.47 39.00 89.00 39.00 C 89.00 28.40 88.99 17.80 89.01 7.20 Z" /><path fill="#a0a0a0" opacity="1.00" d=" M 115.00 7.21 C 117.33 7.21 119.66 7.21 121.99 7.21 C 122.01 17.81 122.00 28.40 122.00 39.00 C 119.67 39.00 117.33 39.00 115.00 39.00 C 115.00 28.40 114.99 17.80 115.00 7.21 Z" /><path fill="#a0a0a0" opacity="1.00" d=" M 133.35 7.47 C 139.11 5.56 146.93 6.28 150.42 11.87 C 151.42 13.39 151.35 15.31 151.72 17.04 C 149.33 17.05 146.95 17.05 144.56 17.03 C 144.13 12.66 138.66 11.12 135.34 13.30 C 133.90 14.24 133.54 16.87 135.35 17.61 C 139.99 20.02 145.90 19.54 149.92 23.19 C 154.43 26.97 153.16 35.36 147.78 37.72 C 143.39 40.03 137.99 40.11 133.30 38.69 C 128.80 37.34 125.34 32.90 125.91 28.10 C 128.22 28.10 130.53 28.11 132.84 28.16 C 132.98 34.19 142.68 36.07 145.18 30.97 C 146.11 27.99 142.17 27.05 140.05 26.35 C 135.54 25.04 129.83 24.33 127.50 19.63 C 125.30 14.78 128.42 9.00 133.35 7.47 Z" />
<path fill="#a0a0a0" opacity="1.00" d=" M 153.31 7.21 C 161.99 7.21 170.67 7.21 179.34 7.21 C 179.41 9.30 179.45 11.40 179.48 13.50 C 176.35 13.50 173.22 13.50 170.09 13.50 C 170.05 21.99 170.12 30.48 170.05 38.98 C 167.61 39.00 165.18 39.00 162.74 39.00 C 162.64 30.52 162.73 22.04 162.69 13.55 C 159.57 13.49 156.44 13.49 153.32 13.50 C 153.32 11.40 153.31 9.31 153.31 7.21 Z" /></g><g id="#ffd700ff"><path fill="#ffd700" opacity="1.00" d=" M 12.02 29.98 C 14.02 27.98 16.02 25.98 18.02 23.98 C 22.01 27.99 26.03 31.97 30.00 35.99 C 34.01 31.99 38.01 27.98 42.02 23.99 C 44.02 25.98 46.02 27.98 48.01 29.98 C 42.29 36.06 35.80 41.46 30.59 48.00 L 29.39 48.00 C 24.26 41.42 17.71 36.08 12.02 29.98 Z" /></g></svg>`;
/**
* 连接数据库
*/
async function initDB() {
OJBetter.common.database = new Dexie('OJBetterDB');
OJBetter.common.database.version(3).stores({
samplesData: '&url',
editorCode: '&url',
translateData: '&url',
localizeSubsData: '&lang'
});
// 等待数据库打开
await OJBetter.common.database.open();
}
/**
* 清空数据库
*/
async function clearDatabase() {
const isConfirmed = await OJB_createDialog(
i18next.t('isClearDatabase.title', { ns: 'dialog' }),
i18next.t('isClearDatabase.content', { ns: 'dialog' }),
[
i18next.t('isClearDatabase.buttons.0', { ns: 'dialog' }),
i18next.t('isClearDatabase.buttons.1', { ns: 'dialog' })
]
);
if (!isConfirmed) {
try {
// 开启一个读写事务,包含数据库中的所有表
await OJBetter.common.database.transaction('rw', OJBetter.common.database.tables, async () => {
// 遍历所有表
for (const table of OJBetter.common.database.tables) {
// 清空当前表
await table.clear();
}
});
console.log("All tables in the database have been cleared.");
alert("All tables in the database have been cleared.");
} catch (error) {
console.error("Error clearing the database:", error);
}
}
}
/**
* 导出数据库
* @returns {Promise<string>} 数据库的JSON字符串
*/
async function exportDatabase() {
try {
// 创建一个存储数据的对象
const exportData = {};
// 获取数据库中所有表的名称
const tableNames = OJBetter.common.database.tables.map(table => table.name);
// 遍历每一个表,获取数据
for (const tableName of tableNames) {
const tableData = await OJBetter.common.database.table(tableName).toArray();
exportData[tableName] = tableData;
}
// 将数据对象转换为JSON字符串
const jsonData = JSON.stringify(exportData, null, 4);
return jsonData;
} catch (error) {
console.error("Error exporting database:", error);
}
}
/**
* 导入数据库
* @param {string} jsonData 数据库的JSON字符串
*/
async function importDatabase(jsonData) {
const isConfirmed = await OJB_createDialog(
i18next.t('isImportDatabase.title', { ns: 'dialog' }),
i18next.t('isImportDatabase.content', { ns: 'dialog' }),
[
i18next.t('isImportDatabase.buttons.0', { ns: 'dialog' }),
i18next.t('isImportDatabase.buttons.1', { ns: 'dialog' })
]
);
if (!isConfirmed) {
try {
// 将JSON字符串解析为对象
const importData = JSON.parse(jsonData);
// 开启一个事务,并清空现有数据
await OJBetter.common.database.transaction('rw', OJBetter.common.database.tables, async () => {
// 清空所有表的数据
for (const tableName of OJBetter.common.database.tables.map(table => table.name)) {
await OJBetter.common.database.table(tableName).clear();
}
// 插入新数据
for (const [tableName, rows] of Object.entries(importData)) {
await OJBetter.common.database.table(tableName).bulkAdd(rows);
}
});
alert("Data imported successfully");
} catch (error) {
console.error("Error importing database:", error);
}
}
}
/**
* 将数据下载为文件
* @param {string} data 数据
* @param {string} filename 文件名,默认为'export.json'
* @param {string} fileType 文件MIME类型,默认为'application/json'
* @returns {void}
*/
function downloadDataAsFile(data, filename = 'export.json', fileType = 'application/json') {
// 创建一个blob对象,指定文件类型
const blob = new Blob([data], { type: fileType });
const url = URL.createObjectURL(blob);
// 创建一个隐藏的a标签,模拟点击进行下载
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
// 清理
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
/**
* 从文件中读取数据
* @param {Function} callback 回调函数
* @returns {void}
*/
function readFileInput(callback) {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '.json';
fileInput.style.display = 'none'; // 隐藏input元素
fileInput.onchange = (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const fileContent = e.target.result;
if (callback && typeof callback === 'function') {
callback(fileContent); // 调用回调函数,传入文件内容
}
};
reader.readAsText(file);
}
};
document.body.appendChild(fileInput);
fileInput.click();
document.body.removeChild(fileInput);
}
/**
* 清除所有设置
*/
async function deleteAllConfigSettings() {
const isConfirmed = await OJB_createDialog(
i18next.t('isDeleteAllConfigSettings.title', { ns: 'dialog' }),
i18next.t('isDeleteAllConfigSettings.content', { ns: 'dialog' }),
[
i18next.t('isDeleteAllConfigSettings.buttons.0', { ns: 'dialog' }),
i18next.t('isDeleteAllConfigSettings.buttons.1', { ns: 'dialog' })
]
);
if (!isConfirmed) {
const keys = GM_listValues();
keys.forEach(key => {
GM_deleteValue(key);
});
alert('All settings have been deleted.');
window.location.reload();
}
}
/**
* 导出设置到JSON
* @returns {string} JSON字符串
*/
function exportSettingsToJSON() {
const keys = GM_listValues();
let settings = {};
keys.forEach(key => {
settings[key] = GM_getValue(key);
});
return JSON.stringify(settings, null, 4);
}
/**
* 从JSON导入设置
* @param {string} jsonData JSON字符串
* @returns {void}
*/
async function importSettingsFromJSON(jsonData) {
const isConfirmed = await OJB_createDialog(
i18next.t('isImportSettings.title', { ns: 'dialog' }),
i18next.t('isImportSettings.content', { ns: 'dialog' }),
[
i18next.t('isImportSettings.buttons.0', { ns: 'dialog' }),
i18next.t('isImportSettings.buttons.1', { ns: 'dialog' })
]
);
if (!isConfirmed) {
let settings;
try {
settings = JSON.parse(jsonData);
} catch (e) {
console.error('JSON parsing error:', e);
return;
}
Object.keys(settings).forEach(key => {
GM_setValue(key, settings[key]);
});
alert('Settings imported successfully!');
window.location.reload();
}
}
/**
* 加载元素本地化语言数据
* @param {JQuery} element jQuery元素
* @param {number} [retries=10] 重试次数
* @param {number} [interval=50] 重试间隔
*/
function elementLocalize(element, retries = 10, interval = 50) {
if ($.isFunction(element.localize)) {
element.localize();
} else if (retries > 0) {
setTimeout(elementLocalize, interval, element, retries - 1, interval);
} else {
console.error('Unable to localize', element);
}
}
// 切换系统黑暗监听
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)');
const changeEventListeners = [];
/**
* 处理颜色模式变化事件
* @param {MediaQueryListEvent} event - 媒体查询事件对象
*/
const handleColorSchemeChange = (event) => {
const theme = event.matches ? 'dark' : 'light';
// 更新页面主题
$('html').attr('data-theme', theme);
OJBetter.common.realDarkMode = theme;
const updateMonacoTheme = (theme) => {
const intervalId = setInterval(() => {
if (OJBetter?.monaco?.editor) {
monaco.editor.setTheme(theme);
clearInterval(intervalId);
}
}, 100);
OJBetter.monaco.beautifyEditor.forEach((editor) => {
editor.updateOptions({ theme });
});
};
if (event.matches) {
updateMonacoTheme('vs-dark');
} else {
const originalColor = $(this).data("original-color");
$(this).css("background-color", originalColor);
updateMonacoTheme('vs');
}
};
// 黑暗模式
(function setDark() {
// 初始化
function setDarkTheme() {
const htmlElement = document.querySelector('html');
if (htmlElement) {
htmlElement.setAttribute('data-theme', 'dark');
const intervalId = setInterval(() => {
if (OJBetter.monaco && OJBetter.monaco.editor) {
monaco.editor.setTheme('vs-dark');
clearInterval(intervalId);
}
}, 100);
} else {
setTimeout(setDarkTheme, 100);
}
}
// 设置黑暗模式和监听器
OJBetter.basic.darkMode = OJB_getGMValue("darkMode", "follow");
if (OJBetter.basic.darkMode == "dark") {
OJBetter.common.realDarkMode = 'dark';
setDarkTheme();
} else if (OJBetter.basic.darkMode == "light") {
OJBetter.common.realDarkMode = 'light';
} else if (OJBetter.basic.darkMode == "follow") {
OJBetter.common.realDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
// 添加事件监听器
changeEventListeners.push(handleColorSchemeChange);
mediaQueryList.addEventListener('change', handleColorSchemeChange);
if (window.matchMedia('(prefers-color-scheme: dark)').matches) setDarkTheme();
}
// 定义全局变量
GM_addStyle(`
/* 黑暗支持 */
html[data-theme=dark]:root {
color-scheme: light dark;
}
/* 颜色 */
:root {
/* 文字颜色 */
--ojb-color-text-primary: #a0adb9; /* 主要文字颜色 */
--ojb-color-text-secondary: #9AA4B1; /* 次要文字颜色 */
--ojb-color-text-tertiary: #9BA5B2; /* 第三级文字颜色 */
--ojb-color-text-success: #43A047; /* 成功状态文字颜色 */
--ojb-color-text-highlight: #cbd6e2; /* 高亮文字颜色 */
--ojb-color-text-disabled: #506778; /* 禁用状态文字颜色 */
--ojb-color-text-icon-success: #2e7d32; /* 成功状态图标颜色 */
--ojb-color-text-link: #4b8eda; /* 链接颜色 */
/* 背景颜色 */
--ojb-color-bg-primary: #22272e; /* 主背景颜色 */
--ojb-color-bg-secondary: #2d333b; /* 次级背景颜色 */
--ojb-color-bg-disabled: #24292e; /* 禁用元素背景颜色 */
/* 边框颜色 */
--ojb-color-border-primary: #48535F; /* 主要边框颜色 */
--ojb-color-border-disabled: #404950; /* 禁用状态边框颜色 */
--ojb-color-border-dashed-hover: #03A9F4; /* 虚线边框悬浮颜色 */
--ojb-color-border-radio-checked: #326154; /* 选中的单选框边框颜色 */
/* 阴影颜色 */
--ojb-shadow-standard: 0px 0px 0.5px 0.5px #3A4048; /* 标准阴影 */
--ojb-shadow-menu-modal: 0px 0px 0px 4px #2d333b; /* 菜单和模态框阴影 */
/* 区域遮罩颜色 */
--ojb-overlay-background: repeating-linear-gradient(135deg, #49525f6e, #49525f6e 30px, #49525f29 0px, #49525f29 55px); /* 区域遮罩背景 */
/* 文字阴影 */
--ojb-text-shadow-icon: 1px 1px 0px #2d333b, 1px -1px 0px #2d333b, -1px -1px 0px #2d333b, -1px 1px 0px #2d333b; /* 图标文字阴影 */
}
/* 边框样式 */
:root {
/* 边框样式 */
--ojb-border-width: 1px; /* 边框宽度 */
--ojb-border-style-solid: solid; /* 实线样式 */
--ojb-border-style-dashed: dashed; /* 虚线样式 */
--ojb-border-radius-small: 4px; /* 小圆角 */
--ojb-border-radius-medium: 8px; /* 中圆角 */
--ojb-border-radius-large: 12px; /* 大圆角 */
/* 组合边框样式 */
--ojb-border-solid-primary: var(--ojb-border-width) var(--ojb-border-style-solid) var(--ojb-color-border-primary); /* 主要实线边框 */
--ojb-border-dashed: var(--ojb-border-width) var(--ojb-border-style-dashed) var(--ojb-color-border-primary); /* 主要虚线边框 */
--ojb-border-dashed-hover: var(--ojb-border-width) var(--ojb-border-style-dashed) var(--ojb-color-border-dashed-hover); /* 悬浮虚线边框 */
--ojb-border-solid-disabled: var(--ojb-border-width) var(--ojb-border-style-solid) var(--ojb-color-border-disabled); /* 禁用状态实线边框 */
}
`);
// OJBetter界面样式
GM_addStyle(`
/* 主要文字颜色 */
html[data-theme=dark] .alert-success, html[data-theme=dark] .alert-info, html[data-theme=dark] .alert-error,
html[data-theme=dark] .alert-warning, html[data-theme=dark] .markItUpEditor,
html[data-theme=dark] .translate-problem-statement, html[data-theme=dark] .OJBetter_setting_menu,
html[data-theme=dark] .help_tip .tip_text,
html[data-theme=dark] .OJBetter_setting_menu input, html[data-theme=dark] .OJBetter_setting_menu textarea,
html[data-theme=dark] #OJBetter_SubmitForm input, html[data-theme=dark] #OJBetter_SubmitForm textarea, html[data-theme=dark] #OJBetter_SubmitForm select,
html[data-theme=dark] #items-per-page, html[data-theme=dark] #pagBar,
html[data-theme=dark] .OJBetter_setting_sidebar li a:link,
html[data-theme=dark] .popup .content{
color: var(--ojb-color-text-primary);
}
/* 次要文字颜色 */
html[data-theme=dark] .ojb_btn:hover, html[data-theme=dark] .OJBetter_modal button, html[data-theme=dark] #OJBetter_statusBar,
html[data-theme=dark] #RunTestButton, html[data-theme=dark] #programTypeId, html[data-theme=dark] #addCustomTest,
html[data-theme=dark] #customTestBlock, html[data-theme=dark] .OJBetter_setting_list.alert_info{
color: var(--ojb-color-text-secondary);
}
/* 文字颜色3 */
html[data-theme=dark] .ojb_btn{
color: var(--ojb-color-text-tertiary);
}
/* 文字颜色 浅绿 */
html[data-theme=dark] #SubmitButton{
color: var(--ojb-color-text-success);
}
/* 禁止文字颜色 */
html[data-theme=dark] .ojb_btn[disabled]{
color: var(--ojb-color-text-disabled);
}
/* 主要背景层次 */
html[data-theme=dark] .OJBetter_setting_menu, html[data-theme=dark] .help_tip .tip_text, html[data-theme=dark] li#add_button:hover,
html[data-theme=dark] .ojb_btn:hover,
html[data-theme=dark] .OJBetter_setting_menu input, html[data-theme=dark] .OJBetter_setting_menu textarea,
html[data-theme=dark] #OJBetter_SubmitForm input,
html[data-theme=dark] .OJBetter_setting_menu input[type="checkbox"], html[data-theme=dark] .OJBetter_setting_menu input[type="checkbox"]:checked,
html[data-theme=dark] #OJBetter_SubmitForm textarea, html[data-theme=dark] #OJBetter_SubmitForm select,
html[data-theme=dark] .OJBetter_setting_sidebar li a.active, html[data-theme=dark] .OJBetter_setting_sidebar li,
html[data-theme=dark] .OJBetter_setting_menu::-webkit-scrollbar-track, html[data-theme=dark] .OJBetter_setting_content::-webkit-scrollbar-track,
html[data-theme=dark] .OJBetter_modal, html[data-theme=dark] .OJBetter_modal button:hover,
html[data-theme=dark] .popup .content,
html[data-theme=dark] .config_bar_list, html[data-theme=dark] #LSPLog,
html[data-theme=dark] .OJBetter_setting_menu .OJBetter_checkboxs,
html[data-theme=dark] .OJBetter_setting_menu .OJBetter_checkboxs input[type="checkbox"]::before,
html[data-theme=dark] .OJBetter_setting_menu a, html[data-theme=dark] .OJBetter_setting_menu .OJBetter_setting_list button:hover,
html[data-theme=dark] .OJBetter_setting_menu select{
background-color: var(--ojb-color-bg-primary);
background-image: none;
}
/* 次要背景层次 */
html[data-theme=dark] .ojb_btn,
html[data-theme=dark] .alert-success, html[data-theme=dark] .alert-info, html[data-theme=dark] .alert-error,
html[data-theme=dark] .alert-warning, html[data-theme=dark] .SumoSelect>.optWrapper>.options li.opt:hover,
html[data-theme=dark] .translate-problem-statement-panel,
html[data-theme=dark] .translate-problem-statement,
html[data-theme=dark] .OJBetter_setting_list,
html[data-theme=dark] .OJBetter_setting_menu hr,
html[data-theme=dark] .OJBetter_setting_sidebar li a,
html[data-theme=dark] .OJBetter_setting_menu::-webkit-scrollbar-thumb, html[data-theme=dark] .OJBetter_setting_content::-webkit-scrollbar-thumb,
html[data-theme=dark] .OJBetter_modal button, html[data-theme=dark] .test-for-popup pre,
html[data-theme=dark] .popup .content pre, html[data-theme=dark] .popup .content pre code,
html[data-theme=dark] ul.config_bar_ul::-webkit-scrollbar-thumb, html[data-theme=dark] #OJBetter_statusBar,
html[data-theme=dark] #RunTestButton, html[data-theme=dark] #programTypeId, html[data-theme=dark] .sampleDiv,
html[data-theme=dark] #addCustomTest, html[data-theme=dark] #LSPLog li:nth-child(odd),
html[data-theme=dark] .OJBetter_setting_menu .OJBetter_checkboxs input[type="checkbox"]:checked::before,
html[data-theme=dark] .config::before, html[data-theme=dark] .config li.tempConfig_add_button:hover,
html[data-theme=dark] .OJBetter_setting_menu details, html[data-theme=dark] #config_bar_menu,
html[data-theme=dark] .OJBetter_setting_menu .OJBetter_setting_list button,
html[data-theme=dark] .OJBetter_setting_menu .badge, html[data-theme=dark] #OJBetter_SubmitForm #SubmitButton{
background-color: var(--ojb-color-bg-secondary);
}
/* 禁止背景层次 */
html[data-theme=dark] .ojb_btn[disabled]{
background-color: var(--ojb-color-bg-disabled);
}
/* 实线边框颜色-圆角 */
html[data-theme=dark] .alert-success, html[data-theme=dark] .alert-info, html[data-theme=dark] .alert-error,
html[data-theme=dark] .alert-warning, html[data-theme=dark] .translate-problem-statement{
border: var(--ojb-border-solid-primary);
border-radius: 2px;
}
/* 实线边框颜色-无圆角 */
html[data-theme=dark] .ojb_btn,
html[data-theme=dark] .OJBetter_setting_list, html[data-theme=dark] .config_bar_list,
html[data-theme=dark] label.config_bar_ul_li_text,
html[data-theme=dark] .OJBetter_setting_sidebar li, html[data-theme=dark] .OJBetter_setting_menu select,
html[data-theme=dark] .translate-problem-statement-panel, html[data-theme=dark] .OJBetter_modal button, html[data-theme=dark] #OJBetter_SubmitForm select,
html[data-theme=dark] #OJBetter_editor, html[data-theme=dark] #OJBetter_statusBar,
html[data-theme=dark] #OJBetter_SubmitForm #RunTestButton, html[data-theme=dark] #programTypeId, html[data-theme=dark] #customTestBlock,
html[data-theme=dark] #OJBetter_SubmitForm #addCustomTest, html[data-theme=dark] #OJBetter_SubmitForm #SubmitButton,
html[data-theme=dark] .OJBetter_setting_menu input,
html[data-theme=dark] .OJBetter_setting_menu input[type="checkbox"], html[data-theme=dark] .OJBetter_setting_menu input[type="checkbox"]:checked,
html[data-theme=dark] .OJBetter_setting_menu textarea,
html[data-theme=dark] #OJBetter_SubmitForm input, html[data-theme=dark] #OJBetter_SubmitForm textarea,
html[data-theme=dark] #CompilerSetting select, html[data-theme=dark] #CompilerSetting textarea, html[data-theme=dark] #CompilerBox,
html[data-theme=dark] .OJBetter_setting_menu .OJBetter_checkboxs,
html[data-theme=dark] .help_tip .tip_text, html[data-theme=dark] .config::before,
html[data-theme=dark] #statePanel, html[data-theme=dark] .test-case, html[data-theme=dark] .OJBetter_setting_menu .badge{
border: var(--ojb-border-solid-primary);
}
html[data-theme=dark] #customTestBlock #customTests{
border-top: var(--ojb-border-solid-primary);
}
html[data-theme=dark] .OJBetter_setting_sidebar {
border-right: var(--ojb-border-solid-primary);
}
/* 实线边框-禁止 */
html[data-theme=dark] .ojb_btn[disabled]{
border: var(--ojb-border-solid-disabled);
}
/* 虚线边框 */
html[data-theme=dark] li#add_button,
html[data-theme=dark] .OJBetter_setting_menu_label_text{
border: var(--ojb-border-dashed);
}
/* 虚线边框-悬浮 */
html[data-theme=dark] li#add_button:hover{
border: var(--ojb-border-dashed-hover);
background-color: var(--ojb-color-bg-secondary);
color: var(--ojb-color-border-dashed-hover);
}
/* 无边框 */
html[data-theme=dark] .translate-problem-statement-panel .ojb_btn{
border: none;
}
/* 区域遮罩 */
html[data-theme=dark] .ojb-overlay::before {
background: var(--ojb-overlay-background);
color: var(--ojb-color-text-secondary);
text-shadow: 0px 0px 2px #000000;
}
/* 阴影 */
html[data-theme=dark] .translate-problem-statement-panel, html[data-theme=dark] .translate-problem-statement{
box-shadow: var(--ojb-shadow-standard);
}
/* 图标按钮状态样式 */
html[data-theme=dark] .ojb_btn_popover.success i:before, html[data-theme=dark] .ojb_btn_popover.success i {
color: var(--ojb-color-text-icon-success);
}
html[data-theme=dark] .ojb_btn_popover i:before {
text-shadow: var(--ojb-text-shadow-icon);
}
/* 其他样式 */
html[data-theme=dark] .OJBetter_setting_menu, html[data-theme=dark] .OJBetter_modal{
box-shadow: var(--ojb-shadow-menu-modal);
border: 1px solid var(--ojb-color-bg-secondary);
}
html[data-theme=dark] input[type="radio"]:checked+.OJBetter_setting_menu_label_text {
color: var(--ojb-color-text-primary);
border: 1px solid var(--ojb-color-border-radio-checked);
}
html[data-theme=dark] .alert{
text-shadow: none;
}
`);
// 网站界面样式
GM_addStyle(`
/* 文字颜色1 */
html[data-theme=dark] body, html[data-theme=dark] .float-container>#main-container,
html[data-theme=dark] .panel-default>.panel-heading, html[data-theme=dark] #header a,
html[data-theme=dark] .pagination>li>a, html[data-theme=dark] .pagination>li>span, html[data-theme=dark] .dropdown-menu,
html[data-theme=dark] .select2-container--bootstrap .select2-selection--single .select2-selection__rendered,
html[data-theme=dark] .ace-tm .ace_gutter, html[data-theme=dark] .translate-problem-statement-panel,
html[data-theme=dark] .select2-container--bootstrap .select2-results__option--highlighted[aria-selected],
html[data-theme=dark] .nav-pills>li.active>a, html[data-theme=dark] .user-unrated, html[data-theme=dark] #header .header-page li.is-active a,
html[data-theme=dark] .m-box_inner, html[data-theme=dark] .m-list-job_item, html[data-theme=dark] .a-btn_arrow,
html[data-theme=dark] #header, html[data-theme=dark] #header .header-sub_page li a,
html[data-theme=dark] .select2-container--default .select2-selection--single .select2-selection__rendered, html[data-theme=dark] .select2-results{
color: var(--ojb-color-text-primary) !important;
}
/* 文字颜色2 */
html[data-theme=dark] pre, html[data-theme=dark] .html2mdButton, html[data-theme=dark] .btn-default, html[data-theme=dark] .btn-pre,
html[data-theme=dark] small.contest-duration, html[data-theme=dark] .select2-container--bootstrap .select2-results__option,
html[data-theme=dark] #ace_settingsmenu, #kbshortcutmenu, html[data-theme=dark] code{
color: var(--ojb-color-text-secondary) !important;
}
/* 文字颜色3 */
html[data-theme=dark] input, html[data-theme=dark] #header .header-page li a:hover{
color: var(--ojb-color-text-secondary);
}
/* 文字颜色4 */
html[data-theme=dark] .katex{
color: var(--ojb-color-text-highlight) !important;
}
/* 链接颜色 */
html[data-theme=dark] a:link {
color: var(--ojb-color-text-link);
}
html[data-theme=dark] a:visited {
color: var(--ojb-color-text-secondary);
}
/* 按钮 */
html[data-theme=dark] input:hover, html[data-theme=dark] .btn-default:hover{
background-color: var(--ojb-color-bg-primary) !important;
}
/* 背景层次1 */
html[data-theme=dark] body, html[data-theme=dark] #main-div.float-container, html[data-theme=dark] pre,
html[data-theme=dark] .html2mdButton:hover, html[data-theme=dark] .pagination>.active>a, html[data-theme=dark] .ace-tm,
html[data-theme=dark] .dropdown-menu>li>a:hover, html[data-theme=dark] .dropdown-menu>li>a:focus,
html[data-theme=dark] .dropdown-menu .divider, html[data-theme=dark] .select2-container--bootstrap .select2-selection,
html[data-theme=dark] .ace-tm .ace_gutter-active-line, html[data-theme=dark] .select2-dropdown,
html[data-theme=dark] input, html[data-theme=dark] button, html[data-theme=dark] select, html[data-theme=dark] textarea,
html[data-theme=dark] code, html[data-theme=dark] #keyvisual .keyvisual-inner:before, html[data-theme=dark] .m-box_inner,
html[data-theme=dark] .m-list-job_item, html[data-theme=dark] .select2-container--default .select2-selection--single,
html[data-theme=dark] ol.linenums, html[data-theme=dark] li.L0, html[data-theme=dark] li.L1, html[data-theme=dark] li.L2,
html[data-theme=dark] li.L3, html[data-theme=dark] li.L4, html[data-theme=dark] li.L5, html[data-theme=dark] li.L6,
html[data-theme=dark] li.L7, html[data-theme=dark] li.L8, html[data-theme=dark] li.L9{
background-color: var(--ojb-color-bg-primary) !important;
}
/* 背景层次2 */
html[data-theme=dark] .float-container>#main-container, html[data-theme=dark] #contest-nav-tabs,
html[data-theme=dark] .btn-default, html[data-theme=dark] .html2mdButton,
html[data-theme=dark] .nav-tabs>li.active>a, html[data-theme=dark] .nav-tabs>li.active>a:hover, html[data-theme=dark] .nav-tabs>li.active>a:focus,
html[data-theme=dark] .nav>li>a:hover, html[data-theme=dark] .nav>li>a:focus, html[data-theme=dark] .panel,
html[data-theme=dark] .table-striped>tbody>tr:nth-of-type(odd), html[data-theme=dark] .insert-participant-box,
html[data-theme=dark] .btn-pre, html[data-theme=dark] .alert-success, html[data-theme=dark] .alert-info, html[data-theme=dark] .alert-danger,
html[data-theme=dark] .alert-warning, html[data-theme=dark] .panel-default>.panel-heading,
html[data-theme=dark] .pagination>li>a, html[data-theme=dark] .pagination>li>span, html[data-theme=dark] .dropdown-menu,
html[data-theme=dark] .ace-tm .ace_gutter, html[data-theme=dark] .select2-container--bootstrap .select2-results__option[aria-selected=true],
html[data-theme=dark] #ace_settingsmenu, #kbshortcutmenu, html[data-theme=dark] #header .header-inner,
html[data-theme=dark] ul#config_bar_ul::-webkit-scrollbar-thumb, html[data-theme=dark] .panel-info>.panel-heading,
html[data-theme=dark] .post-footer, html[data-theme=dark] .a-btn_arrow:before,
html[data-theme=dark] .table-hover>tbody>tr:hover,
html[data-theme=dark] li.L1, html[data-theme=dark] li.L3, html[data-theme=dark] li.L5, html[data-theme=dark] li.L7,
html[data-theme=dark] li.L9{
background-color: var(--ojb-color-bg-secondary) !important;
}
/* 实线边框颜色-圆角 */
html[data-theme=dark] input{
border: var(--ojb-border-solid-primary) !important;
border-radius: 2px;
}
/* 实线边框颜色-无圆角 */
html[data-theme=dark] .btn-default, html[data-theme=dark] .html2mdButton, html[data-theme=dark] .nav-tabs>li>a:hover,
html[data-theme=dark] .nav-tabs>li.active>a, html[data-theme=dark] .nav-tabs>li.active>a:hover,
html[data-theme=dark] .nav-tabs>li.active>a:focus, html[data-theme=dark] .btn-pre, html[data-theme=dark] .btn-pre:hover,
html[data-theme=dark] pre, html[data-theme=dark] .pagination>li>a, html[data-theme=dark] .pagination>li>span,
html[data-theme=dark] .table-bordered>thead>tr>th, html[data-theme=dark] .table-bordered>tbody>tr>th, html[data-theme=dark] .table-bordered>tfoot>tr>th,
html[data-theme=dark] .table-bordered>thead>tr>td, html[data-theme=dark] .table-bordered>tbody>tr>td, html[data-theme=dark] .table-bordered>tfoot>tr>td,
html[data-theme=dark] .panel, html[data-theme=dark] #editor, html[data-theme=dark] div#config_bar_list, html[data-theme=dark] label.config_bar_ul_li_text,
html[data-theme=dark] .select2-container--bootstrap .select2-selection, html[data-theme=dark] .select2-container--default .select2-selection--single{
border: var(--ojb-border-solid-primary) !important;
}
html[data-theme=dark] hr, html[data-theme=dark] .panel-footer,
html[data-theme=dark] .table>thead>tr>th, html[data-theme=dark] .table>tbody>tr>th, html[data-theme=dark] .table>tfoot>tr>th,
html[data-theme=dark] .table>thead>tr>td, html[data-theme=dark] .table>tbody>tr>td, html[data-theme=dark] .table>tfoot>tr>td{
border-top: var(--ojb-border-solid-primary) !important;
}
html[data-theme=dark] .nav-tabs, html[data-theme=dark] .panel-info>.panel-heading, html[data-theme=dark] .panel-default>.panel-heading,
html[data-theme=dark] .a-btn_arrow{
border-bottom: var(--ojb-border-solid-primary) !important;
}
html[data-theme=dark] .table>thead>tr>th{
border-bottom: 2px solid var(--ojb-color-border-primary) !important;
}
/* 双实线边框 */
html[data-theme=dark] #header .header-inner{
border-bottom: 5px double var(--ojb-color-border-primary) !important;
}
/* 阴影 */
html[data-theme=dark] .float-container>#main-container{
box-shadow: 0px 0px 10px 5px #fff0;
}
/* 图片-亮度 */
html[data-theme=dark] img{
opacity: .75;
}
/* 反转 */
html[data-theme=dark] .ace_content, html[data-theme=dark] #header .header-logo img, html[data-theme=dark] pre code{
filter: invert(1) hue-rotate(.5turn);
}
/* 区域遮罩 */
html[data-theme=dark] .ojb-overlay {
background: repeating-linear-gradient(135deg, #49525f6e, #49525f6e 30px, #49525f29 0px, #49525f29 55px);
color: #9099a3;
text-shadow: 0px 0px 2px #000000;
}
/* 其他样式 */
html[data-theme=dark] .nav-tabs>li.active>a, html[data-theme=dark] .nav-tabs>li.active>a:hover, html[data-theme=dark] .nav-tabs>li.active>a:focus{
border-bottom-color: transparent !important;
}
html[data-theme=dark] .collapsible-topic.collapsed .content .collapsible-topic-options:before{
background-image: linear-gradient(#22272e00, #22272e);
}
html[data-theme=dark] .alert{
text-shadow: none;
}
html[data-theme=dark] .m-box-news_post:before{
background: linear-gradient(0deg, #22272e 50%, rgba(255,255,255,0) 100%);
}
html[data-theme=dark] #header .header-sub_page li a:before, html[data-theme=dark] #header .header-page li a:before{
background-color: #9e9e9e !important;
}
html[data-theme=dark] .standings-score{
color: #2196f3;
}
html[data-theme=dark] pre code{
background-color: transparent !important;
}
html[data-theme=dark] #fixed-server-timer {
color: #000;
}
`);
})()
/**
* 黑暗模式额外的处理事件
*/
function darkModeStyleAdjustment() {
}
/**
* 初始化monaco编辑器资源
*/
async function initMonacoEditor() {
if (OJBetter.monaco.enableOnProblemPage || OJBetter.monaco.beautifyPreBlocks) {
try {
// 等待Monaco Editor加载器脚本加载完成
await OJB_LoadJS("https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/monaco-editor/0.49.0/min/vs/loader.min.js", "sha512-ZG31AN9z/CQD1YDDAK4RUAvogwbJHv6bHrumrnMLzdCrVu4HeAqrUX7Jsal/cbUwXGfaMUNmQU04tQ8XXl5Znw==");
// 配置Monaco Editor
require.config({
paths: { vs: "https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/monaco-editor/0.49.0/min/vs" },
"vs/nls": { availableLanguages: { "*": "zh-cn" } },
});
// 加载Monaco Editor主脚本
require(["vs/editor/editor.main"], () => {
OJBetter.monaco.loaderOnload = true;
});
} catch (error) {
console.error("Failed to load Monaco Editor: ", error);
}
}
}
/**
* 美化代码块
*/
async function beautifyPreBlocksWithMonaco() {
// 等待 MonacoLoader 加载完毕
await OJB_waitUntilTrue(() => OJBetter.monaco.loaderOnload);
const LINE_HEIGHT = 20; // 每行代码的高度
const MIN_HEIGHT = 100; // 容器的最小高度
/**
* 计算容器的高度
* @param {number} lineCount 代码的行数
* @param {string} [maxHeightStr='1000px'] 最大高度,为 'none' 时表示不限制
* @returns {string} 容器的高度
*/
const calculateContainerHeight = (lineCount, maxHeightStr = '1000px') => {
const maxHeight = parseInt(maxHeightStr, 10); // 最大高度
const calculatedHeight = Math.max(lineCount * LINE_HEIGHT, MIN_HEIGHT);
return `${isNaN(maxHeight) ? calculatedHeight : Math.min(calculatedHeight, maxHeight)}px`;
};
/**
* 用于替换 <pre> 标签为 Monaco 编辑器的函数
* @param {HTMLElement} pre <pre> 元素
*/
const replacePreWithMonaco = (pre) => {
if (pre.classList.contains('source-code-for-copy')) return; // 跳过复制块
const code = OJB_getCodeFromPre(pre); // 获取 <pre> 标签中的代码
if (!code) return;
const language = OJB_codeLangDetect(code); // 检测代码语言
const container = document.createElement('div'); // 创建一个用于 Monaco 编辑器的容器
const lineCount = code.split('\n').length; // 计算代码的行数
// 设置容器的样式
container.style.height = calculateContainerHeight(lineCount);
container.style.width = '100%';
pre.style.display = 'none';
pre.insertAdjacentElement('afterend', container);
// 折叠/展开按钮点击事件
const toggleExpandCollapse = (() => {
let isExpanded = false;
return () => {
container.style.height = calculateContainerHeight(lineCount, isExpanded ? '1000px' : 'none');
isExpanded = !isExpanded;
};
})();
// 重新注册 submission-code-expand-btn 按钮的点击事件
document.querySelectorAll('.submission-code-expand-btn').forEach(button => {
button.addEventListener('click', toggleExpandCollapse);
});
// 初始化 Monaco 编辑器
const editor = monaco.editor.create(container, {
value: code,
language,
readOnly: true,
tabSize: 4,
theme: OJBetter.common.realDarkMode == "dark" ? "vs-dark" : "vs",
scrollbar: {
verticalScrollbarSize: 10,
horizontalScrollbarSize: 10,
alwaysConsumeMouseWheel: false
},
automaticLayout: true,
scrollBeyondLastLine: false
});
// 保存编辑器实例
OJBetter.monaco.beautifyEditor.push(editor);
};
// 全局替换页面上所有的 <pre> 元素
document.querySelectorAll('pre').forEach(replacePreWithMonaco);
// 监听页面上的提交状态页面窗口的 <pre> 元素
if (OJBetter.typeOfPage.is_statePage || OJBetter.typeOfPage.is_submissions) {
OJB_observeElement({
selector: '#facebox',
callback: (node) => {
// 如果 facebox 中存在 pre 元素,则替换它们
node.querySelectorAll('pre').forEach(replacePreWithMonaco);
}
});
}
}
// 样式
GM_addStyle(`
/*动画*/
@keyframes shake {
0% { transform: translateX(-5px); }
100% { transform: translateX(5px); }
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
@keyframes rippleout {
0% {
box-shadow: 0 0 0 0 rgba(96, 98, 102, 0.2);
}
100% {
box-shadow: 0 0 0 6px rgba(0, 0, 0, 0);
}
}
@keyframes bounce-in {
20%,40%,60%,80%,from,to {
animation-timing-function: cubic-bezier(.215,.61,.355,1);
}
0% {
opacity: 0;
transform: scale3d(.995,.995,.995);
}
20% {
opacity: 1;
transform: scale3d(1.005,1.005,1.005);
}
40% {
transform: scale3d(.998,.998,.998);
}
60% {
transform: scale3d(1.002,1.002,1.002);
}
80% {
transform: scale3d(.995,.995,.995);
}
to {
opacity: 1;
transform: scale3d(1,1,1);
}
}
/*iconfont图标*/
.iconfont {
font-family: "iconfont" !important;
font-size: 16px;
font-style: normal !important;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
@font-face {
font-family: 'iconfont'; /* Project id 4284341 */
src: url('//aowuucdn.oss-accelerate.aliyuncs.com/iconfont/iconfont.woff2') format('woff2'),
url('//aowuucdn.oss-accelerate.aliyuncs.com/iconfont/iconfont.woff2.ttf') format('truetype');
}
html {
scroll-behavior: smooth;
}
:root {
--vp-font-family-base: "Chinese Quotes", "Inter var", "Inter", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Helvetica, Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
}
span.mdViewContent {
white-space: pre-wrap;
}
/* dialog */
dialog {
margin: 0px !important;
}
dialog::backdrop {
background-color: rgba(0, 0, 0, 0.4);
}
/*题目页链接栏样式*/
#problemToolbar {
display: flex;
gap: 6px;
flex-wrap: wrap;
justify-content: flex-end;
overflow: auto;
height: auto;
margin: 0.5em;
}
/*html2md面板*/
.html2md-panel {
display: flex;
justify-content: flex-end;
align-items: center;
gap: 6px;
padding: 5px 0px !important;
}
.html2md-panel a {
text-decoration: none;
}
.html2md-panel.is_simple {
position: absolute;
right: 2%;
}
/*通用按钮*/
.ojb_btn {
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
background-color: #ffffff;
color: #606266;
width: auto;
font-size: 13px;
border-radius: 0.3rem;
padding: 2px 5px;
margin: 0px;
border: 1px solid #dcdfe6;
}
.ojb_btn[disabled] {
cursor: not-allowed !important;
color: rgb(168, 171, 178) !important;
border: 1px solid #e4e7ed;
background-color: #ffffff;
}
.ojb_btn:hover {
color: #409eff;
border-color: #409eff;
background-color: #f1f8ff;
}
.ojb_btn.primary {
color: #ffffff;
border: 1px solid #409eff;
background-color: #409eff;
}
.ojb_btn.primary:hover {
color: #ffffff;
border: 1px solid #79bbff;
background-color: #79bbff;
}
.ojb_btn.success {
color: #4caf50;
border: 1px solid #C8E6C9;
background-color: #f0f9eb;
}
.ojb_btn.warning {
color: #e6a23c;
border: 1px solid #f3d19e;
background-color: #fdf6ec;
}
.ojb_btn.error {
color: #f56c6c;
border: 1px solid #fab6b6;
background-color: #fef0f0;
}
.ojb_btn.enabled {
color: #42A5F5;
border: 1px solid #90CAF9;
background-color: #fafbff;
}
.ojb_btn.active {
animation: rippleout 0.5s ease-in-out;
}
a.ojb_btn {
text-decoration: none;
}
a.ojb_btn:link {
color: #606266;
}
a.ojb_btn span {
margin-left: 2px;
}
/*按钮图标和popover*/
.ojb_btn_popover {
display: flex;
justify-content: center;
position: relative;
outline: none;
appearance: none;
}
.ojb_btn_popover:hover span {
opacity: 1;
visibility: visible;
}
.ojb_btn_popover i:before {
position: absolute;
text-shadow: 1px 1px 0px #ffffff, 1px -1px 0px #ffffff, -1px -1px 0px #ffffff, -1px 1px 0px #ffffff;
}
.ojb_btn_popover span {
cursor: initial;
position: absolute;
left: 50%;
opacity: 0;
visibility: hidden;
padding: 4px 8px;
background-color: rgba(33, 33, 33, 0.8);
color: rgba(255, 255, 255, 0.9019607843);
font-size: 12px;
border-radius: 6px;
line-height: 1.6;
text-align: left;
white-space: nowrap;
transition: all 0.15s ease-in-out;
z-index: 999;
}
.ojb_btn_popover span:hover {
opacity: 0;
visibility: hidden;
}
.ojb_btn_popover.top:hover span {
transform: translate(-50%, 0);
}
.ojb_btn_popover.top span {
bottom: 100%;
transform: translate(-50%, -20%);
margin-bottom: 4px;
}
.ojb_btn_popover.top span:hover {
transform: translate(-50%, -20%);
}
.ojb_btn_popover.bottom:hover span {
transform: translate(-50%, 105%);
}
.ojb_btn_popover.bottom span {
bottom: -2%;
transform: translate(-50%, 100%);
margin-top: 4px;
}
.ojb_btn_popover.bottom span:hover {
transform: translate(-50%, 50%);
}
.ojb_btn_popover.loading i {
color: rgba(33, 33, 33, 0.1);
}
.ojb_btn_popover.loading i:before {
content: "\\e640";
color: rgb(168, 171, 178);
animation: rotate 2s cubic-bezier(0.65, 0.05, 0.36, 1) infinite;
}
.ojb_btn_popover.running i {
color: rgba(33, 33, 33, 0.1);
}
.ojb_btn_popover.running i:before {
content: "\\e600";
color: rgb(168, 171, 178);
animation: rotate 1s linear infinite;
}
.ojb_btn_popover.warning i {
color: rgba(230, 162, 61, 0.8);
}
.ojb_btn_popover.warning i:before {
content: "\\e68b";
font-size: 15px;
left: 10px;
bottom: 0%;
color: #ff9800;
}
.ojb_btn_popover.error i {
color: rgba(245, 108, 108, 0.8);
}
.ojb_btn_popover.error i:before {
content: "\\e651";
font-size: 15px;
left: 10px;
bottom: 0%;
color: #F44336;
}
.ojb_btn_popover.success i {
color: rgba(76, 175, 80, 0.9);
}
.ojb_btn_popover.success i:before {
content: "\\e61e";
font-size: 15px;
left: 10px;
bottom: 0%;
color: #4caf50;
}
.ojb_btn_popover.enabled i {
color: rgba(33, 150, 243, 0.6);
}
.ojb_btn_popover.enabled i:before {
content: "\\e6f4";
font-size: 15px;
left: 10px;
bottom: 0%;
color: #2196F3;
}
.ojb_btn_popover.redo i {
color: rgba(33, 33, 33, 0.1);
}
.ojb_btn_popover.redo i:before {
content: "\\e831";
color: #616161;
}
.ojb_btn_popover.reverse i {
transform: rotate(180deg);
}
/*translateDiv样式*/
.translateDiv .topText {
display: flex;
margin-left: 5px;
color: #9e9e9e;
font-size: 13px;
align-items: center;
}
.translateDiv .borderlessButton{
display: flex;
align-items: center;
margin: 2.5px 7px;
fill: #9E9E9E;
}
.translateDiv .borderlessButton:hover{
cursor: pointer;
fill: #059669;
}
.translateDiv.bounce-in {
animation: bounce-in 1s forwards;
}
html:not([data-theme='dark']) .translateDiv {
box-shadow: 0px 0px 0.5px 0.5px #defdf378;
}
.translate-problem-statement {
justify-items: start;
letter-spacing: 1.8px;
color: #059669;
background-color: #f9f9fa;
border: 1px solid #c5ebdf;
border-radius: 0rem 0rem 0.3rem 0.3rem;
padding: 5px;
margin: -5px 0px 6px 0px;
width: 100%;
box-sizing: border-box;
font-size: 13px;
}
.translate-problem-statement h2 {
font-size: 1.6em;
font-weight: 700;
}
.translate-problem-statement h3 {
font-size: 1.3em;
font-weight: 700;
}
.translate-problem-statement-panel{
display: flex;
justify-content: space-between;
background-color: #f9f9fa;
border: 1px solid #c5ebdf;
border-radius: 0.3rem;
margin: 4px 0px;
}
.translate-problem-statement-panel .ojb_btn {
background: none;
border: none;
color: #9e9e9e;
padding: 5px 8px;
}
.translate-problem-statement-panel.error, .translate-problem-statement.error {
color: red;
border-color: red;
}
.translate-problem-statement a, .translate-problem-statement a:link {
color: #10b981;
font-weight: 600;
background: 0 0;
text-decoration: none;
}
.translate-problem-statement ol, .translate-problem-statement ul {
display: grid;
margin-inline-start: 0.8em;
margin-block-start: 0em;
margin: 0.5em 0 0 3em;
padding-inline-start: 0px;
}
.translate-problem-statement li {
display: list-item;
height: auto;
word-wrap: break-word;
}
.translate-problem-statement ol li {
list-style-type: auto;
}
.translate-problem-statement ul li {
list-style-type: disc;
}
.translate-problem-statement img {
max-width: 100.0%;
max-height: 100.0%;
}
#task-statement .translate-problem-statement .MathJax {
color: #059669!important;
}
.translate-problem-statement span.math {
margin: 0px 2.5px !important;
}
.translate-problem-statement a:hover {
background-color: #800;
color: #fff;
text-decoration: none;
}
.translate-problem-statement table {
border: 1px #ccc solid !important;
margin: 1.5em 0 !important;
color: #059669 !important;
}
.translate-problem-statement table thead th {
border: 1px #ccc solid !important;
color: #059669 !important;
}
.translate-problem-statement table td {
border-right: 1px solid #ccc;
border-top: 1px solid #ccc;
padding: 0.7143em 0.5em;
}
.translate-problem-statement table th {
padding: 0.7143em 0.5em;
}
.translate-problem-statement p:not(:first-child) {
margin: 1.5em 0 0;
}
.translate-problem-statement p {
line-height: 20px !important;
word-wrap: break-word;
font-size: 13px !important
}
.problem-statement p:last-child {
margin-bottom: 0px !important;
}
/*设置按钮*/
header .enter-or-register-box, header .languages {
position: absolute;
right: 170px;
}
.ojb_btn.OJBetter_setting {
float: right;
height: 30px;
background: #60a5fa;
color: white;
margin: 10px;
border: 1px solid #60a5fa;
}
.ojb_btn.OJBetter_setting.open {
background-color: #e6e6e6;
color: #727378;
cursor: not-allowed;
}
/*设置面板*/
.OJBetter_setting_menu {
box-shadow: 0px 0px 0px 4px #ffffff;
position: fixed;
top: 50%;
left: 50%;
width: 600px;
min-height: 600px;
transform: translate(-50%, -50%);
border-radius: 6px;
background-color: #f0f4f9;
border-collapse: collapse;
border: 1px solid #ffffff;
color: #697e91;
font-family: var(--vp-font-family-base);
padding: 10px 20px 20px 10px;
box-sizing: content-box;
}
.OJBetter_setting_menu h3 {
margin-top: 10px;
font-size: 1.4em;
font-weight: 700;
}
.OJBetter_setting_menu h4 {
margin: 15px 0px 10px 0px;
}
.OJBetter_setting_menu h4,.OJBetter_setting_menu h5 {
font-weight: 600;
}
.OJBetter_setting_menu hr {
border: none;
height: 1px;
background-color: #ccc;
margin: 10px 0;
}
.OJBetter_setting_menu details {
padding: 10px;
margin-bottom: 5px;
background-color: #ffffff;
border-bottom: 1px solid #c9c6c696;
border-radius: 8px;
}
.OJBetter_setting_menu .badge {
border-radius: 4px;
border: 1px solid #009688;
color: #009688;
background-color: #fff;
padding: 0.5px 4px;
margin-left: 5px;
margin-right: auto;
line-height: initial;
font-weight: initial;
}
.OJBetter_setting_menu .missing {
box-shadow: inset 0px 0px 1px 1px red;
}
/* 页面切换 */
.OJBetter_setting_menu .settings-page {
display: none;
}
.OJBetter_setting_menu .settings-page.active {
display: block;
}
.OJBetter_setting_container {
display: flex;
}
.OJBetter_setting_sidebar {
flex: 0 0 auto;
min-width: 110px;
padding: 6px 10px 6px 6px;
margin: 20px 0px;
border-right: 1px solid #d4d8e9;
}
.OJBetter_setting_content {
flex-grow: 1;
margin: 20px 0px 0px 12px;
padding-right: 10px;
max-height: 580px;
overflow-y: auto;
box-sizing: border-box;
}
.OJBetter_setting_sidebar h3 {
margin-top: 0;
}
.OJBetter_setting_sidebar hr {
margin-top: 10px;
margin-bottom: 10px;
border: none;
border-top: 1px solid #DADCE0;
}
.OJBetter_setting_sidebar ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.OJBetter_setting_sidebar li {
margin: 5px 0px;
background-color: #ffffff;
border: 1px solid #d4d8e9;
border-radius: 4px;
font-size: 16px;
}
.OJBetter_setting_sidebar li a {
text-decoration: none;
display: flex;
width: 100%;
font-size: 16px;
color: gray;
background-color: #ffffff;
border: none;
letter-spacing: 2px;
padding: 7px;
margin: 0px;
border-radius: 4px;
align-items: center;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.OJBetter_setting_sidebar li a.active {
background-color: #eceff1c7;
}
/* 链接样式 */
.OJBetter_setting_menu a {
font-size: 13px;
color: #009688 !important;
background-color: #E0F2F1;
border: 1px solid #009688;
border-radius: 4px;
padding: 0px 5px;
margin: 0px 5px;
text-decoration: none;
}
/* 下拉选择框 */
.OJBetter_setting_menu select {
appearance: none;
padding: 5px 10px;
margin: -5px 0px;
border-radius: 6px;
border-style: solid;
border: 1px solid #ced4da;
color: #009688;
background: #ffffff;
font-size: 15px;
}
.OJBetter_setting_menu select:focus-visible {
outline: none;
}
.OJBetter_setting_menu select option:disabled {
color: #EEEEEE;
}
/* 数值输入框 */
.OJBetter_setting_menu input[type="number"] {
width: 70px;
color: #009688;
font-size: 15px;
appearance: none;
padding: 5px 10px;
margin: -5px 3px;
border-radius: 6px;
border-style: solid;
border: 1px solid #ced4da;
}
.OJBetter_setting_menu input[type="number"]:focus-visible {
outline: none;
}
.OJBetter_setting_menu input[type="number"]::-webkit-inner-spin-button,
.OJBetter_setting_menu input[type="number"]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
/*设置面板-滚动条*/
.OJBetter_setting_menu::-webkit-scrollbar, .OJBetter_setting_content::-webkit-scrollbar,
.OJBetter_modal .content::-webkit-scrollbar {
width: 5px;
height: 7px;
background-color: #aaa;
}
.OJBetter_setting_menu::-webkit-scrollbar-thumb, .OJBetter_setting_content::-webkit-scrollbar-thumb,
.OJBetter_modal .content::-webkit-scrollbar-thumb {
background-clip: padding-box;
background-color: #d7d9e4;
}
.OJBetter_setting_menu::-webkit-scrollbar-track, .OJBetter_setting_content::-webkit-scrollbar-track,
.OJBetter_modal .content::-webkit-scrollbar-track {
background-color: #f1f1f1;
}
/*设置面板-关闭按钮*/
.OJBetter_setting_menu .tool-box {
position: absolute;
width: 20px;
height: 20px;
top: 3px;
right: 3px;
}
.OJBetter_setting_menu .btn-close {
width: 20px;
height: 20px;
border-radius: 50%;
border: none;
margin: 0px;
padding: 0px;
background-color: #ff000080;
transition: .15s ease all;
box-sizing: border-box;
text-align: center;
color: transparent;
}
.OJBetter_setting_menu .iconfont {
font-size: 10px;
font-weight: bolder;
}
.OJBetter_setting_menu .btn-close:hover {
color: #ffffff;
background-color: #ff0000cc;
box-shadow: 0 5px 5px 0 #00000026;
}
.OJBetter_setting_menu .btn-close:active {
color: #ffffffde;
background-color: #ff000080;
}
/*设置面板-checkbox*/
.OJBetter_setting_menu input[type=checkbox]:focus {
outline: 0px;
}
.OJBetter_setting_menu .OJBetter_setting_list input[type="checkbox"] {
margin: 0px;
appearance: none;
-webkit-appearance: none;
width: 40px;
height: 20px;
border: 1.5px solid #D7CCC8;
padding: 0px !important;
border-radius: 20px;
background: #efebe978;
position: relative;
box-sizing: border-box;
}
.OJBetter_setting_menu .OJBetter_setting_list input[type="checkbox"]::before {
content: "";
width: 17px;
height: 17px;
background: #D7CCC8;
border: 1.5px solid #BCAAA4;
border-radius: 50%;
position: absolute;
top: 0;
left: 0;
transform: translate(2%, 2%);
transition: all 0.3s ease-in-out;
box-sizing: border-box;
}
.OJBetter_setting_menu .OJBetter_setting_list input[type="checkbox"]::after {
content: url("data:image/svg+xml,%3Csvg xmlns='://www.w3.org/2000/svg' width='23' height='23' viewBox='0 0 23 23' fill='none'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M6.55021 5.84315L17.1568 16.4498L16.4497 17.1569L5.84311 6.55026L6.55021 5.84315Z' fill='%23EA0707' fill-opacity='0.89'/%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M17.1567 6.55021L6.55012 17.1568L5.84302 16.4497L16.4496 5.84311L17.1567 6.55021Z' fill='%23EA0707' fill-opacity='0.89'/%3E%3C/svg%3E");
position: absolute;
top: 0;
left: 24px;
}
.OJBetter_setting_menu .OJBetter_setting_list input[type="checkbox"]:checked {
border: 1.5px solid #C5CAE9;
background: #E8EAF6;
}
.OJBetter_setting_menu .OJBetter_setting_list input[type="checkbox"]:checked::before {
background: #C5CAE9;
border: 1.5px solid #7986CB;
transform: translate(122%, 2%);
transition: all 0.3s ease-in-out;
}
.OJBetter_setting_menu .OJBetter_setting_list input[type="checkbox"]:checked::after {
content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 15 13' fill='none'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M14.8185 0.114533C15.0314 0.290403 15.0614 0.605559 14.8855 0.818454L5.00187 12.5L0.113036 6.81663C-0.0618274 6.60291 -0.0303263 6.2879 0.183396 6.11304C0.397119 5.93817 0.71213 5.96967 0.886994 6.18339L5.00187 11L14.1145 0.181573C14.2904 -0.0313222 14.6056 -0.0613371 14.8185 0.114533Z' fill='%2303A9F4' fill-opacity='0.9'/%3E%3C/svg%3E");
position: absolute;
top: 1.5px;
left: 4.5px;
}
.OJBetter_setting_menu .OJBetter_setting_list button {
cursor: pointer;
color: #7986cb;
background-color: #e8eaf6;
border: 1px solid #7986cb;
border-radius: 6px;
width: 100px;
margin: -5px 2px;
padding: 5px 10px;
}
.OJBetter_setting_menu .OJBetter_setting_list button:hover {
color: #e8eaf6;
background-color: #7986cb;
border: 1px solid #7986cb;
}
.OJBetter_setting_menu label, #darkMode_span, #loaded_span {
font-size: 16px;
}
.OJBetter_setting_list {
display: flex;
flex-wrap: wrap;
align-items: center;
padding: 10px;
margin: 5px 0px;
background-color: #ffffff;
border: 1px solid #c9c6c642;
border-bottom-color: #c9c6c696;
border-radius: 8px;
justify-content: space-between;
}
.OJBetter_setting_list.alert_danger {
color: #F44336;
background-color: #FFEBEE;
border: 1px solid #F44336;
margin: 10px 0px;
}
.OJBetter_setting_list.alert_warn {
color: #E65100;
background-color: #FFF3E0;
border: 1px solid #FF9800;
margin: 10px 0px;
}
.OJBetter_setting_list.alert_tip {
color: #009688;
background-color: #E0F2F1;
border: 1px solid #009688;
margin: 10px 0px;
}
.OJBetter_setting_list.alert_info {
color: #ffffff;
background-color: #009688;
margin: 10px 0px;
box-shadow: rgba(0, 0, 0, 0.06) 0px 2px 4px 0px inset;
}
.OJBetter_setting_list p:not(:last-child) {
margin-bottom: 10px;
}
.OJBetter_setting_list p:not(:first-child) {
margin-top: 10px;
}
/*设置面板-checkboxs*/
.OJBetter_setting_menu .OJBetter_checkboxs {
flex-basis: 100%;
display: flex;
padding: 8px;
margin: 10px 0px 0px 0px;
border-bottom: 1px solid #c9c6c696;
border-radius: 8px;
border: 1px solid #c5cae9;
background-color: #f0f8ff;
}
.OJBetter_setting_menu .OJBetter_checkboxs label {
font-size: 13px;
margin: 0px 6px 0px 3px;
}
.OJBetter_setting_menu .OJBetter_checkboxs input[type=checkbox]:checked+label{
color: #7986cb;
}
.OJBetter_setting_menu .OJBetter_checkboxs input[type="checkbox"] {
border: none;
width: 16px;
height: 16px;
}
.OJBetter_setting_menu .OJBetter_checkboxs input[type="checkbox"]::before{
background: #ffffff;
transform: none;
width: 16px;
height: 16px;
}
.OJBetter_setting_menu .OJBetter_checkboxs input[type="checkbox"]:checked {
background: none;
border: none;
}
.OJBetter_setting_menu .OJBetter_checkboxs input[type="checkbox"]:checked::before {
border: 1.5px solid #95a2de;
background: #e8eaf6;
transform: none;
}
.OJBetter_setting_menu .OJBetter_checkboxs input[type="checkbox"]:checked::after {
content: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='9' height='9' viewBox='0 0 15 13' fill='none'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M14.8185 0.114533C15.0314 0.290403 15.0614 0.605559 14.8855 0.818454L5.00187 12.5L0.113036 6.81663C-0.0618274 6.60291 -0.0303263 6.2879 0.183396 6.11304C0.397119 5.93817 0.71213 5.96967 0.886994 6.18339L5.00187 11L14.1145 0.181573C14.2904 -0.0313222 14.6056 -0.0613371 14.8185 0.114533Z' fill='%2303A9F4' fill-opacity='0.9'/%3E%3C/svg%3E");
top: 0px;
left: 3.5px;
}
.OJBetter_setting_menu .OJBetter_checkboxs input[type="checkbox"]:disabled+label {
color: #BDBDBD;
}
/*设置面板-radio*/
.OJBetter_setting_menu label {
display: block;
font-weight: initial;
list-style-type: none;
padding-inline-start: 0px;
overflow-x: auto;
max-width: 100%;
margin: 3px 0px;
overflow-x: visible;
}
.OJBetter_setting_menu_label_text {
display: flex;
border: 1px dashed #00aeeccc;
height: 35px;
width: 100%;
color: #6e6e6e;
font-weight: 300;
font-size: 14px;
letter-spacing: 2px;
padding: 7px;
margin-bottom: 4px;
align-items: center;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
input[type="radio"]:checked+.OJBetter_setting_menu_label_text {
background: #41e49930;
border: 1px solid green;
color: green;
text-shadow: 0px 0px 0.5px green;
}
input[type="radio"]:disabled+.OJBetter_setting_menu_label_text {
background: #fafafa00;
border: 1px solid #e0e0e07a;
color: #e0e0e0;
}
.OJBetter_setting_menu label input[type="radio"], .OJBetter_contextmenu label input[type="radio"]{
appearance: none;
list-style: none;
padding: 0px !important;
margin: 0px;
clip: rect(0 0 0 0);
-webkit-clip-path: inset(100%);
clip-path: inset(100%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
/*设置面板-文本输入框*/
.OJBetter_setting_menu input[type="text"] {
display: block;
height: 25px !important;
width: 100%;
background-color: #ffffff;
color: #727378;
font-size: 12px;
border-radius: 0.3rem;
padding: 1px 5px !important;
box-sizing: border-box;
margin: 5px 0px 5px 0px;
border: 1px solid #00aeeccc;
box-shadow: 0 0 1px #0000004d;
}
.OJBetter_setting_menu .OJBetter_setting_list input[type="text"] {
margin-left: 5px;
}
.OJBetter_setting_menu input[type="text"]:focus-visible{
border-style: solid;
border-color: #3f51b5;
outline: none;
}
.OJBetter_setting_menu_config_box {
width: 100%;
display: grid;
margin-top: 5px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.OJBetter_setting_menu input::placeholder {
color: #727378;
}
.OJBetter_setting_menu input.no_default::placeholder{
color: #BDBDBD;
}
.OJBetter_setting_menu input.is_null::placeholder{
color: red;
border-width: 1.5px;
}
.OJBetter_setting_menu input.is_null{
border-color: red;
}
.OJBetter_setting_menu textarea {
resize: vertical;
display: block;
width: 100%;
height: 60px;
background-color: #ffffff;
color: #727378;
font-size: 12px;
padding: 1px 5px !important;
box-sizing: border-box;
margin: 5px 0px 5px 0px;
border: 1px solid #00aeeccc;
box-shadow: 0 0 1px #0000004d;
}
.OJBetter_setting_menu textarea:focus-visible{
border-style: solid;
border-color: #3f51b5;
outline: none;
}
.OJBetter_setting_menu textarea::placeholder{
color: #BDBDBD;
font-size: 14px;
}
.OJBetter_setting_menu #tempConfig_save {
cursor: pointer;
display: inline-flex;
padding: 5px;
background-color: #1aa06d;
color: #ffffff;
font-size: 14px;
line-height: 1.5rem;
font-weight: 500;
justify-content: center;
width: 100%;
border-radius: 0.375rem;
border: none;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
margin-top: 20px
}
.OJBetter_setting_menu button#debug_button.debug_button {
width: 18%;
}
.OJBetter_setting_menu span.tip {
color: #999;
font-size: 12px;
font-weight: 500;
padding: 5px 0px;
}
/*设置面板-tip*/
.help_tip {
margin-right: auto;
}
span.input_label {
font-size: 14px;
}
.help_tip .tip_text {
display: none;
position: absolute;
color: #697e91;
font-weight: 400;
font-size: 14px;
letter-spacing: 0px;
background-color: #ffffff;
padding: 10px;
margin: 5px 0px;
border-radius: 4px;
border: 1px solid #e4e7ed;
box-shadow: 0px 0px 12px rgba(0, 0, 0, .12);
z-index: 100;
}
.help_tip .tip_text p {
margin-bottom: 5px;
}
.help_tip .tip_text:before {
content: "";
position: absolute;
top: -20px;
right: -10px;
bottom: -10px;
left: -10px;
z-index: -1;
}
.help-icon {
cursor: help;
width: 15px;
color: #b4b9d4;
margin-left: 5px;
margin-top: 3px;
}
.OJBetter_setting_menu .OJBetter_setting_menu_label_text .help_tip .help-icon {
color: #7fbeb2;
}
.help_tip .help-icon:hover + .tip_text, .help_tip .tip_text:hover {
display: block;
cursor: help;
width: 250px;
}
/* 版本信息 */
.OJBetter_setting_menu .versionInfo{
display: grid;
justify-items: center;
font-size: 16px;
padding: 10px;
}
.OJBetter_setting_menu .versionInfo>* {
margin: 10px 0px;
}
/* 配置管理面板 */
.config{
width: 100%;
margin: 10px 0px;
}
.config li.tempConfig_add_button {
cursor: pointer;
height: 40px;
border: 1px dashed #BDBDBD;
border-radius: 8px;
background-color: #fcfbfb36;
color: #bdbdbd;
font-size: 14px;
align-items: center;
justify-content: center;
}
.config li.tempConfig_add_button:hover {
border: 1px dashed #03A9F4;
background-color: #d7f0fb8c;
color: #03A9F4;
}
.config .config_bar_list {
display: flex;
width: 100%;
padding-bottom: 2px;
border: 1px solid #c5cae9;
background-color: #f0f8ff;
box-sizing: border-box;
border-radius: 0px 0px 8px 8px;
}
.config .config_bar_list input[type="radio"] {
appearance: none;
width: 0;
height: 0;
overflow: hidden;
}
.config .config_bar_list input[type="radio"] {
margin: 0px;
}
.config .config_bar_list input[type=radio]:focus {
outline: 0px;
}
.config .config_bar_ul_li_text {
display: flex;
align-items: center;
justify-content: center;
max-width: 100%;
height: 40px;
overflow-x: auto;
font-size: 14px;
font-weight: 400;
margin: 0px 4px;
padding: 3px;
border: 1px solid #dedede;
border-radius: 10px;
box-shadow: 0px 2px 4px 0px rgba(0,0,0,.05);
box-sizing: border-box;
}
.config .config_bar_ul li button {
background-color: #e6e6e6;
color: #727378;
height: 23px;
font-size: 14px;
border-radius: 0.3rem;
padding: 1px 5px;
margin: 5px;
border: none;
box-shadow: 0 0 1px #0000004d;
}
.config .config_bar_ul {
display: flex;
align-items: center;
list-style-type: none;
padding-inline-start: 0px;
overflow-x: auto;
max-width: 100%;
margin: 0px;
padding: 5px;
}
.config .config_bar_ul li {
width: 80px;
display: grid;
margin: 4px 4px;
min-width: 100px;
box-sizing: border-box;
}
.config .config_bar_ul_li_text:hover {
background-color: #eae4dc24;
}
input[type="radio"]:checked + .config_bar_ul_li_text {
background: #41b3e430;
border: 1px solid #5e7ce0;
color: #5e7ce0;
}
.config .config_bar_ul::-webkit-scrollbar {
width: 5px;
height: 4px;
}
.config .config_bar_ul::-webkit-scrollbar-thumb {
background-clip: padding-box;
background-color: #d7d9e4;
border-radius: 8px;
}
.config .config_bar_ul::-webkit-scrollbar-button:start:decrement {
width: 4px;
background-color: transparent;
}
.config .config_bar_ul::-webkit-scrollbar-button:end:increment {
width: 4px;
background-color: transparent;
}
.config .config_bar_ul::-webkit-scrollbar-track {
border-radius: 5px;
}
.config .config_bar_ul_li_text::-webkit-scrollbar {
width: 5px;
height: 7px;
background-color: #aaa;
}
.config .config_bar_ul_li_text::-webkit-scrollbar-thumb {
background-clip: padding-box;
background-color: #d7d9e4;
}
.config .config_bar_ul_li_text::-webkit-scrollbar-track {
background-color: #f1f1f1;
}
.config .config_bar_list_add_div {
display: flex;
height: 40px;
margin: 4px 2px;
}
/* 修改菜单 */
#config_bar_menu {
z-index: 400;
position: fixed;
width: 60px;
background: #ffffff;
box-shadow: 1px 1px 4px 0px #0000004d;
border: 0px solid rgba(0,0,0,0.04);
border-radius: 4px;
padding: 8px 0;
}
.config_bar_menu_item {
cursor: pointer;
padding: 2px 6px;
display: flex;
justify-content: center;
align-items: center;
height: 32px;
font-size: 14px;
font-weight: 500;
box-shadow: inset 0px 0px 0px 0px #8bb2d9;
}
#config_bar_menu_edit:hover {
background-color: #00aeec;
color: white;
}
#config_bar_menu_delete:hover {
background-color: #FF5722;
color: white;
}
/* 配置编辑页面 */
#config_edit_menu {
z-index: 300;
width: 450px;
}
/* 黑暗模式选项按钮 */
.dark-mode-selection {
display: flex;
justify-content: center;
align-items: center;
max-width: 350px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.dark-mode-selection label {
margin: 8px 0px 8px 8px;
}
.dark-mode-selection > * {
margin: 6px;
}
.dark-mode-selection .OJBetter_setting_menu_label_text {
border-radius: 8px;
margin-bottom: 0px;
}
/*确认弹窗*/
.OJBetter_modal {
z-index: 600;
display: grid;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 12px;
font-family: var(--vp-font-family-base);
width: max-content;
padding: 10px 20px;
box-shadow: 0px 0px 0px 4px #ffffff;
border-radius: 6px;
background-color: #f0f4f9;
border-collapse: collapse;
border: 1px solid #ffffff;
color: #697e91;
}
.OJBetter_modal h2 {
font-size: 1.6em;
font-weight: 700;
}
.OJBetter_modal .content{
white-space: nowrap;
max-height: 500px;
overflow-y: auto;
}
.OJBetter_modal .buttons{
display: flex;
padding-top: 15px;
}
.OJBetter_modal button {
display: inline-flex;
justify-content: center;
align-items: center;
line-height: 1;
white-space: nowrap;
cursor: pointer;
text-align: center;
box-sizing: border-box;
outline: none;
transition: .1s;
user-select: none;
vertical-align: middle;
-webkit-appearance: none;
height: 24px;
padding: 5px 11px;
margin-right: 15px;
font-size: 12px;
border-radius: 4px;
color: #ffffff;
background: #009688;
border-color: #009688;
border: none;
}
.OJBetter_modal button.secondary{
background-color:#4DB6AC;
}
.OJBetter_modal button:hover{
background-color:#4DB6AC;
}
.OJBetter_modal button.secondary:hover {
background-color: #80CBC4;
}
.OJBetter_modal .help-icon {
margin: 0px 8px 0px 0px;
height: 1em;
width: 1em;
line-height: 1em;
display: inline-flex;
justify-content: center;
align-items: center;
position: relative;
fill: currentColor;
font-size: inherit;
}
.OJBetter_modal p {
margin: 5px 0px;
}
/* 右键菜单 */
.OJBetter_contextmenu {
z-index: 500;
display: grid;
position: absolute;
background-color: #f0f4f9;
border-collapse: collapse;
color: #697e91;
font-family: var(--vp-font-family-base);
overflow: hidden;
box-sizing: content-box;
box-shadow: 0px 0px 0px 2px #eddbdb4d;
}
.OJBetter_contextmenu label {
margin: 0px;
}
input[type="radio"]:checked+.OJBetter_contextmenu_label_text {
background: #41e49930;
border: 1px solid green;
color: green;
font-weight: 500;
}
.OJBetter_contextmenu_label_text {
display: flex;
border: 1px dashed #80cbc4;
height: 26px;
width: 100%;
color: gray;
font-size: 13px;
font-weight: initial;
padding: 4px;
align-items: center;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.OJBetter_contextmenu_label_text:hover {
color: #F44336;
border: 1px dashed #009688;
background-color: #ffebcd;
}
/* RatingByClist */
.ratingBadge, html[data-theme=dark] button.ratingBadge{
display: block;
font-weight: 700;
font-size: 11px;
margin-top: 5px;
padding: 2px;
border-radius: 4px;
color: #B0BEC5;
border: 1px solid #cccccc66;
}
/* 多选翻译 */
.block_selected{
box-shadow: 0px 0px 0px 1px #FF9800;
outline: none;
}
/* 悬浮菜单 */
.OJBetter_MiniTranslateButton {
z-index: 100;
display: grid;
position: absolute;
border-collapse: collapse;
fill: #F57C00;
background-color: #FFF3E0;
overflow: hidden;
box-sizing: content-box;
box-shadow: 0px 0px 0px 2px #FFE0B2;
border-radius: 100%;
}
.OJBetter_MiniTranslateButton:hover {
cursor: pointer;
box-shadow: 0px 0px 0px 2px #FFB74D;
}
/* acmsguru划分块 */
.OJBetter_acmsguru {
margin: 0 0 1em!important;
}
/* 代码提交表单 */
#OJBetter_SubmitForm.input-output-copier:hover {
background-color: #ffffff00;
}
#OJBetter_SubmitForm input[type="number"] {
width: 40px;
color: #009688;
appearance: none;
border-radius: 6px;
border-style: solid;
border: none;
background-color: #ffffff00;
}
#OJBetter_SubmitForm :focus-visible {
outline: none;
}
#OJBetter_SubmitForm .topDiv {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 0px;
box-sizing: border-box;
}
#OJBetter_SubmitForm .topDiv .topRightDiv {
height: 100%;
display: flex;
flex-wrap: wrap;
gap: 5px;
}
#OJBetter_SubmitForm input[type="checkbox"], #OJBetter_SubmitForm label {
margin: 0px;
font-weight: initial;
}
/* 顶部右侧区域 */
#OJBetter_SubmitForm .topRightDiv>* {
height: 30px;
box-sizing: border-box;
}
#OJBetter_SubmitForm .topRightDiv>button{
padding: 0px 8px;
}
#OJBetter_SubmitForm .topRightDiv {
display: flex;
flex-wrap: wrap;
gap: 0px;
align-items: center;
}
/* LSP连接Log */
#LSPLog{
width: 500px;
height: 500px;
position: fixed;
top: 50%;
left: 50%;
padding: 10px;
transform: translate(-50%, -50%);
border: 1px solid;
z-index: 200;
background-color: #ffffff;
}
#LSPLog button{
position: fixed;
top: 10px;
right: 10px;
z-index: 200;
}
#LSPLog #LSPLogList{
width: 500px;
height: 500px;
overflow: auto;
color: #424242;
}
#LSPLog li:nth-child(odd){
background-color: #f5f5f5;
}
#LSPLog details{
padding: 2px;
}
/* 代码编辑器 */
#OJBetter_editor{
box-sizing: border-box;
height: 600px;
border: 1px solid #d3d3d3;
width: 100%;
resize: vertical;
display: flex;
flex-direction: column;
}
#OJBetter_editor.fullscreen{
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
z-index: 2000;
}
#OJBetter_editor.bottom{
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 50vh;
z-index: 2000;
}
.ojb_btn.exit_button_bottom {
position: fixed;
bottom: 30px;
right: 15px;
z-index: 2000;
height: 28px;
}
/* monaco */
#OJBetter_monaco {
flex: 1;
min-height: 0;
width: 100%;
}
#OJBetter_monaco .highlight {
border: 1px solid #ffffff00;
background-color: #ffffff00!important
}
.monaco-hover hr {
margin: 4px -8px 4px !important;
}
/* 状态底栏 */
#OJBetter_statusBar{
height: 22px;
font-size: 12px;
color: #757575;
border: 1px solid #d3d3d3;
background-color: #f8f8f8;
padding: 3px;
box-sizing: border-box;
}
/* 提交 */
#OJBetter_submitDiv{
display: flex;
gap: 5px;
padding-top: 15px;
height: 50px;
box-sizing: border-box;
}
#OJBetter_submitDiv >* {
border-radius: 6px;
}
#OJBetter_submitDiv > button {
height: 100%;
aspect-ratio: 1 / 1;
}
#SubmitButton {
color: #fff;
background-color: #209978;
border-color: #17795E;
}
#SubmitButton:hover {
background-color: #17795e;
}
#SubmitButton.disabled {
background-color: red;
animation: shake 0.07s infinite alternate;
}
.topLeftDiv > select {
height: 100%;
padding: 5px 10px;
border-radius: 6px;
border-style: solid;
border: 1px solid #ced4da;
color: #212529;
}
/* 调试 */
.OJBetter_loding{
padding: 6px 0px 0px 5px;
height: 22px;
}
#CompilerArgsInput{
flex-grow: 1;
width: 100%;
height: 100%;
margin-bottom: 10px;
padding: 5px 10px;
border-radius: 6px;
box-sizing: border-box;
border: 1px solid #ccc;
box-shadow: inset 0px 1px 1px rgba(0,0,0,.075);
}
#CompilerArgsInput[disabled] {
cursor: not-allowed;
}
#CompilerSetting{
font-size: 14px;
margin-top: 10px;
display: none;
}
#CompilerSetting select, #CompilerSetting textarea{
padding: 4px 10px;
border-radius: 6px;
border-style: solid;
border: 1px solid #ced4da;
color: #212529;
}
#CompilerBox{
display: grid;
margin-top: 10px;
border: #d0d7de solid 1px;
border-radius: 6px;
}
#CompilerBox > * {
margin: 5px;
}
/* 自定义样例 */
#customTestBlock {
margin-top: 10px;
font-size: 14px;
color: #616161;
border: 1px solid #d3d3d3;
box-sizing: border-box;
position: relative;
}
#customTestBlock #customTests{
border-top: 1px solid #d3d3d3;
margin: 0px 0px 40px 0px;
}
#customTestBlock summary {
cursor: pointer;
padding: 10px;
}
#customTestBlock textarea {
resize: vertical;
}
.sampleDiv {
color: #727378;
background-color: #FAFAFA;
padding: 5px;
margin-bottom: 10px;
box-shadow: inset 0 0 1px #0000004d;
position: relative;
}
.dynamicTextarea {
width: 98%;
height: 120px;
margin: 10px 5px;
border: 1px solid #E0E0E0;
}
.deleteCustomTest {
cursor: pointer;
position: absolute;
top: 5px;
right: 5px;
display: flex;
fill: #9E9E9E;
padding: 2px 2px;
border-radius: 4px;
border: 1px solid #ffffff00;
background-color: #ffffff00;
align-items: center;
}
.deleteCustomTest:hover {
fill: #EF5350;
border: 1px solid #ef9a9a;
background-color: #FFEBEE;
}
#addCustomTest {
cursor: pointer;
position: absolute;
bottom: 5px;
right: 5px;
padding: 3px 10px;
color: #795548;
border: 1px solid #ccc;
border-radius: 4px;
background-color: #FAFAFA;
}
#addCustomTest:hover {
background-color: #f5f5f5;
}
/* 调试结果 */
#statePanel{
display: none;
padding: 5px;
margin-top: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}
.test-case {
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}
.test-case:not(:first-child){
margin-top: 5px;
}
.test-case > * {
margin: 5px 0px;
}
.test-case > :first-child {
margin-top: 0px;
}
.test-case > :last-child {
margin-bottom: 0px;
}
.test-case-title, .test-case-status {
font-size: 16px;
display: inline;
}
.test-case-status{
margin-left: 5px;
}
.test-case-status.error{
color: red;
}
.test-case-status.success{
color: #449d44;
}
.test-case-judge, .judge-checker {
font-size: 13px;
}
/* 差异对比 */
.output_diff {
color: #5d4037;
margin: 5px 0px;
display: grid;
border: 1px solid #bcaaa4;
font-size: 13px;
font-family: Consolas, "Lucida Console", "Andale Mono", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace;
overflow: auto;
}
.output_diff .added {
background-color: #c8f7c5;
user-select: none;
}
.output_diff .removed {
background-color: #f7c5c5;
}
.output_diff .diffLine {
display: flex;
}
.output_diff .diffLine:nth-child(odd) {
background-color: #f5f5f5;
}
.lineNo {
display: flex;
align-items: center;
justify-content: center;
width: 17px;
color: #BDBDBD;
font-size: 10px;
border-right: 1px solid;
user-select: none;
}
.lineContent {
display: grid;
width: 100%;
}
.lineContent>span {
height: 16px;
padding-left: 3px;
}
.output_no_diff {
padding: 5px;
border: 1px solid #ddd;
}
.diff_note {
font-size: 10px;
}
/* 网站本地化替换规则标记 */
.markingTextReplaceRule{
color: #FFF3E0;
background-color: #FF9800;
}
/* SelectPage样式 */
.sp_input {
padding: 4px 6px px !important;
height: 20px !important;
min-height: 20px !important;
line-height: 20px !important;
}
div.sp_clear_btn {
padding: 0px !important;
}
/* 移动设备 */
@media (max-device-width: 450px) {
.ojb_btn{
height: 2em;
font-size: 1.2em;
}
.ojb_btn.OJBetter_setting{
height: 2.5em;
font-size: 1em;
}
.OJBetter_setting_menu{
width: 90%;
}
.OJBetter_setting_menu label, #darkMode_span, #loaded_span, .OJBetter_setting_menu_label_text,
.OJBetter_setting_sidebar li{
font-size: 1em;
}
.translate-problem-statement{
font-size: 1.2em;
}
.OJBetter_modal{
font-size: 1.5em;
}
.OJBetter_setting_list, .translate-problem-statement{
padding: 0.5em;
}
.OJBetter_setting_menu_label_text{
height: 2.5em;
padding: 0.5em;
}
#pagBar #jump-input, #pagBar #items-per-page, .OJBetter_modal button{
height: 2.5em;
font-size: 1em;
}
.translate-problem-statement p, .translate-problem-statement ul li{
line-height: 1.5em !important;
}
.OJBetter_contextmenu_label_text{
height: 3em;
font-size: 1em;
}
}
/* 覆盖网站原本的样式 */
div#select-lang {
padding: 0px;
}
`);
/**
* 添加一些依赖库和条件加载的css样式
*/
function addDependencyStyles() {
GM_addStyle(GM_getResourceText("xtermcss"));
GM_addStyle(GM_getResourceText("selectpagecss"));
GM_addStyle(GM_getResourceText("dialogpolyfillcss"));
// 自定义图标大小
GM_addStyle(`
.iconfont {
font-size: ${OJBetter.preference.iconButtonSize}px;
}
`);
}
/**
* 添加包含i18n内容的css样式
*/
function addI18nStyles() {
GM_addStyle(`
/* 加载鼠标悬浮覆盖层css */
.ojb-overlay::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: repeating-linear-gradient(135deg, rgb(77 208 225 / 30%), rgb(77 208 225 / 30%) 30px, rgb(77 208 225 / 10%) 0px, rgb(77 208 225 / 10%) 55px);
z-index: 100;
}
.ojb-overlay::after {
content: '${i18next.t('targetArea', { ns: 'common' })}';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #00695C;
font-size: 16px;
font-weight: bold;
z-index: 100;
}
.config::before {
content: "${i18next.t('common.configManageTitle', { ns: 'settings' })}";
display: block;
height: 20px;
background-color: #f0f8ff;
border: 1px solid #c5cae9;
border-bottom: 0px;
line-height: 20px;
padding: 2px 10px;
border-radius: 8px 8px 0px 0px;
box-sizing: content-box;
}
.config.missing::before {
content: "${i18next.t('common.missing.radio', { ns: 'settings' })}";
background-color: #fef0f0;
color: #f56c6c;
border: 1px solid #fab6b6;
}
`);
}
// ------------------------------
// 一些工具类
// ------------------------------
/**
* 自定义错误类,以区分不同的错误类型
*/
class OJB_GMError extends Error {
constructor(type, message, originalError) {
super(message);
this.name = 'GMError';
this.type = type;
this.stack = originalError.stack;
Object.assign(this, originalError);
}
}
/**
* 文本块替换/恢复类
*/
class TextBlockReplacer {
constructor() {
/** @type {string[]} 匹配项 */
this.matches = [];
/** @type {Map<string, string>} 待还原项 */
this.replacements = new Map();
/** @type {Map<string, string>} 暂时未找到的待还原项 */
this.tempReplacements = new Map();
/** @type {string} 替换符号 */
this.replaceSymbol = OJBetter.translation.replaceSymbol;
}
/**
* 替换文本
* @param {string} text 原文本
* @param {RegExp} regex 匹配规则
* @returns {string} 替换后的文本
*/
replace(text, regex) {
// 优化序数词翻译
let isOrdinal = (text) => {
return Boolean(text.match(/\$\d+?\$[(st)(nd)(rd)(th)]/g));
}
let ordinalTranslation = (text) => {
return '第 ' + text.match(/\$\d+?\$/g)[0] + ' 个';
};
this.matches = text.match(regex) || [];
try {
for (let i = 0; i < this.matches.length; i++) {
const match = this.matches[i];
const id = OJB_getRandomNumber(8);
let replacement = '';
switch (this.replaceSymbol) {
case "1":
replacement = `【${id}】`;
break;
case "2":
replacement = `{${id}}`;
break;
case "3":
replacement = `[${id}]`;
break;
default:
replacement = `【${id}】`;
break;
}
text = text.replace(match, replacement);
if (isOrdinal(match) && OJBetter.translation.targetLang === 'zh')
this.replacements.set(id, ordinalTranslation(match));
else this.replacements.set(id, match);
}
} catch (e) { }
return text;
}
/**
* 恢复替换的文本
* @param {string} text 还原前的文本
* @returns {string} 还原后的文本
*/
recover(text) {
let textCopy = text;
/**
* 替换回文本
* @param {string} replacement 替换的文本
* @param {string} regexPattern 匹配规则
* @returns {void}
*/
const replaceText = (replacement, regexPattern) => {
const latexMatch = '(?<latex_block>\\$\\$(\\\\\\$|[^\\$])*?\\$\\$)|(?<latex_inline>\\$(\\\\\\$|[^\\$])*?\\$)|';
const regex = new RegExp(latexMatch + regexPattern, 'g');
textCopy = textCopy.replace(regex, (match, ...args) => {
// LaTeX中的不替换
const groups = args[args.length - 1]; // groups是replace方法的最后一个参数
if (groups.latex_block || groups.latex_inline) return match;
// 没有空格则加一个
const offset = args[args.length - 3]; // offset是replace方法的倒数第三个参数
let leftSpace = "", rightSpace = "";
if (!/\s/.test(textCopy[offset - 1])) leftSpace = " ";
if (!/\s/.test(textCopy[offset + match.length]) && /[\x20-\x7E]/.test(replacement[replacement.length - 1]))
rightSpace = " ";
return leftSpace + replacement + rightSpace;
});
};
/**
* 尝试还原
* @param {string} replacement 替换的文本
* @param {string} id 替换的 id
* @returns {boolean} 是否替换成功
*/
const tryRecover = (replacement, id) => {
// 尝试还原,如果还原成功,则从 replacements 中删除
const originalText = textCopy;
replaceText(replacement, `【\\s*${id}\\s*】|\\[\\s*${id}\\s*\\]|{\\s*${id}\\s*}`); // 替换符完整匹配(考虑了多出空格的情况)
replaceText(replacement, `【\\s*${id}(?![】\\d])|(?<![【\\d])${id}\\s*】|\\[\\s*${id}(?![\\]\\d])|(?<![\\[\\d])${id}\\s*\\]|{\\s*${id}(?![}\\d])|(?<![{\\d])${id}\\s*}`); // 替换符部分匹配
if (textCopy === originalText) {
// 如果文本没有变化,说明没有找到,加入到 tempReplacements
this.tempReplacements.set(id, replacement);
return false;
} else {
// 如果文本变化了,说明找到并成功替换,则删除
this.replacements.delete(id);
this.tempReplacements.delete(id);
return true;
}
}
// 处理 replacements 中的项
this.replacements.forEach((replacement, id) => {
tryRecover(replacement, id);
});
// 处理 tempReplacements 中的项
while (this.tempReplacements.size > 0) {
let found = false;
this.tempReplacements.forEach((replacement, id) => {
found = tryRecover(replacement, id) || found;
});
if (!found) break; // 如果这一轮没有找到任何项,终止循环
}
// 如果 tempReplacements 还有未找到的项
if (this.tempReplacements.size > 0) {
console.warn("There are still some replacements not found:", this.tempReplacements);
}
return textCopy;
}
}
// ------------------------------
// 一些工具函数
// ------------------------------
/**
* 格式化链接格式
* @param {string} url 链接字符串
* @returns {string} 清理后的链接字符串
*/
function OJB_cleanLink(url) {
if (url === null || url === undefined) return "";
// 替换'http://'为'https://'
let cleanUrl = url.replace(/^http:\/\//i, 'https://');
// 移除末尾的斜杠
cleanUrl = cleanUrl.replace(/\/$/, '');
return cleanUrl;
}
/**
* 深度比较两个对象或数组是否完全相等。
* @param {any} a - 第一个比较对象。
* @param {any} b - 第二个比较对象。
* @returns {boolean} - 如果两个对象或数组深度相等,则返回true,否则返回false。
*/
function OJB_deepEquals(a, b) {
if (a === b) return true;
if (typeof a !== 'object' || a === null || typeof b !== 'object' || b === null) return false;
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
for (let key of keysA) {
if (!b.hasOwnProperty(key)) return false;
if (!OJB_deepEquals(a[key], b[key])) return false;
}
return true;
}
/**
* 用于封装需要重试的异步函数
* @param {Function} task 需要封装的异步函数
* @param {Object} options 配置项
* @param {Number} options.maxRetries 重试次数,默认为 5
* @param {Number} options.retryInterval 重试时间间隔,默认为 0 毫秒
* @param {Function} options.errorHandler 错误处理函数,默认为抛出错误
* @param {...any} args task 函数的参数
* @returns {Promise} 返回 Promise
*/
async function OJB_promiseRetryWrapper(task, {
maxRetries = 5,
retryInterval = 0,
errorHandler = (err) => { throw err }
} = {}, ...args) {
let attemptsLeft = maxRetries;
while (attemptsLeft--) {
try {
return await task(...args);
} catch (err) {
if (attemptsLeft <= 0) {
return errorHandler(err, maxRetries, attemptsLeft);
}
if (retryInterval > 0) {
await OJB_delay(retryInterval);
}
}
}
}
/**
* GM_xmlhttpRequest 的 Promise 封装
* @param {Object} options GM_xmlhttpRequest 的参数
* @param {Boolean} isStream 是否为流式请求
* @returns {Promise<OJB_GMError>} 返回 Promise
*/
function OJB_GMRequest(options, isStream = false) {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
...options,
...(isStream ? {
onloadstart: resolve
} : {
onload: resolve
}),
onerror: (error) => reject(new OJB_GMError('error', 'An error occurred during the request.', error)),
ontimeout: (error) => reject(new OJB_GMError('timeout', 'The request timed out.', error)),
onabort: (error) => reject(new OJB_GMError('abort', 'The request was aborted.', error)),
});
});
}
/**
* 获取cookie
* @param {string} name cookie名称
* @returns {string} cookie值
*/
function OJB_getCookie(name) {
const cookies = document.cookie.split(";");
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
const [cookieName, cookieValue] = cookie.split("=");
if (cookieName === name) {
return decodeURIComponent(cookieValue);
}
}
return "";
}
/**
* 检查是否仍在同一浏览器会话中
* @param {string} sessionKey - 会话键名,用于标识会话
* @returns {boolean} - 如果在当前会话中之前已经设置过这个键,则返回true,否则返回false
*/
function OJB_isSameBrowserSession(sessionKey) {
const fullCookieName = `OJB_Session_${sessionKey}`;
const sessionValue = OJB_getCookie(fullCookieName);
if (sessionValue === "") {
document.cookie = `${fullCookieName}=true; path=/`;
return false;
}
return true;
}
/**
* 随机数生成
* @param {number} numDigits 位数
* @returns {number} 一个随机数
*/
function OJB_getRandomNumber(numDigits) {
let min = Math.pow(10, numDigits - 1);
let max = Math.pow(10, numDigits) - 1;
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* 随机数生成-范围内
* @param {number} min 最小值
* @param {number} max 最大值
* @returns {number} 一个随机数
*/
function OJB_getRandomNumberInRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* 防抖函数
* @param {Function} callback 回调函数
* @returns {Function}
*/
function OJB_debounce(callback) {
let timer;
let immediateExecuted = false;
const delay = 500;
return function () {
clearTimeout(timer);
if (!immediateExecuted) { callback.call(this); immediateExecuted = true; }
timer = setTimeout(() => { immediateExecuted = false; }, delay);
};
}
/**
* 为元素添加鼠标拖拽支持
* @param {JQuery<HTMLElement>} element 要添加拖拽支持的元素
* @returns {void}
*/
function OJB_addDraggable(element) {
let isDragging = false;
let x, y, l, t, nl, nt;
let isSpecialMouseDown = false; // 选取某些元素时不拖动
element.on('mousedown', function (e) {
isSpecialMouseDown = $(e.target).is('label, p, input, textarea, span, select, details, summary');
if (isSpecialMouseDown) return;
isDragging = true;
x = e.clientX;
y = e.clientY;
l = element.offset().left - $(window).scrollLeft();
t = element.offset().top - $(window).scrollTop();
element.css({ left: l + 'px', top: t + 'px', transform: 'none' });
$(document).on("mousemove", drag);
$(document).on("mouseup", stopDrag);
element.css('cursor', 'all-scroll');
});
const drag = (e) => {
if (!isDragging) return;
// 不执行拖动操作
if ($(e.target).is('label, p, input, textarea, span') || isSpecialMouseDown && !$(e.target).is('input, textarea')) return;
e.preventDefault();
const nx = e.clientX;
const ny = e.clientY;
nl = nx - (x - l);
nt = ny - (y - t);
element.css({ transform: `translate(${nx - x}px, ${ny - y}px)` });
};
const stopDrag = () => {
isDragging = false;
isSpecialMouseDown = false;
element.css('cursor', 'default');
// 在停止拖拽后,设置元素的left和top,并还原transform
element.css({ left: nl + 'px', top: nt + 'px', transform: 'none' });
$(document).off("mousemove", drag);
$(document).off("mouseup", stopDrag);
};
}
/**
* 切换元素的折叠/展开过渡动画
* @param {HTMLElement} element
*/
function OJB_toggleCollapseExpand(element) {
// 设置transitionend事件监听器的函数
const setTransitionListener = (listener) => {
const listenerName = `transitionEndListener${Date.now()}`;
window[listenerName] = listener;
element.addEventListener('transitionend', listener);
element.setAttribute('data-transition-end-listener', listenerName);
};
// 移除事件监听器的函数
const removeTransitionListener = () => {
const transitionEndListenerName = element.getAttribute('data-transition-end-listener');
if (transitionEndListenerName) {
element.removeEventListener('transitionend', window[transitionEndListenerName]);
element.removeAttribute('data-transition-end-listener');
}
};
const collapsed = element.getAttribute('data-collapsed') === 'true';
const sectionHeight = element.scrollHeight;
// 移除事件监听器
removeTransitionListener();
// 设置初始样式
element.style.overflow = 'hidden';
element.style.transition = 'height 0.3s ease-out 0s';
element.style.height = collapsed ? `0px` : `${sectionHeight}px`;
element.style.opacity = collapsed ? '' : '1';
// 需要立即开始动画
requestAnimationFrame(() => {
// 设置结束样式
element.style.height = collapsed ? `${sectionHeight}px` : `0px`;
});
const transitionEndListener = (event) => {
if (event.propertyName === 'height') {
if (collapsed) {
// 展开后的设置
element.style.height = '';
element.style.overflow = '';
} else {
// 折叠后的设置
element.style.opacity = '0';
}
removeTransitionListener();
}
};
setTransitionListener(transitionEndListener);
// 更新data-collapsed属性
element.setAttribute('data-collapsed', collapsed ? 'false' : 'true');
}
/**
* 获取外部JSON并转换为Object
* @param {string} url JSON Url
* @param {boolean} [nacache=true] 是否不使用缓存
* @returns {Promise<Object>} JSON Object
*/
async function OJB_getExternalJSON(url, nacache = true) {
const response = await OJB_GMRequest({
method: "GET",
url: url,
nocache: nacache
});
try {
return JSON.parse(response.responseText);
} catch (e) {
throw new Error(`JSON parse error\n${e}`);
}
}
/**
* 创建确认对话框dialog
* @param {string} title 标题
* @param {string} content 内容
* @param {string[]} buttons 按钮 (取消 确定) 可以为null
* @param {boolean} renderMarkdown 是否使用markdown渲染文本
* @returns {Promise<boolean>} 用户点击了确定按钮返回true, 否则返回false
*/
function OJB_createDialog(title, content, buttons, renderMarkdown = false) {
return new Promise(resolve => {
let contentHtml = content;
if (renderMarkdown) {
const md = window.markdownit();
contentHtml = md.render(content);
}
const dialog = OJB_safeCreateJQElement(`
<dialog class="OJBetter_modal">
<h2>${title}</h2>
<div class="content">${contentHtml}</div>
</dialog>
`);
const buttonbox = OJB_safeCreateJQElement(`<div class="buttons"></div>`);
const cancelButton = OJB_safeCreateJQElement(`<button class="cancelButton">${buttons[0]}</button>`)
.addClass("secondary");
const continueButton = OJB_safeCreateJQElement(`<button class="continueButton">${buttons[1]}</button>`);
if (buttons[0] !== null) buttonbox.append(cancelButton);
if (buttons[1] !== null) buttonbox.append(continueButton);
dialog.append(buttonbox);
$('body').append(dialog);
OJB_showModal(dialog);
OJB_addDraggable(dialog);
continueButton.click(function () {
OJB_closeAndRemoveModal(dialog);
resolve(true);
});
cancelButton.click(function () {
OJB_closeAndRemoveModal(dialog);
resolve(false);
});
});
}
/**
* 显示模态对话框并阻止页面滚动,同时考虑滚动条宽度变化和原始marginRight
* @param {JQuery<HTMLElement>} element
*/
function OJB_showModal(element) {
const dialog = element.get(0);
dialogPolyfill.registerDialog(dialog);
dialog.showModal();
OJBetter.state.openDialogCount++;
if (OJBetter.state.openDialogCount === 1) {
const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
// 获取原始的html marginRight,考虑到可能的非数字值,比如auto
const originalMarginRight = window.getComputedStyle(document.documentElement).marginRight;
const marginRightValue = parseFloat(originalMarginRight) || 0; // 将非数字值转换为0
if (scrollbarWidth > 0) {
// 保存原始的marginRight,并设置新的值以补偿滚动条宽度
document.documentElement.style.setProperty('--original-margin-right', originalMarginRight);
document.documentElement.style.marginRight = `${marginRightValue + scrollbarWidth}px`;
}
// 保存原始的overflow样式
document.documentElement.setAttribute('data-original-overflow', document.documentElement.style.overflow);
document.documentElement.style.overflow = 'hidden';
}
const allowScrollIfNeeded = () => {
OJBetter.state.openDialogCount--;
if (OJBetter.state.openDialogCount === 0) {
// 恢复原始的html marginRight和overflow样式
const originalMarginRight = document.documentElement.style.getPropertyValue('--original-margin-right');
document.documentElement.style.marginRight = originalMarginRight;
document.documentElement.style.removeProperty('--original-margin-right');
const originalOverflow = document.documentElement.getAttribute('data-original-overflow');
document.documentElement.style.overflow = originalOverflow;
document.documentElement.removeAttribute('data-original-overflow');
}
};
dialog.addEventListener('close', allowScrollIfNeeded);
}
/**
* 关闭并移除模态对话框
* @param {JQuery<HTMLElement>} element
*/
function OJB_closeAndRemoveModal(element) {
const dialog = element.get(0);
dialog.close();
dialog.remove();
}
/**
* 关闭并移除模态对话框
* @param {JQuery<HTMLElement>} element
*/
function OJB_closeModal(element) {
const dialog = element.get(0);
dialog.close();
}
/**
* 清除i18next的缓存数据并刷新
*/
function clearI18nextCache() {
Object.keys(localStorage)
.filter(key => key.startsWith('i18next_res_'))
.forEach(key => localStorage.removeItem(key));
window.location.reload();
}
/**
* 清除网站本地化数据
*/
async function clearWebsiteL10nData() {
OJBetter.common.database.localizeSubsData.clear().then(() => {
console.log('localizeSubsData table has been cleared');
window.location.reload();
}).catch((error) => {
console.error('Failed to clear localizeSubsData table:', error);
});
}
/**
* 从Pre代码块中获取原始代码
* @param {HTMLElement} element pre代码块元素
* @returns {string|null} 代码文本
*/
function OJB_getCodeFromPre(element) {
/**
* 从Ace格式化的代码块中获取原始代码
* @param {HTMLElement} element pre代码块元素
* @returns {string} 代码文本
*/
const getCodeFromAcePre = function (element) {
const editor = ace.edit(element);
return editor.getValue();
}
/**
* 从Pretty格式化的代码块中获取原始代码-1
* 代码直接存放在 pre 元素中
* @param {HTMLElement} element pre代码块元素
* @returns {string} 代码文本
*/
const getCodeFromPrettyPre = function (element) {
return Array.from(element.querySelectorAll('li')).map(function (li) {
return li.textContent;
}).join('\n');
}
/**
* 从Pretty格式化的代码块中获取原始代码-2
* 代码存放在子元素 code 中
* @param {HTMLElement} element pre代码块元素
* @returns {string} 代码文本
*/
const getCodeFromPreChild = function (element) {
const code = element.querySelector("code.prettyprint");
if (code.classList.contains("linenums")) {
return getCodeFromPrettyPre(element);
} else {
// return element.querySelector("code.prettyprint").textContent;//这个把<br>换行丢了
// 用 DOMParser 转换 HTML 代码,正确替换换行。
const parser = new DOMParser();
const doc = parser.parseFromString(code.innerHTML.replace(/<br\s*\/?>/g, "\n"), "text/html");
return doc.body.textContent;
}
}
let result;
if (element.id === "submission-code") {
result = getCodeFromAcePre(element);
} else if (element.classList.contains("prettyprint")) {
result = getCodeFromPrettyPre(element);
} else if (element.querySelector("code.prettyprint")) {
result = getCodeFromPreChild(element);
} else {
result = "";
}
result = result.replace(/\u00A0/g, ''); // 过滤文本中的U+00a0字符(由 造成的)
return result;
}
/**
* 判断代码的语言
* @param {string} code 代码文本
* @returns {string} 可能的语言
*/
function OJB_codeLangDetect(code) {
result = hljs.highlightAuto(code);
return result.language;
}
/**
* 获取指定命名空间下的所有i18n翻译键值对。
*
* @param {string} namespace - 要获取键值对的i18next命名空间。
* @returns {Map<string, string>} 一个包含命名空间下所有键值对的Map对象。
*/
function OJB_getAllI18nKeysForNamespace(namespace) {
const language = i18next.language; // 获取当前语言
const resources = i18next.store.data[language]; // 获取当前语言的所有资源
const nsResources = resources[namespace]; // 获取特定命名空间的资源
const resultMap = new Map();
if (nsResources) {
// 遍历命名空间下的所有键值对,并添加到Map中
Object.keys(nsResources).forEach(key => {
resultMap.set(key, nsResources[key]);
});
} else {
console.log(`No resources found for namespace "${namespace}"`);
}
return resultMap;
}
/**
* 更新检查
*/
async function checkScriptVersion() {
try {
const versionResponse = await OJB_GMRequest({
method: "GET",
url: "https://aowuucdn.oss-accelerate.aliyuncs.com/script/versions.json",
timeout: 10 * 1e3,
nocache: true
});
const versionData = JSON.parse(versionResponse.responseText);
const { [OJBetter.state.formatName]: { dev: version_dev, release: version_release } } = versionData;
const baseUrls = {
// greasyfork: 'https://update.greasyfork.org.cn/scripts/465777/Codeforces%20Better%21.user.js',
greasyfork: 'https://update.greasyfork.org.cn/scripts/471106/Atcoder%20Better%21.user.js',
github: `https://github.com/beijixiaohu/OJBetter/raw/main/script/${OJBetter.about.updateChannel}/${OJBetter.state.formatName}.user.js`,
aliyunoss: `https://aowuucdn.oss-accelerate.aliyuncs.com/script/${OJBetter.about.updateChannel}/${OJBetter.state.formatName}.user.js`
};
/** @type {string} 更新跳转url */
const updateUrl = baseUrls[OJBetter.about.updateSource];
/** @type {string} 是否暂时跳过cookie */
const skipUpdate = OJB_getCookie("skipUpdate");
/** @type {string} 当前更新频道的最新版本 */
const version = OJBetter.about.updateChannel == "release" ? version_release : version_dev;
if (OJB_compareVersions(version, OJBetter.state.version) === 1 && skipUpdate !== "true") {
const updateConfirmed = await OJB_createDialog(
i18next.t('update.title', { ns: 'dialog', scriptName: OJBetter.state.name }),
i18next.t('update.content', { ns: 'dialog', oldVersion: OJBetter.state.version, newVersion: version }),
[
i18next.t('update.buttons.0', { ns: 'dialog' }),
i18next.t('update.buttons.1', { ns: 'dialog' })
],
true
);
if (updateConfirmed) {
window.location.href = updateUrl;
} else {
document.cookie = "skipUpdate=true; path=/";
}
}
} catch (error) {
console.error("Update check failed: ", error);
}
}
/**
* 公告
*/
async function showAnnounce() {
/** @type {string} 最新公告版本*/
const lastAnnounceVer = i18next.t('lastVersion', { ns: 'announce' });
if (OJB_compareVersions(OJBetter.state.version, OJBetter.state.lastAnnounceVer) === 1) {
const title = `🎉${i18next.t('announce.title', { ns: 'dialog' })} ${OJBetter.state.version}`;
/** @type {Boolean} 是否是新的公告 */
const isNewAnnounceVer = OJB_compareVersions(lastAnnounceVer, OJBetter.state.lastReadAnnounceVer) === 1;
/** @type {Boolean} 是否展示新的公告(高于当前版本的测试公告不展示) */
const showNewAnnounceVer = OJB_compareVersions(lastAnnounceVer, OJBetter.state.version) !== 1;
/**
* 获取公告的内容
* @returns {string} 公告内容
*/
const getAnnounceContent = function () {
// 获取公告
const announceMap = OJB_getAllI18nKeysForNamespace('announce');
// 移除 'lastVersion' 键
announceMap.delete('lastVersion');
// 将 Map 转换为数组并根据版本号排序
const sortedVersions = [...announceMap.keys()].sort(OJB_compareVersions).reverse();
let content = "";
sortedVersions.forEach(version => {
content += `### ${version}\n\n`; // 使用版本号作为标题
content += announceMap.get(version); // 添加对应版本的公告内容
content += "\n\n";
});
return content;
};
const content = (() => {
if (isNewAnnounceVer && showNewAnnounceVer) {
return `${i18next.t('announce.prefix', { ns: 'dialog' })}\n\n${getAnnounceContent()}`;
} else {
return i18next.t('announce.divContent', { ns: 'dialog' });
}
})();
const ok = await OJB_createDialog(
title,
content,
[
null,
i18next.t('announce.buttons.0', { ns: 'dialog' })
],
true
); //跳过折叠块确认
if (ok) {
if (isNewAnnounceVer && showNewAnnounceVer) {
GM_setValue('lastReadAnnounceVer', lastAnnounceVer);
}
GM_setValue('lastAnnounceVer', OJBetter.state.version);
}
}
};
/**
* 页面顶部提示信息alert类
*/
class LoadingMessage {
constructor() {
this._statusElement = null;
this._isDisplayed = false;
this.init();
}
/**
* 初始化加载提示信息
*/
init() {
this._statusElement = this.createStatusElement();
this.insertStatusElement();
}
/**
* 创建提示信息元素
*/
createStatusElement() {
const statusElement = $("<div></div>").addClass("alert OJBetter_alert")
.css({
"margin": "1em",
"text-align": "center",
"position": "relative"
}).hide();
return statusElement;
}
/**
* 插入提示信息
* @returns {void}
*/
insertStatusElement() {
// (OJBetter.typeOfPage.is_mSite ? $("header") : $(".menu-box:first").next()).after(this._statusElement);
$("#main-container").prepend(this._statusElement);
}
/**
* 显示提示信息
*/
showStatus() {
this._statusElement.show();
this._isDisplayed = true;
}
/**
* 隐藏提示信息
*/
hideStatus() {
this._statusElement.fadeOut(500);
this._isDisplayed = false;
}
/**
* 移除提示信息
*/
removeStatus() {
this._statusElement.remove();
this._isDisplayed = false;
}
/**
* 更新提示信息
* @param {string} text 提示信息文本
* @param {string} type 提示信息类型,可选值:info, success, warning, error
* @param {number} timeout 提示信息显示的持续时间(毫秒), 默认为无限长
*/
updateStatus(text, type = 'info', timeout = Infinity, isMarkdown = false) {
if (isMarkdown) {
let md = window.markdownit({
html: !is_escapeHTML,
});
text = md.render(text);
}
this._statusElement.html(text).removeClass("alert-info alert-success alert-warning alert-error").addClass(`alert-${type}`);
if (!this._isDisplayed) {
this.showStatus();
}
if (timeout !== Infinity) {
setTimeout(() => {
this.hideStatus();
}, timeout);
}
}
}
/**
* 获取网站本地化的数据
* @param {*} localizationLanguage 本地化语言
* @returns {Promise<Object>} 本地化数据
*/
async function getLocalizeWebsiteJson(localizationLanguage) {
let data = await OJBetter.common.database.localizeSubsData.get(localizationLanguage);
let url = localizationLanguage === "zh" ?
`https://aowuucdn.oss-accelerate.aliyuncs.com/resources/subs/${OJBetter.state.formatName}.json` :
`https://aowuucdn.oss-accelerate.aliyuncs.com/i18n/${localizationLanguage}/resources/subs/${OJBetter.state.formatName}.json`;
if (data) data = data.data;
if (!data) {
// 如果本地没有数据,从远端获取并保存
data = await OJB_getExternalJSON(url);
await OJBetter.common.database.localizeSubsData.put({ lang: localizationLanguage, data: data });
} else {
// 如果本地有数据,检查是否已经在当前会话中尝试过更新
const sessionKey = `ojb_updateL10nWebsiteJson_${localizationLanguage}`;
if (!OJB_isSameBrowserSession(sessionKey)) {
// 如果尚未更新,则在后台更新
(async () => {
try {
const newData = await OJB_getExternalJSON(url);
await OJBetter.common.database.localizeSubsData.put({ lang: localizationLanguage, data: newData });
console.log("Website local data has been refreshed!");
} catch (error) {
console.error('Failed to update localization data:', error);
}
})();
}
}
return data;
}
/**
* 网站本地化替换
* @returns
*/
async function localizeWebsite() {
if (OJBetter.localization.websiteLang === "initial") return;
// 设置网页语言
var htmlTag = document.getElementsByTagName("html")[0];
htmlTag.setAttribute("lang", OJBetter.localization.websiteLang);
// 获取网站本地化的数据
var subs = await getLocalizeWebsiteJson(OJBetter.localization.websiteLang);
/**
* 文本节点遍历替换
* @param {JQuery} $nodes jQuery对象
* @param {Object} textReplaceRules 文本替换规则对象
* @param {string} key 应用的规则集的名字
*/
const traverseTextNodes = ($nodes, textReplaceRules, key) => {
if (!$nodes) return;
$nodes.each((_, node) => {
if (node.nodeType === Node.TEXT_NODE) {
Object.entries(textReplaceRules).forEach(([match, replace]) => {
try {
const regex = new RegExp(match, 'g');
const beforeText = node.textContent;
node.textContent = node.textContent.replace(regex, replace);
if (node.textContent !== beforeText && OJBetter.dev.isRuleMarkingEnabled) {
$(node).after(`<span class="markingTextReplaceRule">${key}</span>`);
}
} catch (error) {
console.error(`Error processing text replacement for match: ${match}`, error);
}
});
} else if (node.nodeType === Node.ELEMENT_NODE && node.tagName.toLowerCase() !== 'iframe') {
$(node).contents().each((_, childNode) => {
traverseTextNodes($(childNode), textReplaceRules, key);
});
}
});
};
/**
* value替换
* @param {JQuery} $nodes jQuery对象
* @param {Object} valueReplaceRules 值替换规则对象
* @param {string} key 应用的规则集的名字
*/
function traverseValueNodes($nodes, valueReplaceRules, key) {
if (!$nodes) return;
$nodes.each(function () {
let $node = $(this);
if ($node.is('[value]')) {
Object.keys(valueReplaceRules).forEach(match => {
const replace = valueReplaceRules[match];
const regex = new RegExp(match, 'g');
let currentValue = $node.val();
let newValue = currentValue.replace(regex, replace);
$node.val(newValue);
if (OJBetter.dev.isRuleMarkingEnabled) {
if (newValue !== currentValue) $($node).after(`<span class="markingTextReplaceRule">${key}</span>`);
}
});
} else {
$node.children().each(function () {
traverseValueNodes($(this), valueReplaceRules, key);
});
}
});
}
/**
* 严格的文本节点遍历替换
* 要求被替换文本严格与规则文本一致
* @param {JQuery} $nodes jQuery对象
* @param {Object} textReplaceRules 文本替换规则对象
* @param {string} key 应用的规则集的名字
*/
const strictTraverseTextNodes = ($nodes, textReplaceRules, key) => {
if (!$nodes) return;
$nodes.each((_, node) => {
if (node.nodeType === Node.TEXT_NODE) {
const trimmedNodeText = node.textContent.trim();
for (const [match, replacement] of Object.entries(textReplaceRules)) {
if (trimmedNodeText === match) {
const beforeText = node.textContent;
node.textContent = replacement;
if (node.textContent !== beforeText && OJBetter.dev.isRuleMarkingEnabled) {
$(node).after(`<span class="markingTextReplaceRule">${key}</span>`);
}
}
}
} else if (node.nodeType === Node.ELEMENT_NODE && node.tagName.toLowerCase() !== 'iframe') {
$(node).contents().each((_, childNode) => {
strictTraverseTextNodes($(childNode), textReplaceRules, key);
});
}
});
};
/**
* 应用文本替换
*/
let commonReplacements = subs.commonReplacements;
Object.entries(commonReplacements).forEach(([key, value]) => {
const classSelectors = Array.isArray(value.class) ? value.class : [value.class]; // 兼容,class的值可以为数组或者字符串
classSelectors.forEach(classSelector => {
if (value.isStrict) {
strictTraverseTextNodes(OJB_safeCreateJQElement(`${classSelector}`), value.rules, key);
} else {
traverseTextNodes(OJB_safeCreateJQElement(`${classSelector}`), value.rules, key);
}
});
});
/**
* 应用value替换
*/
let InputValueReplacements = subs.InputValueReplacements;
Object.entries(InputValueReplacements).forEach(([key, value]) => {
const classSelectors = Array.isArray(value.class) ? value.class : [value.class];
classSelectors.forEach(classSelector => {
traverseValueNodes(OJB_safeCreateJQElement(`${classSelector}`), value.rules, key);
});
});
/**
* 动态添加的文本的替换
*/
let dynamicReplacements = subs.dynamicReplacements;
Object.entries(dynamicReplacements).forEach(([key, value]) => {
const classSelectors = Array.isArray(value.class) ? value.class : [value.class]; // 兼容,class的值可以为数组或者字符串
classSelectors.forEach(classSelector => {
OJB_observeElement({
selector: classSelector,
callback: (node) => {
// let popupContent = node.textContent.replace(/^×/, ''); // 去除开头多余的 '×' 字符
if (value.isStrict) {
strictTraverseTextNodes(OJB_safeCreateJQElement(`${classSelector}`), value.rules, key);
} else {
traverseTextNodes(OJB_safeCreateJQElement(`${classSelector}`), value.rules, key);
}
}
});
});
});
// // 杂项
// (function () {
// // 选项汉化input[type="radio"]
// var translations = {
// "as individual participant": "个人",
// "as a team member": "作为一个团队成员",
// };
// $('input[type="radio"]').each(function () {
// var tag = $(this).parent().contents().filter(function () {
// return this.nodeType === Node.TEXT_NODE;
// });
// for (var i = 0; i < tag.length; i++) {
// var text = tag[i].textContent.trim();
// if (translations.hasOwnProperty(text)) {
// $(this).addClass(text);
// tag[i].replaceWith(translations[text]);
// break;
// }
// }
// });
// })();
// (function () {
// var translations = {
// "(standard input\/output)": "标准输入/输出",
// };
// $("div.notice").each(function () {
// var tag = $(this).children().eq(0).text();
// for (var property in translations) {
// if (tag.match(property)) {
// $(this).children().eq(0).text(translations[property]);
// break;
// }
// }
// });
// })();
// // 轻量站特殊
// if (OJBetter.typeOfPage.is_mSite) {
// traverseTextNodes($('nav'), commonReplacements['.second-level-menu']['rules']);
// }
// if (OJBetter.typeOfPage.is_mSite) {
// (function () {
// var translations = {
// "Announcements": "公告",
// "Submissions": "提交记录",
// "Contests": "比赛",
// };
// $(".caption").each(function () {
// var optionValue = $(this).text();
// if (translations[optionValue]) {
// $(this).text(translations[optionValue]);
// }
// });
// })();
// }
};
// i18next 本地缓存
const i18nextLocalStorageBackend = (function () {
class Storage {
constructor(options) {
this.store = options.store;
}
setItem(key, value) {
if (this.store) {
try {
this.store.setItem(key, value);
} catch (e) {
// 存储失败时静默处理
}
}
}
getItem(key) {
if (this.store) {
try {
return this.store.getItem(key);
} catch (e) {
// 获取失败时静默处理
}
}
return undefined;
}
}
function getDefaults() {
let store = null;
try {
store = window.localStorage;
} catch (e) {
if (typeof window !== 'undefined') {
console.log('Failed to load local storage.', e);
}
}
return {
prefix: 'i18next_res_',
expirationTime: 7 * 24 * 60 * 60 * 1000,
defaultVersion: undefined,
versions: {},
store: store
};
}
class Cache {
constructor(services, options = {}) {
this.init(services, options);
this.type = 'backend';
}
init(services, options = {}) {
this.services = services;
this.options = { ...getDefaults(), ...this.options, ...options };
this.storage = new Storage(this.options);
}
read(language, namespace, callback) {
const nowMS = Date.now();
if (!this.storage.store) {
return callback(null, null);
}
const local = this.storage.getItem(`${this.options.prefix}${language}-${namespace}`);
if (local) {
const parsed = JSON.parse(local);
const version = this.getVersion(language);
if (parsed.i18nStamp &&
parsed.i18nStamp + this.options.expirationTime > nowMS &&
version === parsed.i18nVersion) {
const i18nStamp = parsed.i18nStamp;
delete parsed.i18nVersion;
delete parsed.i18nStamp;
return callback(null, parsed, i18nStamp);
}
}
return callback(null, null);
}
save(language, namespace, data) {
if (this.storage.store) {
data.i18nStamp = Date.now();
const version = this.getVersion(language);
if (version) {
data.i18nVersion = version;
}
this.storage.setItem(
`${this.options.prefix}${language}-${namespace}`,
JSON.stringify(data)
);
}
}
getVersion(language) {
return this.options.versions[language] || this.options.defaultVersion;
}
}
Cache.type = 'backend';
return Cache;
})();
// i18next 后端链
const i18nextChainedBackend = (function () {
'use strict';
const arr = [];
const each = arr.forEach;
const slice = arr.slice;
function defaults(obj) {
each.call(slice.call(arguments, 1), function (source) {
if (source) {
for (var prop in source) {
if (obj[prop] === undefined) obj[prop] = source[prop];
}
}
});
return obj;
}
function createClassOnDemand(ClassOrObject) {
if (!ClassOrObject) return null;
if (typeof ClassOrObject === 'function') return new ClassOrObject();
return ClassOrObject;
}
function getDefaults() {
return {
handleEmptyResourcesAsFailed: true,
cacheHitMode: 'none'
};
}
function handleCorrectReadFunction(backend, language, namespace, resolver) {
const fc = backend.read.bind(backend);
if (fc.length === 2) {
try {
const r = fc(language, namespace);
if (r && typeof r.then === 'function') {
r.then(function (data) {
return resolver(null, data);
}).catch(resolver);
} else {
resolver(null, r);
}
} catch (err) {
resolver(err);
}
return;
}
fc(language, namespace, resolver);
}
class Backend {
constructor(services, options = {}, i18nextOptions = {}) {
this.backends = [];
this.type = 'backend';
this.allOptions = i18nextOptions;
this.init(services, options);
}
init(services, options = {}, i18nextOptions = {}) {
this.services = services;
this.options = defaults(options, this.options || {}, getDefaults());
this.allOptions = i18nextOptions;
this.options.backends && this.options.backends.forEach((b, i) => {
this.backends[i] = this.backends[i] || createClassOnDemand(b);
this.backends[i].init(services, this.options.backendOptions && this.options.backendOptions[i] || {}, i18nextOptions);
});
if (this.services && this.options.reloadInterval) {
setInterval(() => this.reload(), this.options.reloadInterval);
}
}
read(language, namespace, callback) {
const bLen = this.backends.length;
const loadPosition = (pos) => {
if (pos >= bLen) return callback(new Error('non of the backend loaded data', true));
const isLastBackend = pos === bLen - 1;
const lengthCheckAmount = this.options.handleEmptyResourcesAsFailed && !isLastBackend ? 0 : -1;
const backend = this.backends[pos];
if (backend.read) {
handleCorrectReadFunction(backend, language, namespace, (err, data, savedAt) => {
if (!err && data && Object.keys(data).length > lengthCheckAmount) {
callback(null, data, pos);
savePosition(pos - 1, data);
if (backend.save && this.options.cacheHitMode && ['refresh', 'refreshAndUpdateStore'].indexOf(this.options.cacheHitMode) > -1) {
if (savedAt && this.options.refreshExpirationTime && savedAt + this.options.refreshExpirationTime > Date.now()) return;
const nextBackend = this.backends[pos + 1];
if (nextBackend && nextBackend.read) {
handleCorrectReadFunction(nextBackend, language, namespace, (err, data) => {
if (err) return;
if (!data) return;
if (Object.keys(data).length <= lengthCheckAmount) return;
savePosition(pos, data);
if (this.options.cacheHitMode !== 'refreshAndUpdateStore') return;
if (this.services && this.services.resourceStore) {
this.services.resourceStore.addResourceBundle(language, namespace, data);
}
});
}
}
} else {
loadPosition(pos + 1);
}
});
} else {
loadPosition(pos + 1);
}
};
const savePosition = (pos, data) => {
if (pos < 0) return;
const backend = this.backends[pos];
if (backend.save) {
backend.save(language, namespace, data);
savePosition(pos - 1, data);
} else {
savePosition(pos - 1, data);
}
};
loadPosition(0);
}
create(languages, namespace, key, fallbackValue, clb = () => { }, opts = {}) {
this.backends.forEach(b => {
if (!b.create) return;
const fc = b.create.bind(b);
if (fc.length < 6) {
try {
let r;
if (fc.length === 5) {
r = fc(languages, namespace, key, fallbackValue, opts);
} else {
r = fc(languages, namespace, key, fallbackValue);
}
if (r && typeof r.then === 'function') {
r.then(data => clb(null, data)).catch(clb);
} else {
clb(null, r);
}
} catch (err) {
clb(err);
}
return;
}
fc(languages, namespace, key, fallbackValue, clb, opts);
});
}
reload() {
const { backendConnector, languageUtils, logger } = this.services;
const currentLanguage = backendConnector.language;
if (currentLanguage && currentLanguage.toLowerCase() === 'cimode') return;
const toLoad = [];
const append = (lng) => {
const lngs = languageUtils.toResolveHierarchy(lng);
lngs.forEach(l => {
if (toLoad.indexOf(l) < 0) toLoad.push(l);
});
};
append(currentLanguage);
if (this.allOptions.preload) this.allOptions.preload.forEach(l => append(l));
toLoad.forEach(lng => {
this.allOptions.ns.forEach(ns => {
backendConnector.read(lng, ns, 'read', null, null, (err, data) => {
if (err) logger.warn(`loading namespace ${ns} for language ${lng} failed`, err);
if (!err && data) logger.log(`loaded namespace ${ns} for language ${lng}`, data);
backendConnector.loaded(`${lng}|${ns}`, err, data);
});
});
});
}
}
Backend.type = 'backend';
return Backend;
})();
/**
* i18next初始化
*/
async function initI18next() {
return new Promise((resolve, reject) => {
i18next
.use(i18nextChainedBackend)
.init({
lng: OJBetter.localization.scriptLang,
ns: ['common', 'settings', 'config', 'dialog', 'alert', 'translator',
'button', 'codeEditor', 'comments', 'announce', 'logMessage'], // 命名空间列表
defaultNS: 'settings',
fallbackLng: ['zh', OJBetter.translation.targetLang],
load: 'currentOnly',
debug: false,
backend: {
backends: [
i18nextLocalStorageBackend,
i18nextHttpBackend
],
backendOptions: [{
prefix: 'i18next_res_',
expirationTime: 7 * 24 * 60 * 60 * 1000,
defaultVersion: `v${OJBetter.state.version}`,
store: typeof window !== 'undefined' ? window.localStorage : null
}, {
/* options for secondary backend */
loadPath: (lng, ns) => {
if (lng[0] === 'zh' || lng[0] === 'zh-Hans') {
return `https://aowuucdn.oss-accelerate.aliyuncs.com/resources/locales/${OJBetter.state.formatName}/${ns}.json`;
}
return `https://aowuucdn.oss-accelerate.aliyuncs.com/i18n/${lng}/resources/locales/${OJBetter.state.formatName}/${ns}.json`;
}
}]
}
}, (err, t) => {
if (err) {
reject(err);
} else {
jqueryI18next.init(i18next, $, {
useOptionsAttr: true
});
resolve(t);
}
});
});
};
/**
* 抽象命令类
*/
class Command {
execute() { }
undo() { }
}
/**
* 命令调用者
*/
class CommandInvoker {
constructor() {
this.history = [];
}
/**
* 执行命令
* @param {Command} command 命令对象
*/
execute(command) {
this.history.push(command);
command.execute();
}
/**
* 撤销命令
*/
undo() {
const command = this.history.pop();
if (command) {
command.undo();
}
}
}
/**
* 接收者
*/
class DOMContainer {
/**
* @param {JQueryObject} element 容器对象
*/
constructor(element) {
this.containerElement = element;
}
/**
* 添加元素
* @param {JQueryObject} element 元素对象
* @returns {JQueryObject} 添加的元素对象
*/
add(element) {
this.containerElement.append(element);
return this.containerElement.children().last();
}
/**
* 删除元素
* @param {JQueryObject} element 元素对象
*/
remove(element) {
$(element).remove();
}
}
/**
* 具体命令类:添加元素
*/
class AddElementCommand extends Command {
/**
* @param {DOMContainer} receiver 接收者
* @param {JQueryObject} element 元素对象
*/
constructor(receiver, element) {
super();
this.receiver = receiver;
this.element = element;
this.addedElement = null;
}
execute() {
this.addedElement = this.receiver.add(this.element);
}
undo() {
if (this.addedElement) {
this.receiver.remove(this.addedElement);
}
}
}
/**
* 具体命令类:删除元素
*/
class RemoveElementCommand extends Command {
/**
* @param {DOMContainer} receiver 接收者
* @param {JQueryObject} element 元素对象
*/
constructor(receiver, element) {
super();
this.receiver = receiver;
this.element = element;
this.parent = $(element).parent();
this.nextSibling = $(element).next();
}
execute() {
this.receiver.remove(this.element);
}
undo() {
if (this.nextSibling.length > 0) {
$(this.element).insertBefore(this.nextSibling);
} else {
this.parent.append(this.element);
}
}
}
/**
* 验证器
*/
class Validator {
/**
* 表单必填项空值校验
*/
static required(structure) {
let config = {};
let allFieldsValid = true;
for (const key in structure) {
let value = key.type == 'checkbox' ?
$(key).prop("checked") : $(key).val();
config[structure[key].value] = value;
if (value || structure[key].require === false) {
$(key).removeClass('is_null');
} else {
$(key).addClass('is_null');
allFieldsValid = false;
}
}
return {
valid: allFieldsValid,
config: config
};
}
/**
* 表单合法性校验
*/
static checkKeyValuePairs(structure, config) {
let errorKeys = [];
let allFieldsValid = true;
for (const key in structure) {
const { check, value } = structure[key];
const fieldValue = config[value];
// 如果字段没有值或校验类型不匹配,则跳过当前迭代
if (!fieldValue) continue;
let isValid = true;
switch (check) {
case 'keyValuePairs':
isValid = Validator.keyValuePairs(fieldValue);
break;
case 'dotSeparatedPath':
isValid = Validator.validateDotSeparatedPath(fieldValue);
break;
default:
// 没有匹配的校验类型
continue;
}
Validator.toggleErrorDisplay(key, isValid);
if (!isValid) {
allFieldsValid = false;
errorKeys.push(key);
}
}
return {
valid: allFieldsValid,
errorKeys: errorKeys
};
}
/**
* 切换错误信息的显示和隐藏
* @param {string} key - 字段的键
* @param {boolean} isValid - 字段值是否有效
*/
static toggleErrorDisplay(key, isValid) {
const errorMessage = i18next.t('common.unValid', { ns: 'settings' });
const $errorSpan = $(key).prev('span.text-error');
if (!isValid) {
if (!$errorSpan.length) {
$(key).before(`<span class="text-error" style="color: red;">${errorMessage}</span>`);
}
} else {
$errorSpan.remove();
}
}
/**
* 键值对合法性校验
* @param {string} value
* @returns {boolean}
*/
static keyValuePairs(value) {
const keyValuePairs = value.split('\n');
// 允许值中包含空格和冒号
const regex = /^[a-zA-Z0-9_-]+\s*:\s*.+$/;
return keyValuePairs.every(pair => regex.test(pair));
}
/**
* 点分隔符路径格式校验,允许加减运算
* @param {string} path
* @returns {boolean}
*/
static validateDotSeparatedPath(path) {
// 正则表达式允许标识符之间有点号,标识符可以包含加减运算
const regex = /^([a-zA-Z0-9_-]+(\s*[\+\-]\s*[a-zA-Z0-9_-]+)*\.)*([a-zA-Z0-9_-]+(\s*[\+\-]\s*[a-zA-Z0-9_-]+)*)$/;
return regex.test(path);
}
}
/**
* 配置管理
*/
class ConfigManager {
/**
* @param {HTMLElement} element - 挂载容器
* @param {string} prefix - 前缀
* @param {object} tempConfig - 配置内容
* @param {object} structure - 配置结构
* @param {object} configHTML - 配置编辑页面HTML
* @param {boolean} allowChoice - 是否允许选择列表项
*/
constructor(element, prefix, tempConfig, structure, configHTML, allowChoice = true) {
/** @param 设置面板DIV */
this.settingMenuDiv = $('#OJBetter_setting_menu');
this.element = $(element);
this.prefix = prefix;
this.tempConfig = tempConfig;
this.structure = structure;
this.configHTML = configHTML;
this.allowChoice = allowChoice;
this.controlTip = null;
this.config_bar_list = null;
this.config_bar_ul = null;
this.config_add_button = null;
this.menu = null;
this.editItem = null;
this.deleteItem = null;
// 绑定方法
this.onAdd = this.onAdd.bind(this);
this.onEdit = this.onEdit.bind(this);
this.onDelete = this.onDelete.bind(this);
this.createListItemElement = this.createListItemElement.bind(this);
this.lastItemId = 0; // 列表中当前最后一个元素的id号
this.init();
}
init() {
this.createControlBar();
this.createContextMenu();
this.renderList();
}
/**
* 创建控制栏
*/
createControlBar() {
this.controlTip = OJB_safeCreateJQElement(`<div id='${this.prefix}configControlTip' style='color:red;'></div>`);
this.config_bar_list = OJB_safeCreateJQElement(`<div class='config_bar_list' id='${this.prefix}config_bar_list'></div>`);
this.config_bar_ul = OJB_safeCreateJQElement(`<ul class='config_bar_ul' id='${this.prefix}config_bar_ul'></ul>`);
this.element.append(this.controlTip);
this.element.append(this.config_bar_list);
this.config_bar_list.append(this.config_bar_ul);
}
/**
* 创建右键菜单
*/
createContextMenu() {
const menu = OJB_safeCreateJQElement(`<div id='config_bar_menu' style='display: none;'></div>`);
const editItem = OJB_safeCreateJQElement(`
<div class='config_bar_menu_item' id='config_bar_menu_edit'>
${i18next.t('contextMenu.edit', { ns: 'translator' })}
</div>`);
const deleteItem = OJB_safeCreateJQElement(`
<div class='config_bar_menu_item' id='config_bar_menu_delete'>
${i18next.t('contextMenu.delete', { ns: 'translator' })}
</div>`);
menu.append(editItem);
menu.append(deleteItem);
this.editItem = editItem;
this.deleteItem = deleteItem;
this.menu = menu;
this.settingMenuDiv.append(menu);
}
/**
* 关闭右键菜单
*/
closeContextMenu() {
this.menu.css({ display: "none" });
}
/**
* 创建列表项
* @param {string} text - 列表项文本
* @returns {HTMLElement} - 列表项
*/
createListItemElement(text) {
const id = OJB_getRandomNumber(4);
const li = $("<li></li>");
const radio = OJB_safeCreateJQElement(`<input type='radio' name='${this.prefix}config_item'></input>`)
.attr("value", text)
.attr("id", id)
.attr("prev_id", this.lastItemId)
.appendTo(li);
if (!this.allowChoice) {
radio.prop("disabled", true);
}
const label = OJB_safeCreateJQElement(`<label for='${id}' class='config_bar_ul_li_text'>${text}</label>`).appendTo(li);
this.lastItemId = id;
// 添加右键菜单
li.on("contextmenu", (event) => {
event.preventDefault();
this.menu.css({
display: "block",
left: event.pageX, top: event.pageY
});
const deleteItem = this.deleteItem;
const editItem = this.editItem;
// 移除旧事件
deleteItem.off("click");
editItem.off("click");
// 获取 li 在 ul 中的索引
const index = li.index();
deleteItem.on("click", () => this.onDelete(index, li));
editItem.on("click", () => this.onEdit(index, li));
$(document).one("click", (event) => {
if (!this.menu.get(0).contains(event.target)) {
this.closeContextMenu();
deleteItem.off("click", () => this.onDelete);
editItem.off("click", () => this.onEdit);
}
});
});
return li;
}
/**
* 渲染配置列表
*/
renderList() {
const list = this.config_bar_ul;
list.empty(); // 清空
this.tempConfig.configurations.forEach((item) => {
list.append(this.createListItemElement(item['name']));
});
// 添加按钮
let addButton = OJB_safeCreateJQElement(`<li id='${this.prefix}add_button' class="tempConfig_add_button">
<span>+ ${i18next.t('add', { ns: 'common' })}</span>
</li>`);
this.config_add_button = addButton;
list.append(addButton);
addButton.on("click", this.onAdd);
}
/**
* 添加配置项
*/
onAdd() {
const configMenu = this.createConfigHTML();
const structure = this.structure;
configMenu.on("click", "#tempConfig_save", () => {
// 检查必填字段
const { valid, config } = Validator.required(structure);
if (!valid) return;
// 检查键值对
const { valid: checkOk, errorKey } = Validator.checkKeyValuePairs(structure, config);
if (!checkOk) return;
this.tempConfig.configurations.push(config);
this.createListItemElement(config.name).insertBefore(this.config_add_button);
configMenu.remove();
});
configMenu.on("click", ".btn-close", () => {
configMenu.remove();
});
}
/**
* 修改配置项
* @param {number} index - 配置项索引
* @param {HTMLElement} li - 配置项
* @returns {void}
*/
onEdit(index, li) {
const configMenu = this.createConfigHTML();
const structure = this.structure;
this.closeContextMenu();
// 填充表单
for (const [key, { value, type }] of Object.entries(this.structure)) {
const configValue = this.tempConfig.configurations[index][value];
const $element = $(key);
if (type === 'checkbox') {
$element.prop("checked", configValue);
} else {
$element.val(configValue);
}
}
configMenu.on("click", "#tempConfig_save", () => {
// 检查必填字段
const { valid, config } = Validator.required(structure);
if (!valid) return;
// 检查键值对
const { valid: checkOk, errorKey } = Validator.checkKeyValuePairs(structure, config);
if (!checkOk) return;
// 更新配置
this.tempConfig.configurations[index] = config;
li.find('label').text(config.name);
OJB_closeAndRemoveModal(configMenu);
});
configMenu.on("click", ".btn-close", () => {
OJB_closeAndRemoveModal(configMenu);
});
}
/**
* 删除配置项
* @param {number} index - 配置项索引
* @param {HTMLElement} li - 配置项
* @returns {void}
*/
onDelete(index, li) {
this.closeContextMenu();
this.tempConfig.configurations.splice(index, 1);
li.remove();
}
/**
* 创建配置编辑页面
* @returns {JQuery<HTMLElement>} 返回配置编辑页面
*/
createConfigHTML() {
const configMenu = OJB_safeCreateJQElement(this.configHTML);
this.settingMenuDiv.after(configMenu);
OJB_showModal(configMenu);
OJB_addDraggable(configMenu);
elementLocalize(configMenu);
return configMenu;
}
/**
* 获取配置内容
* @returns {object} - 配置内容
*/
getTempConfig() {
return this.tempConfig;
}
/**
* 注册列表项选中改变监听
*/
registerChoiceChange() {
this.config_bar_ul.on("change", "input[type='radio']", (event) => {
const value = event.target.value;
this.tempConfig.choice = value;
});
}
}
const OJBetter_setting_sidebar_HTML = `
<div class="OJBetter_setting_sidebar">
<ul>
<li><a href="#basic-settings" id="sidebar-basic-settings" class="active" data-i18n="settings:sidebar.basic"></a></li>
<li><a href="#preference-settings" id="sidebar-preference-settings" data-i18n="settings:sidebar.preference"></a></li>
<li><a href="#translation-settings" id="sidebar-translation-settings" data-i18n="settings:sidebar.translation"></a></li>
<li><a href="#clist_rating-settings" id="sidebar-clist_rating-settings" data-i18n="settings:sidebar.clist"></a></li>
<li><a href="#code_editor-settings" id="sidebar-code_editor-settings" data-i18n="settings:sidebar.monaco"></a></li>
<li><a href="#dev-settings" id="sidebar-dev-settings" data-i18n="settings:sidebar.dev"></a></li>
<li><a href="#about-settings" id="sidebar-about-settings" data-i18n="settings:sidebar.about"></a></li>
</ul>
</div>
`;
const basic_settings_HTML = `
<div id="basic-settings" class="settings-page active">
<h3 data-i18n="settings:basic.title"></h3>
<hr>
<div class='OJBetter_setting_list' style="padding: 0px 10px;">
<span id="darkMode_span" data-i18n="settings:basic.darkMode.name"></span>
<div class="dark-mode-selection">
<label>
<input class="radio-input" type="radio" name="darkMode" value="dark" />
<span class="OJBetter_setting_menu_label_text"
data-i18n="settings:basic.darkMode.options.dark"></span>
<span class="radio-icon"> </span>
</label>
<label>
<input checked="" class="radio-input" type="radio" name="darkMode" value="light" />
<span class="OJBetter_setting_menu_label_text"
data-i18n="settings:basic.darkMode.options.light"></span>
<span class="radio-icon"> </span>
</label>
<label>
<input class="radio-input" type="radio" name="darkMode" value="follow" />
<span class="OJBetter_setting_menu_label_text"
data-i18n="settings:basic.darkMode.options.system"></span>
<span class="radio-icon"> </span>
</label>
</div>
</div>
<div class='OJBetter_setting_list'>
<label for="scriptL10nLanguage" style="display: flex;" data-i18n="settings:localization.scriptLanguageLabel"></label>
<select id="scriptL10nLanguage" name="scriptL10nLanguage">
<option value="zh">简体中文</option>
<option value="zh-Hant">繁體中文</option>
<option value="en">English</option>
<option value="de">Deutsch</option>
<option value="fr">Français</option>
<option value="ko">한국어</option>
<option value="pt">Português</option>
<option value="ja">日本語</option>
<option value="es">Español</option>
<option value="it">Italiano</option>
<option value="hi">हिन्दी</option>
</select>
</div>
<div class='OJBetter_setting_list'>
<label for="localizationLanguage" style="display: flex;" data-i18n="settings:localization.websiteLanguageLabel"></label>
<select id="localizationLanguage" name="localizationLanguage">
<option value="initial">——</option>
<option value="zh">简体中文</option>
<option value="zh-Hant">繁體中文</option>
<option value="de">Deutsch</option>
<option value="fr">Français</option>
<option value="ko">한국어</option>
<option value="pt">Português</option>
<option value="ja">日本語</option>
<option value="es">Español</option>
<option value="it">Italiano</option>
<option value="hi">हिन्दी</option>
</select>
</div>
<div class='OJBetter_setting_list alert_tip'>
<div data-i18n="[html]settings:localization.notice.1"></div>
</div>
<div class='OJBetter_setting_list alert_tip'>
<div data-i18n="[html]settings:localization.notice.2"></div>
</div>
</div>
`;
const translation_settings_HTML = `
<div id="translation-settings" class="settings-page">
<h3 data-i18n="settings:translation.title"></h3>
<hr>
<h4 data-i18n="settings:translation.options.title"></h4>
<div class='OJBetter_setting_list'>
<label for="transTargetLang" style="display: flex;" data-i18n="settings:translation.preference.target.title"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.preference.target.helpText"></div>
</div>
<select id="transTargetLang" name="transTargetLang">
<option value="zh">简体中文</option>
<option value="zh-Hant">繁體中文</option>
<option value="de">Deutsch</option>
<option value="fr">Français</option>
<option value="ko">한국어</option>
<option value="pt">Português</option>
<option value="ja">日本語</option>
<option value="es">Español</option>
<option value="it">Italiano</option>
<option value="hi">हिन्दी</option>
</select>
</div>
<div id="translationServices">
<label>
<input type='radio' name='translation' value='deepl'>
<span class='OJBetter_setting_menu_label_text'
data-i18n="settings:translation.options.services.deepl"></span>
</label>
<label>
<input type='radio' name='translation' value='iflyrec'>
<span class='OJBetter_setting_menu_label_text'
data-i18n="settings:translation.options.services.iflyrec"></span>
</label>
<label>
<input type='radio' name='translation' value='youdao'>
<span class='OJBetter_setting_menu_label_text'
data-i18n="settings:translation.options.services.youdao"></span>
</label>
<label>
<input type='radio' name='translation' value='google'>
<span class='OJBetter_setting_menu_label_text'
data-i18n="settings:translation.options.services.google"></span>
</label>
<label>
<input type='radio' name='translation' value='caiyun'>
<span class='OJBetter_setting_menu_label_text'
data-i18n="settings:translation.options.services.caiyun"></span>
</label>
<label>
<input type='radio' name='translation' value='openai'>
<span class='OJBetter_setting_menu_label_text'
data-i18n="settings:translation.options.services.openai.name">
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text"
data-i18n="[html]settings:translation.options.services.openai.helpText"></div>
</div>
</span>
</label>
</div>
<hr>
<h4>DeepL</h4>
<div class='OJBetter_setting_list'>
<label for="deepl_type" style="display: flex;" data-i18n="settings:translation.deepl.mode.title"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.deepl.mode.helpText"></div>
</div>
<select id="deepl_type" name="deepl_type">
<option value="free" data-i18n="settings:translation.deepl.mode.select.free"></option>
<option value="api" data-i18n="settings:translation.deepl.mode.select.api"></option>
</select>
</div>
<div id="deepl_config" class="config"></div>
<div class='OJBetter_setting_list'>
<label for="enableEmphasisProtection" data-i18n="settings:translation.deepl.enableEmphasisProtection.title"></label>
<div class="help_tip" style="margin-right: initial;">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.deepl.enableEmphasisProtection.helpText"></div>
</div>
<div class="badge">Official API Only</div>
<input type="checkbox" id="enableEmphasisProtection" name="enableEmphasisProtection">
</div>
<div class='OJBetter_setting_list'>
<label for="enableLinkProtection" data-i18n="settings:translation.deepl.enableLinkProtection.title"></label>
<div class="help_tip" style="margin-right: initial;">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.deepl.enableLinkProtection.helpText"></div>
</div>
<div class="badge">Official API Only</div>
<input type="checkbox" id="enableLinkProtection" name="enableLinkProtection">
</div>
<hr>
<h4>ChatGPT</h4>
<div id="chatgpt_config" class="config"></div>
<div class='OJBetter_setting_list'>
<label for="openai_isStream" data-i18n="settings:translation.chatgpt.isStream.name"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.chatgpt.isStream.helpText"></div>
</div>
<input type="checkbox" id="openai_isStream" name="openai_isStream">
</div>
<div class='OJBetter_setting_list'>
<label for="openai_asSystemPrompt" data-i18n="settings:translation.chatgpt.asSystemPrompt.name"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.chatgpt.asSystemPrompt.helpText"></div>
</div>
<input type="checkbox" id="openai_asSystemPrompt" name="openai_asSystemPrompt">
</div>
<div class="OJBetter_setting_list">
<label for='openai_customPrompt'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="settings:translation.chatgpt.customPrompt.name"></span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.chatgpt.customPrompt.helpText"></div>
</div>
</div>
</label>
<textarea id="openai_customPrompt" placeholder='' require = false data-i18n="[placeholder]settings:translation.chatgpt.customPrompt.placeholder"></textarea>
</div>
<hr>
<h4 data-i18n="settings:translation.preference.title"></h4>
<div class='OJBetter_setting_list'>
<label for="comment_translation_choice" style="display: flex;"
data-i18n="settings:translation.preference.comment_translation_choice.title">
</label>
<select id="comment_translation_choice" name="comment_translation_choice">
<option value="0" data-i18n="settings:translation.preference.comment_translation_choice.services.follow"></option>
<option value="deepl" data-i18n="settings:translation.preference.comment_translation_choice.services.deepl"></option>
<option value="iflyrec" data-i18n="settings:translation.preference.comment_translation_choice.services.iflyrec"></option>
<option value="youdao" data-i18n="settings:translation.preference.comment_translation_choice.services.youdao"></option>
<option value="google" data-i18n="settings:translation.preference.comment_translation_choice.services.google"></option>
<option value="caiyun" data-i18n="settings:translation.preference.comment_translation_choice.services.caiyun"></option>
<option value="openai" data-i18n="settings:translation.preference.comment_translation_choice.services.openai"></option>
</select>
</div>
<hr>
<h4 data-i18n="settings:translation.autoTranslation.title"></h4>
<div class='OJBetter_setting_list'>
<label for="autoTranslation" data-i18n="settings:translation.autoTranslation.enable"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.autoTranslation.helpText"></div>
</div>
<input type="checkbox" id="autoTranslation" name="autoTranslation">
</div>
<div class='OJBetter_setting_list'>
<label for='shortTextLength'>
<div style="display: flex;align-items: center;"
data-i18n="settings:translation.autoTranslation.shortTextLength.name"></div>
</label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.autoTranslation.shortTextLength.helpText">
</div>
</div>
<input type='number' id='shortTextLength' class='no_default' require=true data-i18n="[placeholder]settings:translation.autoTranslation.shortTextLength.placeholder">
<span data-i18n="settings:translation.autoTranslation.shortTextLength.end"></span>
</div>
<div style="display:none;">
<div class='OJBetter_setting_list'>
<label for="allowMixTrans" data-i18n="settings:translation.autoTranslation.allowMixTrans.name"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.autoTranslation.allowMixTrans.helpText">
</div>
</div>
<input type="checkbox" id="allowMixTrans" name="allowMixTrans">
<div class='OJBetter_checkboxs'>
<input type="checkbox" id="deepl" name="mixedTranslation" value="deepl">
<label for="deepl" data-i18n="settings:translation.autoTranslation.allowMixTrans.checkboxs.deepl"></label>
<input type="checkbox" id="iflyrec" name="mixedTranslation" value="iflyrec">
<label for="iflyrec" data-i18n="settings:translation.autoTranslation.allowMixTrans.checkboxs.iflyrec"></label>
<input type="checkbox" id="youdao" name="mixedTranslation" value="youdao">
<label for="youdao" data-i18n="settings:translation.autoTranslation.allowMixTrans.checkboxs.youdao"></label>
<input type="checkbox" id="google" name="mixedTranslation" value="google">
<label for="google" data-i18n="settings:translation.autoTranslation.allowMixTrans.checkboxs.google">Google</label>
<input type="checkbox" id="caiyun" name="mixedTranslation" value="caiyun">
<label for="caiyun" data-i18n="settings:translation.autoTranslation.allowMixTrans.checkboxs.caiyun"></label>
</div>
</div>
</div>
<hr>
<h4 data-i18n="settings:translation.advanced.name"></h4>
<div class='OJBetter_setting_list'>
<label for="comment_translation_mode" style="display: flex;" data-i18n="settings:translation.advanced.mode.name"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.advanced.mode.helpText"></div>
</div>
<select id="comment_translation_mode" name="comment_translation_mode">
<option value="0" data-i18n="settings:translation.advanced.mode.options.0"></option>
<option value="1" data-i18n="settings:translation.advanced.mode.options.1"></option>
<option value="2" data-i18n="settings:translation.advanced.mode.options.2"></option>
</select>
</div>
<div class='OJBetter_setting_list'>
<label for="memoryTranslateHistory" data-i18n="settings:translation.advanced.memory.name"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.advanced.memory.helpText"></div>
</div>
<input type="checkbox" id="memoryTranslateHistory" name="memoryTranslateHistory">
</div>
<div class='OJBetter_setting_list'>
<label for="translation_retransAction" style="display: flex;" data-i18n="settings:translation.advanced.retrans.name"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.advanced.retrans.helpText"></div>
</div>
<select id="translation_retransAction" name="translation_retransAction">
<option value=0 data-i18n="settings:translation.advanced.retrans.options.0"></option>
<option value=1 data-i18n="settings:translation.advanced.retrans.options.1"></option>
</select>
</div>
<div class='OJBetter_setting_list'>
<label for='transWaitTime'>
<div style="display: flex;align-items: center;" data-i18n="settings:translation.advanced.transWaitTime.name"></div>
</label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.advanced.transWaitTime.helpText"></div>
</div>
<input type='number' id='transWaitTime' class='no_default' require=true data-i18n="[placeholder]settings:translation.advanced.transWaitTime.placeholder">
<span data-i18n="settings:translation.advanced.transWaitTime.end"></span>
</div>
<div class='OJBetter_setting_list'>
<label for="translation_replaceSymbol" style="display: flex;" data-i18n="settings:translation.advanced.replaceSymbol.name"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.advanced.replaceSymbol.helpText"></div>
</div>
<select id="translation_replaceSymbol" name="translation_replaceSymbol">
<option value=2 data-i18n="settings:translation.advanced.replaceSymbol.options.2"></option>
<option value=1 data-i18n="settings:translation.advanced.replaceSymbol.options.1"></option>
<option value=3 data-i18n="settings:translation.advanced.replaceSymbol.options.3"></option>
</select>
</div>
<div class='OJBetter_setting_list'>
<label for="filterTextWithoutEmphasis" data-i18n="settings:translation.advanced.filterTextWithoutEmphasis.name"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:translation.advanced.filterTextWithoutEmphasis.helpText"></div>
</div>
<input type="checkbox" id="filterTextWithoutEmphasis" name="filterTextWithoutEmphasis">
</div>
</div>
`;
const clist_rating_settings_HTML = `
<div id="clist_rating-settings" class="settings-page">
<h3 data-i18n="settings:clist.title"></h3>
<hr>
<h4 data-i18n="settings:clist.basics.name"></h4>
<div class='OJBetter_setting_list alert_tip'>
<div>
<p data-i18n="[html]settings:clist.basics.notice"></p>
</div>
</div>
<div class='OJBetter_setting_list'>
<label for='clist_Authorization'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="settings:clist.basics.key.title"></span>
</div>
</label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:clist.basics.key.helpText"></div>
</div>
<input type='text' id='clist_Authorization' class='no_default' required="true"
data-i18n="[placeholder]settings:clist.basics.key.keyPlaceholder">
</div>
<hr>
<h4 data-i18n="settings:clist.displayRating.title"></h4>
<div class='OJBetter_setting_list'>
<label for="showClistRating_contest"><span data-i18n="settings:clist.displayRating.contest.name"></span></label>
<input type="checkbox" id="showClistRating_contest" name="showClistRating_contest">
</div>
<div class='OJBetter_setting_list'>
<label for="showClistRating_problem"><span data-i18n="settings:clist.displayRating.problem.name"></span></label>
<input type="checkbox" id="showClistRating_problem" name="showClistRating_problem">
</div>
<div class='OJBetter_setting_list' style='display:none;'>
<label for="showClistRating_problemset"><span data-i18n="settings:clist.displayRating.problemset.name"></span></label>
<input type="checkbox" id="showClistRating_problemset" name="showClistRating_problemset">
</div>
<hr>
<div class='OJBetter_setting_list'>
<label for="RatingHidden"><span data-i18n="settings:clist.spoilerProtection.title"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:clist.spoilerProtection.helpText"></div>
</div>
<input type="checkbox" id="RatingHidden" name="RatingHidden">
</div>
</div>
`;
const code_editor_settings_HTML = `
<div id="code_editor-settings" class="settings-page">
<h3 data-i18n="settings:codeEditor.title"></h3>
<hr>
<h4 data-i18n="settings:codeEditor.basics"></h4>
<div class='OJBetter_setting_list'>
<label for="problemPageCodeEditor"><span
data-i18n="settings:codeEditor.problemPageCodeEditor.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="settings:codeEditor.problemPageCodeEditor.helpText"></div>
</div>
<input type="checkbox" id="problemPageCodeEditor" name="problemPageCodeEditor">
</div>
<div class='OJBetter_setting_list'>
<label for="beautifyPreBlocks"><span
data-i18n="settings:codeEditor.beautifyPreBlocks.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="settings:codeEditor.beautifyPreBlocks.helpText"></div>
</div>
<input type="checkbox" id="beautifyPreBlocks" name="beautifyPreBlocks">
</div>
<hr>
<h4 data-i18n="settings:codeEditor.preferences.title"></h4>
<div class='OJBetter_setting_list'>
<label for="isCodeSubmitConfirm"><span
data-i18n="settings:codeEditor.preferences.isCodeSubmitConfirm.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="settings:codeEditor.preferences.isCodeSubmitConfirm.helpText"></div>
</div>
<input type="checkbox" id="isCodeSubmitConfirm" name="isCodeSubmitConfirm">
</div>
<div class='OJBetter_setting_list'>
<label for="autoSubmitAfterPass"><span
data-i18n="settings:codeEditor.preferences.autoSubmitAfterPass.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="settings:codeEditor.preferences.autoSubmitAfterPass.helpText"></div>
</div>
<input type="checkbox" id="autoSubmitAfterPass" name="autoSubmitAfterPass">
</div>
<div class='OJBetter_setting_list'>
<label for="alwaysConsumeMouseWheel"><span
data-i18n="settings:codeEditor.preferences.alwaysConsumeMouseWheel.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="settings:codeEditor.preferences.alwaysConsumeMouseWheel.helpText"></div>
</div>
<input type="checkbox" id="alwaysConsumeMouseWheel" name="alwaysConsumeMouseWheel">
</div>
<div class='OJBetter_setting_list'>
<label for="autoMemoryCode"><span
data-i18n="settings:codeEditor.preferences.autoMemoryCode.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="settings:codeEditor.preferences.autoMemoryCode.helpText"></div>
</div>
<input type="checkbox" id="autoMemoryCode" name="autoMemoryCode">
</div>
<div class='OJBetter_setting_list'>
<label for="submitButtonPosition"><span
data-i18n="settings:codeEditor.preferences.submitButtonPosition.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="settings:codeEditor.preferences.submitButtonPosition.helpText"></div>
</div>
<select id="submitButtonPosition" name="submitButtonPosition">
<option value="bottom" data-i18n="settings:codeEditor.preferences.submitButtonPosition.options.bottom"></option>
<option value="top" data-i18n="settings:codeEditor.preferences.submitButtonPosition.options.top"></option>
</select>
</div>
<hr>
<h4 data-i18n="settings:codeEditor.onlineCodeExecution.title"></h4>
<label>
<input type='radio' name='compiler' value='official'>
<span class='OJBetter_setting_menu_label_text'
data-i18n="settings:codeEditor.onlineCodeExecution.compilerOptions.codeforces"></span>
</label>
<label>
<input type='radio' name='compiler' value='wandbox'>
<span class='OJBetter_setting_menu_label_text'
data-i18n="settings:codeEditor.onlineCodeExecution.compilerOptions.wandbox"></span>
</label>
<label>
<input type='radio' name='compiler' value='rextester'>
<span class='OJBetter_setting_menu_label_text'
data-i18n="settings:codeEditor.onlineCodeExecution.compilerOptions.rextester"></span>
</label>
<hr>
<h4 data-i18n="settings:codeEditor.lsp.title"></h4>
<div class='OJBetter_setting_list'>
<label for="useLSP"><span data-i18n="settings:codeEditor.lsp.useLSP.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:codeEditor.lsp.useLSP.helpText"></div>
</div>
<input type="checkbox" id="useLSP" name="useLSP">
</div>
<div class='OJBetter_setting_list'>
<label for='OJBetter_Bridge_WorkUri'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="settings:codeEditor.lsp.OJBetter_Bridge_WorkUri.label"></span>
</div>
</label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:codeEditor.lsp.OJBetter_Bridge_WorkUri.helpText"></div>
</div>
<input type='text' id='OJBetter_Bridge_WorkUri' class='no_default'
require=true data-i18n="[placeholder]settings:codeEditor.lsp.OJBetter_Bridge_WorkUri.placeholder">
</div>
<div class='OJBetter_setting_list'>
<label for='OJBetter_Bridge_SocketUrl'>
<div style="display: flex;align-items: center;">
<span class="input_label"
data-i18n="settings:codeEditor.lsp.OJBetter_Bridge_SocketUrl.label"></span>
</div>
</label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:codeEditor.lsp.OJBetter_Bridge_SocketUrl.helpText"></div>
</div>
<input type='text' id='OJBetter_Bridge_SocketUrl' class='no_default'
require=true data-i18n="[placeholder]settings:codeEditor.lsp.OJBetter_Bridge_SocketUrl.placeholder">
</div>
<hr>
<h4 data-i18n="settings:codeEditor.staticCompletionEnhancement.title"></h4>
<div class='OJBetter_setting_list'>
<label for="cppCodeTemplateComplete"><span
data-i18n="settings:codeEditor.staticCompletionEnhancement.cppCodeTemplateComplete.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:codeEditor.staticCompletionEnhancement.cppCodeTemplateComplete.helpText"></div>
</div>
<input type="checkbox" id="cppCodeTemplateComplete" name="cppCodeTemplateComplete">
</div>
<hr>
<h5 data-i18n="settings:codeEditor.staticCompletionEnhancement.customization"></h5>
<div class='OJBetter_setting_list alert_warn'>
<div>
<p data-i18n="settings:codeEditor.staticCompletionEnhancement.performanceWarning"></p>
</div>
</div>
<div id="Complet_config" class="config"></div>
</div>
`;
const preference_settings_HTML = `
<div id="preference-settings" class="settings-page">
<h3 data-i18n="settings:preference.title"></h3>
<hr>
<div class='OJBetter_setting_list' style="display:none;">
<label for="expandFoldingblocks" data-i18n="settings:basic.expandBlocks"></label>
<input type="checkbox" id="expandFoldingblocks" name="expandFoldingblocks">
</div>
<div class='OJBetter_setting_list' style="display:none;">
<label for="renderPerfOpt" data-i18n="settings:basic.renderOptimization.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:basic.renderOptimization.helpText"></div>
</div>
<input type="checkbox" id="renderPerfOpt" name="renderPerfOpt">
</div>
<div class='OJBetter_setting_list' style="display:none;">
<label for="selectElementPerfOpt" data-i18n="settings:basic.selectElementOptimization.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:basic.selectElementOptimization.helpText"></div>
</div>
<input type="checkbox" id="selectElementPerfOpt" name="selectElementPerfOpt">
</div>
<div class='OJBetter_setting_list' style="display:none;">
<label for="commentPaging" data-i18n="settings:basic.paging.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:basic.paging.helpText"></div>
</div>
<input type="checkbox" id="commentPaging" name="commentPaging">
</div>
<div class='OJBetter_setting_list'>
<label for="showJumpToLuogu" data-i18n="settings:basic.luoguJump.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:basic.luoguJump.helpText"></div>
</div>
<input type="checkbox" id="showJumpToLuogu" name="showJumpToLuogu">
</div>
<div class='OJBetter_setting_list'>
<label for="showCF2vjudge" data-i18n="settings:basic.vjudgeJump.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:basic.vjudgeJump.helpText"></div>
</div>
<input type="checkbox" id="showCF2vjudge" name="showCF2vjudge">
</div>
<div class='OJBetter_setting_list' style="display:none;">
<label for="standingsRecolor" data-i18n="settings:basic.recolor.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:basic.recolor.helpText"></div>
</div>
<input type="checkbox" id="standingsRecolor" name="standingsRecolor">
</div>
<div class='OJBetter_setting_list'>
<label for="showLoading" data-i18n="settings:preference.loadingInfo.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:preference.loadingInfo.helpText" data-i18n-options='{ "scriptName": "${OJBetter.state.name}" }'></div>
</div>
<input type="checkbox" id="showLoading" name="showLoading">
</div>
<div class='OJBetter_setting_list'>
<label for="hoverTargetAreaDisplay" data-i18n="settings:preference.targetArea.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:preference.targetArea.helpText"></div>
</div>
<input type="checkbox" id="hoverTargetAreaDisplay" name="hoverTargetAreaDisplay">
</div>
<div class='OJBetter_setting_list'>
<label for='iconButtonSize'>
<div style="display: flex;align-items: center;" data-i18n="settings:preference.iconButtonSize.title"></div>
</label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:preference.iconButtonSize.helpText"></div>
</div>
<input type='number' id='iconButtonSize' class='no_default' require=true data-i18n="[placeholder]settings:preference.iconButtonSize.placeholder">
<span>px</span>
</div>
<div class='OJBetter_setting_list'>
<label for="showSameContestProblems" data-i18n="settings:preference.showSameContestProblems.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:preference.showSameContestProblems.helpText"></div>
</div>
<input type="checkbox" id="showSameContestProblems" name="showSameContestProblems">
</div>
<div class='OJBetter_setting_list'>
<label for='TranslateTextColor'>
<div style="display: flex;align-items: center;" data-i18n="settings:preference.TranslateTextColor.title"></div>
</label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:preference.TranslateTextColor.helpText"></div>
</div>
<textarea type="text" id='TranslateTextColor' class='no_default' data-i18n="[placeholder]settings:preference.TranslateTextColor.placeholder"></textarea>
</div>
</div>
`;
const dev_settings_HTML = `
<div id="dev-settings" class="settings-page">
<h3 data-i18n="settings:dev.title"></h3>
<hr>
<div class='OJBetter_setting_list alert_danger'>
<div>
<p data-i18n="[html]settings:dev.notice"></p>
</div>
</div>
<hr>
<h5 data-i18n="settings:dev.load.title"></h5>
<div class='OJBetter_setting_list'>
<label for="notWaiteLoaded"><span data-i18n="settings:dev.load.notWaiteLoaded.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:dev.load.notWaiteLoaded.helpText"></div>
</div>
<input type="checkbox" id="notWaiteLoaded" name="notWaiteLoaded">
</div>
<hr>
<h5 data-i18n="settings:dev.l10n.title"></h5>
<div class='OJBetter_setting_list'>
<label><span data-i18n="settings:dev.l10n.refreshScrpitCache.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:dev.l10n.refreshScrpitCache.helpText"></div>
</div>
<button type="button" id="l10n_refreshScrpitCacheButton" name="l10n_refreshScrpitCacheButton" data-i18n="settings:dev.l10n.refreshScrpitCache.button"></button>
</div>
<hr>
<h5 data-i18n="settings:dev.l10n_web.title"></h5>
<div class='OJBetter_setting_list'>
<label><span data-i18n="settings:dev.l10n_web.refreshScrpitCache.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:dev.l10n_web.refreshScrpitCache.helpText"></div>
</div>
<button type="button" id="l10n_web_refreshScrpitCacheButton" name="l10n_web_refreshScrpitCacheButton" data-i18n="settings:dev.l10n_web.refreshScrpitCache.button"></button>
</div>
<div class='OJBetter_setting_list'>
<label for="isRuleMarkingEnabled"><span data-i18n="settings:dev.l10n_web.isRuleMarkingEnabled.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:dev.l10n_web.isRuleMarkingEnabled.helpText"></div>
</div>
<input type="checkbox" id="isRuleMarkingEnabled" name="isRuleMarkingEnabled">
</div>
<hr>
<h5 data-i18n="settings:dev.indexedDB.title"></h5>
<div class='OJBetter_setting_list'>
<label><span data-i18n="settings:dev.indexedDB.clear.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:dev.indexedDB.clear.helpText"></div>
</div>
<button type="button" id="indexedDB_clearButton" name="indexedDB_clearButton" data-i18n="settings:dev.indexedDB.clear.button"></button>
</div>
<div class='OJBetter_setting_list'>
<label><span data-i18n="settings:dev.indexedDB.inputOrExport.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:dev.indexedDB.inputOrExport.helpText"></div>
</div>
<button type="button" id="indexedDB_exportButton" name="indexedDB_exportButton" data-i18n="settings:dev.indexedDB.inputOrExport.export"></button>
<button type="button" id="indexedDB_importButton" name="indexedDB_importButton" data-i18n="settings:dev.indexedDB.inputOrExport.import"></button>
</div>
<hr>
<h5 data-i18n="settings:dev.configuration.title"></h5>
<div class='OJBetter_setting_list'>
<label><span data-i18n="settings:dev.configuration.clear.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:dev.configuration.clear.helpText"></div>
</div>
<button type="button" id="configuration_clearButton" name="configuration_clearButton" data-i18n="settings:dev.configuration.clear.button"></button>
</div>
<div class='OJBetter_setting_list'>
<label><span data-i18n="settings:dev.configuration.inputOrExport.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:dev.configuration.inputOrExport.helpText"></div>
</div>
<button type="button" id="configuration_exportButton" name="configuration_exportButton" data-i18n="settings:dev.configuration.inputOrExport.export"></button>
<button type="button" id="configuration_importButton" name="configuration_importButton" data-i18n="settings:dev.configuration.inputOrExport.import"></button>
</div>
</div>
`;
const about_settings_HTML = `
<div id="about-settings" class="settings-page">
<h3 data-i18n="settings:about.title"></h3>
<hr>
<div class='versionInfo'>
<p>${OJBetter.state.name}</p>
<p><span data-i18n="settings:about.version"></span><span id="nowVersion">${OJBetter.state.version}</span></p>
<p> @北极小狐 <a target="_blank" href="https://github.com/beijixiaohu/OJBetter">Github</a>
<a target="_blank" href="https://greasyfork.org.cn/zh-CN/scripts/465777">GreasyFork</a></p>
</div>
<hr>
<h5 data-i18n="settings:about.update.title"></h5>
<div id="thanksforDevChannelNotice" class='OJBetter_setting_list alert_info'>
<div data-i18n="[html]settings:about.update.thanksforDevChannelNotice"} data-i18n-options='{ "scriptName": "${OJBetter.state.name}" }' ></div>
</div>
<div class='OJBetter_setting_list'>
<label for="updateChannel"><span data-i18n="settings:about.update.channel.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:about.update.channel.helpText"></div>
</div>
<select id="updateChannel" name="updateChannel">
<option value="release" data-i18n="settings:about.update.channel.options.release"></option>
<option value="dev" data-i18n="settings:about.update.channel.options.dev"></option>
</select>
</div>
<div class='OJBetter_setting_list'>
<label for="updateSource"><span data-i18n="settings:about.update.source.label"></span></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]settings:about.update.source.helpText"></div>
</div>
<select id="updateSource" name="updateSource">
<option value="aliyunoss" data-i18n="settings:about.update.source.options.aliyunoss"></option>
<option value="greasyfork" data-i18n="settings:about.update.source.options.greasyfork"></option>
<option value="github" data-i18n="settings:about.update.source.options.github"></option>
</select>
</div>
</div>
`;
const OJBetter_setting_content_HTML = `
<div class="OJBetter_setting_content">
${basic_settings_HTML}
${translation_settings_HTML}
${clist_rating_settings_HTML}
${code_editor_settings_HTML}
${preference_settings_HTML}
${dev_settings_HTML}
${about_settings_HTML}
</div>
`;
// 设置界面HTML
const OJBetterSettingMenu_HTML = `
<dialog class='OJBetter_setting_menu' id='OJBetter_setting_menu'>
<div class="tool-box">
<button class='ojb_btn ojb_btn_popover top btn-close' type="button">
<i class="iconfont"></i>
</button>
</div>
<div class="OJBetter_setting_container">
${OJBetter_setting_sidebar_HTML}
${OJBetter_setting_content_HTML}
</div>
</dialog>
`;
const apiCustomConfigHTML = (prefix) => {
return `
<div class="OJBetter_setting_list">
<label for='${prefix}_header'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="config:common.advanced.header.label"></span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:common.advanced.header.tipText"></div>
</div>
</div>
</label>
<textarea id="${prefix}_header" placeholder='' require = false data-i18n="[placeholder]config:common.advanced.header.placeholder"></textarea>
</div>
<div class="OJBetter_setting_list">
<label for='${prefix}_data'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="config:common.advanced.data.label"></span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:common.advanced.data.tipText"></div>
</div>
</div>
</label>
<textarea id="${prefix}_data" placeholder='' require = false data-i18n="[placeholder]config:common.advanced.data.placeholder"></textarea>
</div>
`;
};
const apiQuotaConfigHTML = (prefix) => {
return `
<div class="OJBetter_setting_list">
<label for='${prefix}_quota_url'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="config:common.quota.url.label"></span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:common.quota.url.tipText"></div>
</div>
</div>
</label>
<input type='text' id='${prefix}_quota_url' class='no_default' placeholder='' require = true data-i18n="[placeholder]config:common.quota.url.placeholder">
</div>
<div class="OJBetter_setting_list">
<label for="${prefix}_quota_method" style="display: flex;" data-i18n="config:common.quota.method.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:common.quota.method.tipText"></div>
</div>
<select id="${prefix}_quota_method" name="${prefix}_quota_method">
<option value="get">GET</option>
<option value="post">POST</option>
</select>
</div>
<div class="OJBetter_setting_list">
<label for='${prefix}_quota_header'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="config:common.quota.header.label"></span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:common.quota.header.tipText"></div>
</div>
</div>
</label>
<textarea id="${prefix}_quota_header" placeholder='' require = false data-i18n="[placeholder]config:common.quota.header.placeholder"></textarea>
</div>
<div class="OJBetter_setting_list">
<label for='${prefix}_quota_data'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="config:common.quota.data.label"></span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:common.quota.data.tipText"></div>
</div>
</div>
</label>
<textarea id="${prefix}_quota_data" placeholder='' require = false data-i18n="[placeholder]config:common.quota.data.placeholder"></textarea>
</div>
<div class="OJBetter_setting_list">
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="config:common.quota.surplus.label"></span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:common.quota.surplus.tipText"></div>
</div>
</div>
</label>
<input type='text' id='${prefix}_quota_surplus' class='no_default' placeholder='' require = true data-i18n="[placeholder]config:common.quota.surplus.placeholder">
</div>
`;
}
const deeplConfigEditHTML = `
<dialog class='OJBetter_setting_menu' id='config_edit_menu'>
<div class='OJBetter_setting_content'>
<div class="tool-box">
<button class='ojb_btn ojb_btn_popover top btn-close' type="button">
<i class="iconfont"></i>
</button>
</div>
<h4 data-i18n="config:deepl.title"></h4>
<h5 data-i18n="config:deepl.basic.title"></h5>
<hr>
<div class="OJBetter_setting_list">
<label for='name'>
<span class="input_label" data-i18n="config:deepl.basic.name.label"></span>
</label>
<input type='text' id='name' class='no_default' placeholder='' require = true data-i18n="[placeholder]config:deepl.basic.name.placeholder">
</div>
<div class='OJBetter_setting_list'>
<label for="deepl_apiGenre" style="display: flex;" data-i18n="config:deepl.genre.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:deepl.genre.tipText"></div>
</div>
<select id="deepl_apiGenre" name="deepl_apiGenre">
<option value="api-free">api-free</option>
<option value="api-pro">api-pro</option>
<option value="deeplx">deeplx</option>
</select>
</div>
<div class="OJBetter_setting_list">
<label for='deepl_key'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="config:deepl.basic.key.label"></span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:deepl.basic.key.tipText"></div>
</div>
</div>
</label>
<input type='text' id='deepl_key' class='no_default' placeholder='' require = true data-i18n="[placeholder]config:deepl.basic.key.placeholder">
</div>
<div class="OJBetter_setting_list">
<label for='deepl_proxy'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="config:deepl.basic.proxy.label">Proxy API:</span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:deepl.basic.proxy.tipText"></div>
</div>
</div>
</label>
<input type='text' id='deepl_proxy' placeholder='' require = false>
</div>
<hr>
<details>
<summary data-i18n="config:common.advanced.title"></summary>
${apiCustomConfigHTML('deepl')}
</details>
<details>
<summary data-i18n="config:common.quota.title"></summary>
${apiQuotaConfigHTML('deepl')}
</details>
<button id='tempConfig_save' data-i18n="common:save"></button>
</div>
</dialog>
`;
const chatgptConfigEditHTML = `
<dialog class='OJBetter_setting_menu' id='config_edit_menu'>
<div class='OJBetter_setting_content'>
<div class="tool-box">
<button class='ojb_btn ojb_btn_popover top btn-close' type="button">
<i class="iconfont"></i>
</button>
</div>
<h4 data-i18n="config:chatgpt.title"></h4>
<h5 data-i18n="config:chatgpt.basic.title"></h5>
<hr>
<div class="OJBetter_setting_list">
<label for='name'>
<span class="input_label" data-i18n="config:chatgpt.basic.name.label"></span>
</label>
<input type='text' id='name' class='no_default' placeholder='' require = true data-i18n="[placeholder]config:chatgpt.basic.name.placeholder">
</div>
<div class="OJBetter_setting_list">
<label for='chatgpt_model'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="[html]config:chatgpt.basic.model.label"></span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:chatgpt.basic.model.tipText"></div>
</div>
</div>
</label>
<input type='text' id='chatgpt_model' placeholder='gpt-3.5-turbo' require = false>
</div>
<div class="OJBetter_setting_list">
<label for='chatgpt_key'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="config:chatgpt.basic.key.label"></span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:chatgpt.basic.key.tipText"></div>
</div>
</div>
</label>
<input type='text' id='chatgpt_key' class='no_default' placeholder='' require = true data-i18n="[placeholder]config:chatgpt.basic.key.placeholder">
</div>
<div class="OJBetter_setting_list">
<label for='chatgpt_proxy'>
<div style="display: flex;align-items: center;">
<span class="input_label" data-i18n="config:chatgpt.basic.proxy.label">Proxy API:</span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:chatgpt.basic.proxy.tipText"></div>
</div>
</div>
</label>
<input type='text' id='chatgpt_proxy' placeholder='https://api.openai.com/v1/chat/completions' require = false>
</div>
<hr>
<details>
<summary data-i18n="config:common.advanced.title"></summary>
${apiCustomConfigHTML('chatgpt')}
</details>
<details>
<summary data-i18n="config:common.quota.title"></summary>
${apiQuotaConfigHTML('chatgpt')}
</details>
<button id='tempConfig_save' data-i18n="common:save"></button>
</div>
</dialog>
`;
const CompletConfigEditHTML = `
<dialog class='OJBetter_setting_menu' id='config_edit_menu'>
<div class='OJBetter_setting_content'>
<div class="tool-box">
<button class='ojb_btn ojb_btn_popover top btn-close' type="button">
<i class="iconfont"></i>
</button>
</div>
<h4 data-i18n="config:complet.title"></h4>
<hr>
<div class="OJBetter_setting_list">
<label for='name'>
<span class="input_label" data-i18n="config:complet.name.label"></span>
</label>
<input type='text' id='name' class='no_default' placeholder='' require = true data-i18n="[placeholder]config:complet.name.placeholder">
</div>
<div class='OJBetter_setting_list'>
<label for="complet_isChoose"><span id="loaded_span" data-i18n="config:complet.choose.label"></span></label>
<input type="checkbox" id="complet_isChoose" name="complet_isChoose" require = false>
</div>
<div class='OJBetter_setting_list'>
<label for="complet_genre" style="display: flex;" data-i18n="config:complet.genre.label"></label>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:complet.genre.tipText"></div>
</div>
<select id="complet_genre" name="complet_genre">
<option value="monaco">monaco</option>
<option value="ace">ace</option>
</select>
</div>
<div class='OJBetter_setting_list'>
<label for="complet_language" style="display: flex;" data-i18n="config:complet.language.label"></label>
<select id="complet_language" name="complet_language">
<option value="cpp">cpp</option>
<option value="python">python</option>
<option value="java">java</option>
<option value="c">c</option>
</select>
</div>
<div class="OJBetter_setting_list">
<label for='complet_jsonUrl'>
<div style="display: flex;align-items: center;">
<span class="input_label">JSON URL:</span>
<div class="help_tip">
${helpCircleHTML}
<div class="tip_text" data-i18n="[html]config:complet.jsonurl.tipText"></div>
</div>
</div>
</label>
<div class='OJBetter_setting_list alert_warn' data-i18n="[html]config:complet.jsonurl.alert"></div>
<input type='text' id='complet_jsonUrl' class='no_default' placeholder='' require = true data-i18n="[placeholder]config:complet.jsonurl.placeholder">
</div>
<button id='tempConfig_save' data-i18n="common:save"></button>
</div>
</dialog>
`;
/**
* 加载设置按钮面板
*/
async function initSettingsPanel() {
/**
* 添加右上角设置按钮
* @param {string} location 位置选择器
* @param {string} method 插入方法
*/
function insertOJBetterSettingButton(location, method) {
$(location)[method](`<button class='ojb_btn OJBetter_setting'>
${OJBetter.state.name} ${i18next.t('settings', { ns: 'common' })}</button>`);
}
/**
* ============================================
* 该网站插入设置按钮的位置和方式
*/
if (OJBetter.typeOfPage.isEnglishLanguage) {
insertOJBetterSettingButton("#navbar-collapse > ul:nth-child(2) > li:last-child", "after");
} else {
if ($('.header-mypage').length > 0) insertOJBetterSettingButton(".header-mypage", "after");
else insertOJBetterSettingButton("#navbar-collapse > ul:nth-child(2) > li:last-child", "after");
}
/**
* ============================================
*/
const $settingBtns = $(".OJBetter_setting");
$settingBtns.click(() => {
$settingBtns.prop("disabled", true).addClass("open");
// 设置面板div
const settingMenu = OJB_safeCreateJQElement(OJBetterSettingMenu_HTML);
$("body").append(settingMenu);
elementLocalize(settingMenu); // 加载i18n
OJB_showModal(settingMenu);
OJB_addDraggable($('#OJBetter_setting_menu')); // 窗口支持拖拽
// help帮助悬浮窗位置更新
$(document).on('mouseenter', '.help-icon', function (event) {
var menuOffset = $('.OJBetter_setting_menu:last').offset();
var mouseX = event.pageX - menuOffset.left;
var mouseY = event.pageY - menuOffset.top;
$('.tip_text').css({
'top': mouseY + 'px',
'left': mouseX + 'px'
});
});
// 选项卡切换
$('.OJBetter_setting_sidebar a').click(function (event) {
event.preventDefault();
$('.OJBetter_setting_sidebar a').removeClass('active');
$('.settings-page').removeClass('active');
$(this).addClass('active');
const targetPageId = $(this).attr('href').substring(1);
$('#' + targetPageId).addClass('active');
});
/**
* 更新单选按钮组的可用状态
* @param {string} selector 单选按钮组的选择器
* @param {string} targetLanguage 目标语言
* @param {Object} translationSupport 翻译支持的语言对应表
*/
const updateRadioButtonsAvailability = (selector, targetLanguage) => {
Object.entries(OJBetter.supportList.translationSupport).forEach(([service, languages]) => {
const radioButton = $(selector).find(`input[value="${service}"]`);
const isEnabled = languages[targetLanguage];
$(radioButton).prop('disabled', !isEnabled);
if (!isEnabled) {
$(radioButton).prop('checked', false);
}
});
};
/**
* 检查下拉框选中项是否有效,若无效则清空
* @param {string} selector 下拉框的选择器
*/
const validateSelectOption = (selector) => {
const selectedValue = $(selector).val();
if (!selectedValue) {
$(selector).val('');
}
};
/**
* 更新翻译目标语言下拉框的可用状态
* @param {string} selector 下拉框的选择器
* @param {string} targetLanguage 目标语言
* @param {Object} translationSupport 翻译支持的语言对应表
*/
const updateSelectOptionsAvailability = (selector, targetLanguage) => {
$(selector).children('option').each(function () {
const optionValue = $(this).val();
const isEnabled = OJBetter.supportList.translationSupport[optionValue] ? OJBetter.supportList.translationSupport[optionValue][targetLanguage] : true;
$(this).prop('disabled', !isEnabled);
});
validateSelectOption(selector);
};
/**
* 更新翻译服务复选框的可用状态
* @param {string} selector 复选框的选择器
* @param {string} targetLanguage 目标语言
* @param {Object} translationSupport 翻译支持的语言对应表
*/
const updateCheckboxesAvailability = (selector, targetLanguage) => {
$(selector).children('input').each(function () {
const checkboxValue = $(this).val();
const isEnabled = OJBetter.supportList.translationSupport[checkboxValue][targetLanguage];
$(this).prop('disabled', !isEnabled);
if (!isEnabled) {
$(this).prop('checked', false);
}
});
};
/**
* 更新更新源下拉框的可用状态
* @param {string} selector 下拉框的选择器
* @param {string} targetLanguage 目标语言
* @param {Object} translationSupport 翻译支持的语言对应表
*/
const updateUpdateSourceSelectOptionsAvailability = (selector, updateChannel) => {
$(selector).children('option').each(function () {
const optionValue = $(this).val();
const isEnabled = OJBetter.supportList.updateSourceSupportList[optionValue][updateChannel];
$(this).prop('disabled', !isEnabled);
});
validateSelectOption(selector);
};
/**
* 创建配置结构
* @param {string} type - 该字段的在表单中的类型
* @param {string} value - 在配置中的键值
* @param {boolean} require - 是否是表单的必填项
* @param {string} [check=""] check - 调用的合法性检查
*/
function createStructure(type, value, require, check = "") {
return { type, value, require, check };
}
// deepl配置
const deeplStructure = {
'#name': createStructure('text', 'name', true),
'#deepl_apiGenre': createStructure('text', 'apiGenre', true),
'#deepl_key': createStructure('text', 'key', false),
'#deepl_proxy': createStructure('text', 'proxy', false),
'#deepl_header': createStructure('text', '_header', false, 'keyValuePairs'),
'#deepl_data': createStructure('text', '_data', false, 'keyValuePairs'),
'#deepl_quota_url': createStructure('text', 'quota_url', false),
'#deepl_quota_method': createStructure('text', 'quota_method', false),
'#deepl_quota_header': createStructure('text', 'quota_header', false, 'keyValuePairs'),
'#deepl_quota_data': createStructure('text', 'quota_data', false, 'keyValuePairs'),
'#deepl_quota_surplus': createStructure('text', 'quota_surplus', false, 'dotSeparatedPath'),
};
let tempConfig_deepl = GM_getValue('deepl_config'); // 获取配置信息
const configManager_deepl = new ConfigManager('#deepl_config', 'deepl_config_', tempConfig_deepl, deeplStructure, deeplConfigEditHTML);
configManager_deepl.registerChoiceChange();
// chatgpt配置
const chatgptStructure = {
'#name': createStructure('text', 'name', true),
'#chatgpt_model': createStructure('text', 'model', false),
'#chatgpt_key': createStructure('text', 'key', true),
'#chatgpt_proxy': createStructure('text', 'proxy', false),
'#chatgpt_header': createStructure('text', '_header', false, 'keyValuePairs'),
'#chatgpt_data': createStructure('text', '_data', false, 'keyValuePairs'),
'#chatgpt_quota_url': createStructure('text', 'quota_url', false),
'#chatgpt_quota_header': createStructure('text', 'quota_header', false, 'keyValuePairs'),
'#chatgpt_quota_data': createStructure('text', 'quota_data', false, 'keyValuePairs'),
'#chatgpt_quota_surplus': createStructure('text', 'quota_surplus', false, 'dotSeparatedPath'),
'#chatgpt_quota_method': createStructure('text', 'quota_method', false),
};
let tempConfig_chatgpt = GM_getValue('chatgpt_config'); // 获取配置信息
const configManager_chatgpt = new ConfigManager('#chatgpt_config', 'chatgpt_config_', tempConfig_chatgpt, chatgptStructure, chatgptConfigEditHTML);
configManager_chatgpt.registerChoiceChange();
// Complet配置
const CompletStructure = {
'#name': createStructure('text', 'name', true),
'#complet_isChoose': createStructure('checkbox', 'isChoose', true),
'#complet_genre': createStructure('text', 'genre', true),
'#complet_language': createStructure('text', 'language', true),
'#complet_jsonUrl': createStructure('text', 'jsonUrl', true)
};
let tempConfig_Complet = GM_getValue('Complet_config'); // 获取配置信息
const configManager_complet = new ConfigManager('#Complet_config', 'complet_config_', tempConfig_Complet, CompletStructure, CompletConfigEditHTML, false);
// 状态更新
$("input[name='darkMode'][value='" + OJBetter.basic.darkMode + "']").prop("checked", true);
$("#showLoading").prop("checked", GM_getValue("showLoading") === true);
$("#expandFoldingblocks").prop("checked", GM_getValue("expandFoldingblocks") === true);
$("#renderPerfOpt").prop("checked", GM_getValue("renderPerfOpt") === true);
$("#selectElementPerfOpt").prop("checked", GM_getValue("selectElementPerfOpt") === true);
$("#commentPaging").prop("checked", GM_getValue("commentPaging") === true);
$("#standingsRecolor").prop("checked", GM_getValue("standingsRecolor") === true);
$("#showJumpToLuogu").prop("checked", GM_getValue("showJumpToLuogu") === true);
$("#showCF2vjudge").prop("checked", GM_getValue("showCF2vjudge") === true);
$("#hoverTargetAreaDisplay").prop("checked", GM_getValue("hoverTargetAreaDisplay") === true);
$("#showSameContestProblems").prop("checked", GM_getValue("showSameContestProblems") === true);
$("#showClistRating_contest").prop("checked", GM_getValue("showClistRating_contest") === true);
$("#showClistRating_problemset").prop("checked", GM_getValue("showClistRating_problemset") === true);
$("#showClistRating_problem").prop("checked", GM_getValue("showClistRating_problem") === true);
$("#TranslateTextColor").val(GM_getValue("TranslateTextColor"));
$("#RatingHidden").prop("checked", GM_getValue("RatingHidden") === true);
$('#scriptL10nLanguage').val(GM_getValue("scriptL10nLanguage"));
$('#localizationLanguage').val(GM_getValue("localizationLanguage"));
$("input[name='translation'][value='" + OJBetter.translation.choice + "']").prop("checked", true);
$("input[name='translation']").css("color", "gray");
$('#deepl_type').val(GM_getValue("deepl_type"));
$("#deepl_config_config_bar_ul").find(`input[name='deepl_config_config_item'][value='${tempConfig_deepl.choice}']`).prop("checked", true);
$('#enableEmphasisProtection').prop("checked", GM_getValue("enableEmphasisProtection") === true);
$('#enableLinkProtection').prop("checked", GM_getValue("enableLinkProtection") === true);
$("#chatgpt_config_config_bar_ul").find(`input[name='chatgpt_config_config_item'][value='${tempConfig_chatgpt.choice}']`).prop("checked", true);
$("#openai_isStream").prop("checked", GM_getValue("openai_isStream") === true);
$("#openai_asSystemPrompt").prop("checked", GM_getValue("openai_asSystemPrompt") === true);
$('#openai_customPrompt').val(GM_getValue("openai_customPrompt"));
$('#comment_translation_choice').val(GM_getValue("commentTranslationChoice"));
$('#iconButtonSize').val(GM_getValue("iconButtonSize"));
$("#autoTranslation").prop("checked", GM_getValue("autoTranslation") === true);
$('#shortTextLength').val(GM_getValue("shortTextLength"));
$("#allowMixTrans").prop("checked", GM_getValue("allowMixTrans") === true);
$('.OJBetter_checkboxs').find('input[type="checkbox"][name="mixedTranslation"]').each(function () {
if (OJBetter.translation.auto.mixTrans.servers.indexOf($(this).val()) > -1) {
$(this).prop('checked', true);
}
});
// 翻译目标语言下拉框
$('#transTargetLang').change(function () {
var selectedLang = $(this).val();
updateRadioButtonsAvailability('#translationServices', selectedLang);
updateSelectOptionsAvailability('#comment_translation_choice', selectedLang);
updateCheckboxesAvailability('.OJBetter_checkboxs', selectedLang);
});
$('#transTargetLang').val(GM_getValue("transTargetLang"));
$('#transTargetLang').change();
//
$('#comment_translation_mode').val(GM_getValue("commentTranslationMode"));
$("#memoryTranslateHistory").prop("checked", GM_getValue("memoryTranslateHistory") === true);
$('#transWaitTime').val(GM_getValue("transWaitTime"));
$('#translation_replaceSymbol').val(GM_getValue("replaceSymbol"));
$("#filterTextWithoutEmphasis").prop("checked", GM_getValue("filterTextWithoutEmphasis") === true);
$('#translation_retransAction').val(GM_getValue("retransAction"));
$("#clist_Authorization").val(GM_getValue("clist_Authorization"));
$("#problemPageCodeEditor").prop("checked", GM_getValue("problemPageCodeEditor") === true);
$("#beautifyPreBlocks").prop("checked", GM_getValue("beautifyPreBlocks") === true);
$("#isCodeSubmitConfirm").prop("checked", GM_getValue("isCodeSubmitConfirm") === true);
$("#autoSubmitAfterPass").prop("checked", GM_getValue("autoSubmitAfterPass") === true);
$("#alwaysConsumeMouseWheel").prop("checked", GM_getValue("alwaysConsumeMouseWheel") === true);
$("#autoMemoryCode").prop("checked", GM_getValue("autoMemoryCode") === true);
$("#submitButtonPosition").val(GM_getValue("submitButtonPosition"));
$("#cppCodeTemplateComplete").prop("checked", GM_getValue("cppCodeTemplateComplete") === true);
$("#useLSP").prop("checked", GM_getValue("useLSP") === true);
$("#OJBetter_Bridge_WorkUri").val(GM_getValue("OJBetter_Bridge_WorkUri"));
$("#OJBetter_Bridge_SocketUrl").val(GM_getValue("OJBetter_Bridge_SocketUrl"));
$("input[name='compiler'][value='" + OJBetter.monaco.onlineCompilerChoice + "']").prop("checked", true);
$("input[name='compiler']").css("color", "gray");
// 调试
$("#notWaiteLoaded").prop("checked", GM_getValue("notWaiteLoaded") === true);
$('#l10n_refreshScrpitCacheButton').click(clearI18nextCache);
$('#l10n_web_refreshScrpitCacheButton').click(clearWebsiteL10nData);
$("#isRuleMarkingEnabled").prop("checked", GM_getValue("isRuleMarkingEnabled") === true);
$('#indexedDB_clearButton').click(async () => { await clearDatabase(); });
$('#indexedDB_exportButton').click(async () => { downloadDataAsFile(await exportDatabase(), 'database_export.json') });
$('#indexedDB_importButton').click(() => { readFileInput(async (fileContent) => { await importDatabase(fileContent); }); });
$('#configuration_clearButton').click(deleteAllConfigSettings);
$('#configuration_exportButton').click(() => { downloadDataAsFile(exportSettingsToJSON(), 'configuration_export.json') });
$('#configuration_importButton').click(() => { readFileInput((fileContent) => { importSettingsFromJSON(fileContent); }) });
// 关于
$('#updateChannel').val(GM_getValue("updateChannel"));
$('#updateSource').val(GM_getValue("updateSource"));
$('#updateChannel').change(function () {
var selectedLang = $(this).val();
updateUpdateSourceSelectOptionsAvailability('#updateSource', selectedLang);
if (selectedLang == "dev") $('#thanksforDevChannelNotice').show();
else $('#thanksforDevChannelNotice').hide();
});
$('#updateChannel').change();
// 关闭
settingMenu.on("click", ".btn-close", async () => {
// 设置的数据
const settings = {
darkMode: $("input[name='darkMode']:checked").val(),
showLoading: $("#showLoading").prop("checked"),
hoverTargetAreaDisplay: $("#hoverTargetAreaDisplay").prop("checked"),
showSameContestProblems: $("#showSameContestProblems").prop("checked"),
expandFoldingblocks: $("#expandFoldingblocks").prop("checked"),
renderPerfOpt: $("#renderPerfOpt").prop("checked"),
selectElementPerfOpt: $("#selectElementPerfOpt").prop("checked"),
commentPaging: $("#commentPaging").prop("checked"),
standingsRecolor: $("#standingsRecolor").prop("checked"),
showJumpToLuogu: $("#showJumpToLuogu").prop("checked"),
showCF2vjudge: $("#showCF2vjudge").prop("checked"),
scriptL10nLanguage: $('#scriptL10nLanguage').val(),
localizationLanguage: $('#localizationLanguage').val(),
transTargetLang: $('#transTargetLang').val(),
translation: $("input[name='translation']:checked").val(),
deepl_type: $('#deepl_type').val(),
enableEmphasisProtection: $("#enableEmphasisProtection").prop("checked"),
enableLinkProtection: $("#enableLinkProtection").prop("checked"),
openai_isStream: $("#openai_isStream").prop("checked"),
openai_asSystemPrompt: $("#openai_asSystemPrompt").prop("checked"),
openai_customPrompt: $('#openai_customPrompt').val(),
commentTranslationChoice: $('#comment_translation_choice').val(),
iconButtonSize: $('#iconButtonSize').val(),
TranslateTextColor: $("#TranslateTextColor").val(),
autoTranslation: $("#autoTranslation").prop("checked"),
shortTextLength: $('#shortTextLength').val(),
allowMixTrans: $("#allowMixTrans").prop("checked"),
mixedTranslation: (() => {
let mixedTranslation = [];
$('.OJBetter_checkboxs').find('input[type="checkbox"][name="mixedTranslation"]').each(function () {
if ($(this).is(":checked")) {
mixedTranslation.push($(this).val());
}
});
return mixedTranslation;
})(),
commentTranslationMode: $('#comment_translation_mode').val(),
memoryTranslateHistory: $('#memoryTranslateHistory').prop("checked"),
transWaitTime: $('#transWaitTime').val(),
replaceSymbol: $('#translation_replaceSymbol').val(),
filterTextWithoutEmphasis: $('#filterTextWithoutEmphasis').prop("checked"),
retransAction: $('#translation_retransAction').val(),
showClistRating_contest: $('#showClistRating_contest').prop("checked"),
showClistRating_problemset: $('#showClistRating_problemset').prop("checked"),
showClistRating_problem: $('#showClistRating_problem').prop("checked"),
RatingHidden: $('#RatingHidden').prop("checked"),
clist_Authorization: $('#clist_Authorization').val(),
problemPageCodeEditor: $("#problemPageCodeEditor").prop("checked"),
beautifyPreBlocks: $("#beautifyPreBlocks").prop("checked"),
isCodeSubmitConfirm: $("#isCodeSubmitConfirm").prop("checked"),
autoSubmitAfterPass: $("#autoSubmitAfterPass").prop("checked"),
alwaysConsumeMouseWheel: $("#alwaysConsumeMouseWheel").prop("checked"),
autoMemoryCode: $("#autoMemoryCode").prop("checked"),
submitButtonPosition: $('#submitButtonPosition').val(),
cppCodeTemplateComplete: $("#cppCodeTemplateComplete").prop("checked"),
useLSP: $("#useLSP").prop("checked"),
OJBetter_Bridge_WorkUri: $('#OJBetter_Bridge_WorkUri').val().replace(/\\/g, '/').replace(/\/$/, ''),
OJBetter_Bridge_SocketUrl: $('#OJBetter_Bridge_SocketUrl').val(),
onlineCompilerChoice: $("input[name='compiler']:checked").val(),
notWaiteLoaded: $("#notWaiteLoaded").prop("checked"),
isRuleMarkingEnabled: $("#isRuleMarkingEnabled").prop("checked"),
updateChannel: $('#updateChannel').val(),
updateSource: $('#updateSource').val()
};
// tempConfigs的数据
const tempConfigs = {
'deepl_config': configManager_deepl.getTempConfig(),
'chatgpt_config': configManager_chatgpt.getTempConfig(),
'Complet_config': configManager_complet.getTempConfig()
}
// 判断是否改变
let changes = {};
const combinedConfigs = Object.assign({}, settings, tempConfigs); // 合并settings和tempConfigs对象
for (const [key, value] of Object.entries(combinedConfigs)) {
const storedValue = GM_getValue(key);
if (!OJB_deepEquals(value, storedValue)) {
changes[key] = { oldValue: storedValue, newValue: value };
}
}
// 如果changes对象不为空,则有变化
if (Object.keys(changes).length > 0) {
console.log("Changes detected:", changes);
const shouldSave = await OJB_createDialog(
i18next.t('saveSetting.title', { ns: 'dialog' }),
i18next.t('saveSetting.content', { ns: 'dialog' }),
[
i18next.t('saveSetting.buttons.0', { ns: 'dialog' }),
i18next.t('saveSetting.buttons.1', { ns: 'dialog' })
]
); // 配置改变保存确认
if (shouldSave) {
// 数据校验
// TODO
if (settings.deepl_type !== 'free') {
let selectedIndex = $('input[name="deepl_config_config_item"]:checked').length > 0;
if (!selectedIndex) {
$('.deepl_config a').removeClass('active');
$('.settings-page').removeClass('active');
$('#sidebar-translation-settings').addClass('active');
$('#translation-settings').addClass('active');
$('#deepl_config').addClass('missing');
return;
} else {
$('#deepl_config').removeClass('missing');
}
}
if (settings.translation === "openai") {
let selectedIndex = $('input[name="chatgpt_config_config_item"]:checked').length > 0;
if (!selectedIndex) {
$('.chatgpt_config a').removeClass('active');
$('.settings-page').removeClass('active');
$('#sidebar-translation-settings').addClass('active');
$('#translation-settings').addClass('active');
$('#chatgpt_config').addClass('missing');
return;
} else {
$('#chatgpt_config').removeClass('missing');
}
}
{
let selectedIndex = $('input[name="translation"]:checked').length > 0;
if (!selectedIndex) {
$('.OJBetter_setting_sidebar a').removeClass('active');
$('.settings-page').removeClass('active');
$('#sidebar-translation-settings').addClass('active');
$('#translation-settings').addClass('active');
$('#translationServices').addClass('missing');
return;
} else {
$('#translationServices').removeClass('missing');
}
}
// 保存数据
let refreshPage = false; // 是否需要刷新页面
for (const [key, value] of Object.entries(settings)) {
if (!refreshPage && !(key == 'translation' || key == 'darkMode' || key == 'commentTranslationChoice')) {
if (GM_getValue(key) != value) refreshPage = true;
}
GM_setValue(key, value);
}
for (const [key, value] of Object.entries(tempConfigs)) {
if (!refreshPage && (JSON.stringify(GM_getValue(key)) != JSON.stringify(value))) refreshPage = true;
GM_setValue(key, value);
}
if (refreshPage) location.reload();
else {
// 切换黑暗模式
if (OJBetter.basic.darkMode != settings.darkMode) {
OJBetter.basic.darkMode = settings.darkMode;
// 移除旧的事件监听器
changeEventListeners.forEach(listener => {
mediaQueryList.removeEventListener('change', listener);
});
if (OJBetter.basic.darkMode == "follow") {
changeEventListeners.push(handleColorSchemeChange);
mediaQueryList.addEventListener('change', handleColorSchemeChange);
$('html').removeAttr('data-theme');
} else if (OJBetter.basic.darkMode == "dark") {
$('html').attr('data-theme', 'dark');
if (OJBetter.monaco.editor) {
monaco.editor.setTheme('vs-dark');
}
} else {
$('html').attr('data-theme', 'light');
if (OJBetter.monaco.editor) {
monaco.editor.setTheme('vs');
}
// 移除旧的事件监听器
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)');
window.matchMedia('(prefers-color-scheme: dark)');
}
}
// 更新配置信息
OJBetter.translation.choice = settings.translation;
OJBetter.translation.comment.choice = settings.commentTranslationChoice;
}
}
}
OJB_closeAndRemoveModal(settingMenu);
$settingBtns.prop("disabled", false).removeClass("open");
});
});
};
/**
* 初始化html2markdown转换器
*/
async function initHTML2MarkDown() {
OJBetter.common.turndownService = new TurndownService({ bulletListMarker: '-' });
// 保留原始
OJBetter.common.turndownService.keep(['del']);
OJBetter.common.turndownService.addRule('removeByClass', {
filter: function (node) {
return node.classList.contains('html2md-panel') ||
node.classList.contains('div-btn-copy') ||
node.classList.contains('btn-copy') ||
node.classList.contains('ojb-overlay') ||
node.classList.contains('monaco-editor') ||
node.nodeName === 'SCRIPT' ||
node.nodeName === 'STYLE';
},
replacement: function () {
return '';
}
});
// inline math
OJBetter.common.turndownService.addRule('inline-math', {
filter: function (node, options) {
return node.tagName.toLowerCase() == "span" && node.className == "katex";
},
replacement: function (content, node) {
var latex = $(node).find('annotation').text();
// 替换防止 < >
latex = latex.replace(/</g, "<").replace(/>/g, ">");
return "$" + latex + "$";
}
});
// block math
OJBetter.common.turndownService.addRule('block-math', {
filter: function (node, options) {
return node.tagName.toLowerCase() == "span" && node.className == "katex-display";
},
replacement: function (content, node) {
var latex = $(node).find('annotation').text();
latex = latex.replace(/</g, "<").replace(/>/g, ">");
return "\n$$\n" + latex + "\n$$\n";
}
});
// pre
OJBetter.common.turndownService.addRule('pre', {
filter: function (node, options) {
return node.tagName.toLowerCase() == "pre";
},
replacement: function (content, node) {
if (!node.classList.contains('source-code-for-copy') && !node.classList.contains('prettyprint')) {
return "```\n" + content + "```\n";
} else {
return "";
}
}
});
// bordertable
OJBetter.common.turndownService.addRule('bordertable', {
filter: 'table',
replacement: function (content, node) {
if (node.classList.contains('table')) {
var output = [],
thead = '',
trs = node.querySelectorAll('tr');
if (trs.length > 0) {
var ths = trs[0].querySelectorAll('th, td');
if (ths.length > 0) {
thead = '| ' + Array.from(ths).map(th => OJBetter.common.turndownService.turndown(th.innerHTML.trim())).join(' | ') + ' |\n'
thead += '| ' + Array.from(ths).map(() => ' --- ').join('|') + ' |\n';
}
}
var rows = node.querySelectorAll('tr');
Array.from(rows).forEach(function (row, i) {
if (i > 0) {
var cells = row.querySelectorAll('td,th');
var trow = '| ' + Array.from(cells).map(cell => OJBetter.common.turndownService.turndown(cell.innerHTML.trim())).join(' | ') + ' |';
output.push(trow);
}
});
return thead + output.join('\n');
} else {
return content;
}
}
});
};
/**
* 任务队列
*/
class TaskQueue {
constructor() {
this.taskQueues = {};
this.timers = {}; // 定时器
this.delays = {}; // 等待时间(毫秒)
this.isProcessing = {}; // 处理状态
}
getDelay(type) {
return type === "openai" ? 0 : OJBetter.translation.waitTime;
}
/**
* 添加任务
* @param {string} type 任务类型
* @param {function} fn 任务函数
* @param {boolean} isNonQueueTask 是否为非队列任务
* @returns {Promise<*>} 任务执行的结果
*/
addTask(type, fn, isNonQueueTask = false) {
if (isNonQueueTask) return fn();
if (!this.taskQueues[type]) {
this.taskQueues[type] = [];
this.isProcessing[type] = false;
}
return new Promise((resolve, reject) => {
this.taskQueues[type].push({ fn, resolve, reject });
if (!this.isProcessing[type]) {
this.processNextTask(type);
}
});
}
async processNextTask(type) {
if (!this.taskQueues[type] || this.taskQueues[type].length === 0) {
this.isProcessing[type] = false;
return;
}
this.isProcessing[type] = true;
const { fn, resolve, reject } = this.taskQueues[type].shift();
try {
const result = await fn();
resolve(result);
} catch (error) {
reject(error);
}
const delay = this.getDelay(type);
if (delay > 0) {
await new Promise((resolve) => setTimeout(resolve, delay));
}
this.processNextTask(type);
}
}
/**
* 检测文本是否可能为代码片段
* @param {string} text 待检测的文本
* @returns {boolean} 是否可能为代码片段
*/
/**
* 检测为空文本
* @param {string} text 待检测的文本
* @returns {boolean} 是否为空文本
*/
const isEmptyText = text => text.trim() === '';
/**
* 加载按钮相关函数
*/
async function initButtonFunc() {
// 鼠标悬浮时为目标元素区域添加一个覆盖层
$.fn.addHoverOverlay = function (target) {
let position = $(target).css('position');
let display = $(target).css('display');
this.hover(() => {
$(target)
.addClass('ojb-overlay')
.css('position', 'relative');
if (display == "inline" || display == "contents") {
$(target).css('display', 'block');
}
}, () => {
$(target)
.removeClass('ojb-overlay')
.css('position', position);
if (display == "inline" || display == "contents") {
$(target).css('display', display);
}
})
}
/**
* 为按钮设置图标
* @param {string} icon 图标
* @returns {JQuery<HTMLElement>} 按钮
*/
$.fn.setButtonIcon = function (icon) {
let i = this.find("i");
if (i.length != 0 && i.hasClass("iconfont")) {
i.html(icon);
} else {
i = OJB_safeCreateJQElement(`<i>${icon}</i>`);
this.prepend(i);
}
return this;
}
/**
* 设置按钮为加载等待状态
*/
$.fn.setButtonLoading = function () {
this.addClass("loading");
this.prop("disabled", true);
return this;
}
/**
* 解除按钮的加载等待状态
*/
$.fn.setButtonLoaded = function () {
this.removeClass("loading");
this.prop("disabled", false);
return this;
}
/**
* 为按钮设置popover提示文本
* @param {string} text 文本
* @returns {JQuery<HTMLElement>} 按钮
*/
$.fn.setButtonPopover = function (text) {
// find if has popover_content class element
let popover_content = this.find(".popover_content");
if (popover_content.length != 0) {
popover_content.text(text);
} else {
popover_content = OJB_safeCreateJQElement(`<span class="popover_content">${text}</span>`);
this.append(popover_content);
}
return this;
}
/**
* 获取MarkDown
* @returns {string} MarkDown
*/
$.fn.getMarkdown = function () {
const markdown = this.data('markdown');
if (markdown === undefined) {
const htmlContent = this.html();
const newMarkdown = OJBetter.common.turndownService.turndown(htmlContent);
this.data('markdown', newMarkdown);
return newMarkdown;
}
return markdown;
}
// 设置按钮状态
$.fn.setButtonState = function (state, popoverText = null, disabled = false) {
this.data('buttonState', state)
.prop('disabled', disabled)
.css('cursor', disabled ? 'not-allowed' : 'pointer')
.removeClass('running success enabled error loading redo');
if (popoverText) this.setButtonPopover(popoverText);
if (state !== 'initial') this.addClass(state);
return this;
};
// 为按钮添加鼠标悬浮重试
$.fn.setHoverRedo = function () {
this.hover(() => {
prevState = this.getButtonState();
if (prevState !== "normal" && prevState !== "running") {
this.setButtonState('redo');
}
}, () => {
const currentState = this.getButtonState();
if (prevState !== "normal" && ["normal", "redo"].includes(currentState)) {
this.setButtonState(prevState);
prevState = null;
}
});
};
// 获取按钮状态
$.fn.getButtonState = function () {
return this.data('buttonState') || 'normal';
};
// 设置翻译按钮状态
$.fn.setTransButtonState = function (state, text = null) {
const popoverText = text || i18next.t(`trans.${state}`, { ns: 'button' });
const disabled = state === 'running' || state === 'loading';
this.setButtonState(state, popoverText, disabled);
return this;
};
// 存翻译结果
$.fn.pushResultToTransButton = function (result) {
let resultStack = this.data('resultStack');
if (!resultStack) resultStack = [];
resultStack.push(result);
this.data('resultStack', resultStack);
}
// 获取翻译结果
$.fn.getResultFromTransButton = function () {
return this.data('resultStack');
}
// 标记为不自动翻译
$.fn.setNotAutoTranslate = function () {
this.data('notAutoTranslate', true);
}
// 获取是否为不自动翻译
$.fn.getNotAutoTranslate = function () {
return this.data('notAutoTranslate');
}
// 判断是否已经翻译
$.fn.IsTranslated = function () {
if (this.hasAttr('translated')) {
return true;
} else {
return false;
}
}
// 判断是否为评论区按钮
$.fn.IsCommentButton = function () {
let isCommentButton = this.data('isCommentButton');
if (isCommentButton == undefined) {
this.parents('.comments').length > 0 ? isCommentButton = true : isCommentButton = false;
this.data('isCommentButton', isCommentButton);
}
return isCommentButton;
}
// 按钮点击效果
$(document).on('mousedown', '.ojb_btn', function () {
$(this).addClass('active').on('animationend', () => $(this).removeClass('active'));
});
}
/**
* 添加题目markdown转换/复制/翻译按钮面板
* @param {HTMLElement} element 需要添加按钮面板的元素
* @param {string} suffix 按钮面板id后缀
* @param {string} type 按钮面板添加位置
* @param {boolean} is_simple 是否是简单模式
* @returns {object} 返回按钮面板元素
*/
function addButtonPanel(element, suffix, type, is_simple = false) {
let text;
if (OJBetter.translation.comment.transMode == "1") text = i18next.t('trans.segment', { ns: 'button' });
else if (OJBetter.translation.comment.transMode == "2") text = i18next.t('trans.select', { ns: 'button' });
else text = i18next.t('trans.normal', { ns: 'button' });
let panel = OJB_safeCreateJQElement(`<div class='html2md-panel input-output-copier ${is_simple ? 'is_simple' : ''}'></div>`);
let viewButton = OJB_safeCreateJQElement(`
<button class='ojb_btn ojb_btn_popover top' id='html2md-view${suffix}' type='button'>
<i class="iconfont"></i>
<span class="popover_content">${i18next.t('md.normal', { ns: 'button' })}</span>
</button>`);
let copyButton = OJB_safeCreateJQElement(`
<button class='ojb_btn ojb_btn_popover top' id='html2md-cb${suffix}' type='button'>
<i class="iconfont"></i>
<span class="popover_content">${i18next.t('copy.normal', { ns: 'button' })}</span>
</button>`);
let translateButton = OJB_safeCreateJQElement(`
<button class='ojb_btn translateButton ojb_btn_popover top' id='translateButton${suffix}' type='button'>
<i class="iconfont"></i>
<span class="popover_content">${text}</span>
</button>`);
if (!is_simple) panel.append(viewButton);
if (!is_simple) panel.append(copyButton);
panel.append(translateButton);
if (type === "this_level") {
$(element).before(panel);
} else if (type === "child_level") {
$(element).prepend(panel);
}
return {
panel: panel,
viewButton: viewButton,
copyButton: copyButton,
translateButton: translateButton
}
}
/**
* 添加MD视图按钮
* @param {JQuery<HTMLElement>} button 按钮
* @param {JQuery<HTMLElement>} element 目标元素
* @param {string} suffix id后缀
* @param {string} type 类型
* @returns {void}
*/
async function addButtonWithHTML2MD(button, element, suffix, type) {
/**
* 改变按钮状态
* @param {string} state 状态
*/
const changeButtonState = (state) => {
if (state == "loading") {
button.setButtonLoading();
button.setButtonPopover(i18next.t('state.waitMathJax', { ns: 'button' }));
} else if (state == "loaded") {
button.setButtonLoaded();
button.setButtonPopover(i18next.t('md.normal', { ns: 'button' }));
} else if (state == "normal") {
button.removeClass("enabled");
button.setButtonPopover(i18next.t('md.normal', { ns: 'button' }));
} else if (state == "mdView") {
button.addClass("enabled");
button.setButtonPopover(i18next.t('md.reduction', { ns: 'button' }));
} else if (state == "disabled") {
button.prop("disabled", true);
button.setButtonPopover(i18next.t('md.disabled', { ns: 'button' }));
}
}
/**
* 存放目标元素的 JQueryObject
*/
const target = (() => {
if (type = "child_level") {
return $(element).children(':not(.html2md-panel)');
} else {
return $(element);
}
})();
if (OJBetter.typeOfPage.is_oldLatex || OJBetter.typeOfPage.is_acmsguru) {
changeButtonState("disabled");
return;
} else {
changeButtonState("loading");
await waitForMathJaxIdle();
changeButtonState("loaded");
}
button.click(OJB_debounce(function () {
/**
* 检查是否是MarkDown视图
* @returns {boolean} 是否是MarkDown视图
*/
function checkViewmd() {
if ($(element).attr("viewmd") === "true") {
return true;
} else {
return false;
}
}
/**
* 设置是否是MarkDown视图
* @param {boolean} value 是否是MarkDown视图
* @returns {void}
*/
function setViewmd(value) {
$(element).attr("viewmd", value);
if (value) {
changeButtonState("mdView");
} else {
changeButtonState("normal");
}
}
if (checkViewmd()) {
setViewmd(false);
target.last().next(".mdViewContent").remove();
target.show();
} else {
setViewmd(true);
const markdown = $(element).getMarkdown();
const mdViewContent = OJB_safeCreateJQElement(`<span class="mdViewContent" style="width:auto; height:auto;">${markdown}</span>`);
target.last().after(mdViewContent);
target.hide();
}
}));
if (OJBetter.preference.hoverTargetAreaDisplay && !OJBetter.typeOfPage.is_oldLatex && !OJBetter.typeOfPage.is_acmsguru) {
button.addHoverOverlay($(element));
}
}
/**
* 添加复制按钮
* @param {JQuery<HTMLElement>} button 按钮
* @param {JQuery<HTMLElement>} element 目标元素
* @param {string} suffix 后缀
* @param {string} type 类型
*/
async function addButtonWithCopy(button, element, suffix, type) {
/**
* 改变按钮状态
* @param {string} state 状态
*/
function changeButtonState(state) {
if (state == "loading") {
button.setButtonLoading();
button.setButtonPopover(i18next.t('state.waitMathJax', { ns: 'button' }));
} else if (state == "loaded") {
button.setButtonLoaded();
button.setButtonPopover(i18next.t('copy.normal', { ns: 'button' }));
} else if (state == "normal") {
button.setButtonPopover(i18next.t('copy.normal', { ns: 'button' }));
} else if (state == "copied") {
button.setButtonPopover(i18next.t('copy.copied', { ns: 'button' }));
} else if (state == "disabled") {
button.prop("disabled", true);
button.setButtonPopover(i18next.t('copy.disabled', { ns: 'button' }));
}
}
// 等待MathJax队列完成
if (OJBetter.typeOfPage.is_oldLatex || OJBetter.typeOfPage.is_acmsguru) {
changeButtonState("disabled");
return;
} else {
changeButtonState("loading");
await waitForMathJaxIdle();
changeButtonState("loaded");
}
button.click(OJB_debounce(function () {
var target = $(element).get(0);
var markdown = $(element).getMarkdown();
// 得到的应当是原字符串,getMarkdown得到的字符被转义
GM_setClipboard(OJB_unescapeHtml(markdown));
$(this).addClass("success");
changeButtonState("copied");
// 更新复制按钮文本
setTimeout(() => {
$(this).removeClass("success");
changeButtonState("normal")
}, 2000);
}));
if (OJBetter.preference.hoverTargetAreaDisplay && !OJBetter.typeOfPage.is_oldLatex && !OJBetter.typeOfPage.is_acmsguru) {
button.addHoverOverlay($(element));
}
}