本工具由 @chenbs 和我共同编写。
// ==UserScript==
// @name WZOI 辅助工具
// @namespace http://tampermonkey.net/
// @description 在 WZOI 中,给样例输入输出、表格等添加复制按钮。并可以一键吸氧、增加搜索、查看自己以前的 AC 代码。
// @version 2026-02-09
// @author chenbs,wanganze
// @match *://*/*
// @grant none
// @license MIT
// ==/UserScript==
(function () {
'use strict';
//判断
if(document.getElementsByClassName('mt-auto').length == 0 || document.getElementsByClassName('mt-auto')[0].innerHTML == null || document.getElementsByClassName('mt-auto')[0].innerHTML.search("wzoj") == -1)return;
//跳转
if(window.location.pathname=="/tags-chart")
{
let s = window.origin + "/search?name=&search_item=problems&tags%5B%5D=" + window.location.hash.substr(5);
console.log(s);
window.location.replace(s);
}
// 复制字符串函数
function copy(text) {
let input = document.createElement('textarea');
input.setAttribute('id', 'input_for_copyText');
input.value = text;
document.getElementsByTagName('body')[0].appendChild(input);
document.getElementById('input_for_copyText').select();
document.execCommand('copy');
document.getElementById('input_for_copyText').remove();
}
// 样例
{
function split_yl(x, txt) { // 成功返回 0
if (x.innerText.includes(txt)) {
console.log(x,txt);
// 分割多个样例
let tmp = x.innerText.split(txt);
for (let j = 1; j < tmp.length; j++) tmp[j] = tmp[j].substr(tmp[j].indexOf("\n")).trim();
for (let j = 1; j < tmp.length; j++) {
let pre = document.createElement("pre");
pre.className = "sample_io";
pre.innerText = tmp[j];
x.parentNode.insertBefore(pre, x);
}
x.remove();
return 0;
}
return 1;
}
let fa = Array.from(document.getElementsByClassName("sample_io"));
console.log(fa);
for (let i = 0; i < fa.length; i++) {
if(split_yl(fa[i], "输入样例") && split_yl(fa[i], "输出样例") && split_yl(fa[i], "样例")) {
// 不用分割
// 创建一个新的,把原来的 pre 元素删掉
let pre = document.createElement("pre");
pre.className = "sample_io";
pre.innerText = fa[i].innerText;
fa[i].parentNode.insertBefore(pre, fa[i]);
fa[i].remove();
}
}
fa = Array.from(document.getElementsByClassName("sample_io"));
for (let i = 0; i < fa.length; i++) {
while (fa[i].innerText[fa[i].innerText.length - 1] == "\n") fa[i].innerText = fa[i].innerText.substr(0, fa[i].innerText.length - 1); // 删除末尾回车
let cp = document.createElement("button");
cp.className = "new-button";
cp.innerText = "复制";
cp.onclick = function () { let s = this.parentNode.innerText; copy(s.substr(0, s.length - 3)); };
fa[i].appendChild(cp);
}
let tmp = Array.from(document.getElementsByClassName("text-muted"));
for (let i = 0; i < tmp.length; i++)
{
if (tmp[i].innerText == "(双击复制)") tmp[i].remove();
}
}
// 个人简介
let fa = document.getElementsByClassName("card-header");
for (let i = 0; i < fa.length; i++) {
if (fa[i].parentNode.className != "card") continue;
fa[i].style.display = "flex";
let cp = document.createElement("button");
cp.className = "new-button";
cp.innerText = "复制";
cp.style.marginLeft = "15px";
cp.onclick = function () { let s = this.parentNode.parentNode.children[1].innerText; copy(s); };
fa[i].appendChild(cp);
}
// 题解
fa = document.getElementsByClassName("language-cpp");
for(let i=0; i<fa.length; i++){
if(fa[i].tagName != "PRE") continue;
if(fa[i].className.includes("line-numbers") || window.location.hostname == "wzoi.cn") continue;
let cp = document.createElement("button");
cp.className = "new-button";
cp.innerText = "复制";
cp.onclick = function(){let s=this.parentNode.innerText;copy(s.substr(3,s.length));};
fa[i].insertBefore(cp, fa[i].firstChild);
}
// 表格
fa = document.getElementsByTagName("table");
for (let i = 0; i < fa.length; i++) {
if (fa[i].children[0].tagName != "THEAD") continue;
fa[i].style.cssText += "margin-top: 5px !important";
let cp = document.createElement("button");
cp.className = "new-button";
cp.innerText = "复制表格 Markdown";
cp.onclick = function () {
function calc(s) { // 转义字符
let s2 = "";
for (let i = 0; i < s.length; i++) {
if (s[i] == "\n") {
s2 += " ";
} else {
if (s[i] == "|" || s[i] == "*" || s[i] == "~") s2 += "\\";
s2 += s[i];
}
}
return s2;
}
// 表格头部
let head = this.parentNode.children[1].children[0].children;
let s = "|";
for (let j = 0; j < head.length; j++) s += calc(head[j].innerText) + "|";
s += "\n|";
for (let j = 0; j < head.length; j++) s += ":-:|";
// 表格内容
let body = this.parentNode.children[2].children;
for (let j = 0; j < body.length; j++) {
s += "\n|";
for (let k = 0; k < body[j].children.length; k++) {
// 显示打勾图标
if (body[j].children[k].innerHTML.includes("class=\"fa fa-check\"")) s += "✅|";
// 显示完整用户名
else if (body[j].children[k].innerHTML.includes("user-badge badge badge-light")) {
if (body[j].children[k].children[0].children[0].dataset.nickname == "") {s += body[j].children[k].children[0].children[0].dataset.uname + "|";}
else s += body[j].children[k].children[0].children[0].dataset.nickname + "|"; // 有真名就显示真名
}
else s += calc(body[j].children[k].innerText) + "|";
}
}
copy(s);
};
fa[i].parentElement.insertBefore(cp, fa[i].parentElement.firstChild);
}
// 样式
let tmp = document.createElement("style");
tmp.innerText = `
.new-button{
transition:all .2s ease;
border-radius:114514px;
display:block;
}
.new-button:hover{
background: #DDDDDD;
}
.new_div{
transition:all .2s ease;
border-radius:114514px;
}
.new_div:hover{
background: #DDDDDD;
}
`;
document.body.appendChild(tmp);
// 一键吸氧
{
let fa = document.getElementById("sol-form");
if (fa != null) {
const o3 = "#pragma GCC optimize(3,\"Ofast\",\"inline\")";
const o2 = "#pragma GCC optimize(2)";
const TEXTnode = fa.children[5].children[0];
let tmp = document.createElement("button");
let tmp1 = document.createElement("button");
tmp.className = "btn btn-primary";
tmp1.className = "btn btn-primary";
tmp.style="margin-right:4px "
tmp1.innerText = "吸臭氧";
tmp.innerText = "吸氧";
TEXTnode.addEventListener("input", () => {
if (TEXTnode.value.includes(o2)) tmp.innerText = "取消吸氧";
else tmp.innerText = "吸氧";
});
tmp.onclick = function (event) {
if (tmp.innerText == "取消吸氧") {
TEXTnode.value = TEXTnode.value.replaceAll(o2 + "\n", "").replaceAll(o2, "");
tmp.innerText = "吸氧";
} else {
TEXTnode.value = o2 + "\n" + TEXTnode.value;
tmp.innerText = "取消吸氧";
}
event.stopPropagation();
return false;
}
fa.appendChild(tmp);
TEXTnode.addEventListener("input", () => {
if (TEXTnode.value.includes(o3)) tmp1.innerText = "取消吸臭氧";
else tmp1.innerText = "吸臭氧";
});
tmp1.onclick = function (event) {
if (tmp1.innerText == "取消吸臭氧") {
TEXTnode.value = TEXTnode.value.replaceAll(o3 + "\n", "").replaceAll(o3, "");
tmp1.innerText = "吸臭氧";
} else {
TEXTnode.value = o3 + "\n" + TEXTnode.value;
tmp1.innerText = "取消吸臭氧";
}
event.stopPropagation();
return false;
}
fa.appendChild(tmp1);
}
}
// 评测记录显示优化
{
let style = document.createElement("style");
style.innerText = ".progress-bar{border:1px solid black;}";
document.body.appendChild(style);
setInterval(function () {
var p = document.getElementsByClassName("progress clickable");
for (let i = 0; i < p.length; i++) {
if (p[i].dataset.vis == undefined) {
p[i].dataset.vis = 1;
fetch('/solutions/' + p[i].parentNode.parentNode.id.substr(5)).then(response => response.text()) // 网页请求
.then(function (data) {
let v = 0, j = 0;
while (1) {
v = data.indexOf("<td>", v + 1);
if (v == -1) return 0;
let tmps = data.substr(v + 4);
tmps = tmps.substr(0, tmps.indexOf("<"));
j++;
if (j >= 13) {
let idx = ~~((j - 13) / 6);
if ((j - 13) % 6 == 1) p[i].children[idx].dataset.score = tmps;
else if ((j - 13) % 6 == 2) p[i].children[idx].dataset.time = tmps;
else if ((j - 13) % 6 == 3) p[i].children[idx].dataset.mem = tmps;
else if ((j - 13) % 6 == 4) p[i].children[idx].dataset.state = tmps;
}
}
}).then(function () {
for (let j = 0; j < p[i].children.length; j++) {
let ele = p[i].children[j];
let newele = document.createElement("div");
newele.innerText = ele.dataset.state + "(" + ele.dataset.time + "," + ele.dataset.mem + ")";
newele.style.display = "none";
ele.appendChild(newele);
ele.onmouseover = function () {
this.firstChild.style.display = "block"
this.style.width = 100 / this.parentNode.children.length * 2 + "%";
};
ele.onmouseout = function () {
this.firstChild.style.display = "none";
this.style.width = 100 / this.parentNode.children.length + "%";
};
}
});
}
}
}, 100);
}
// 题库中快速查看自己的 AC 代码
{
if (!document.getElementsByClassName("dropdown-item")[0]) return; // 未登录则不触发
const username = document.getElementsByClassName("dropdown-item")[0].innerText.trim();
// 展示结果的框(cb = code box)
let cb = document.createElement("div");
cb.style.position = "fixed";
cb.style.background = "#eee";
cb.style.zIndex = 114514;
cb.style.maxHeight = "500px";
cb.style.maxWidth = "700px";
cb.style.overflow = "auto";
cb.style.padding = "20px";
cb.style.right = "0px";
cb.style.display = "none";
var format = 2, code, code2;
let cb_top = document.createElement("div");
cb_top.style.display = "flex";
cb_top.style.marginBottom = "20px";
let cb_top_btn = document.createElement("button");
let cb_top_btn2 = document.createElement("button");
let cb_top_btn3 = document.createElement("button");
let cp = document.createElement("button");
cb_top_btn.className = "new-button";
cb_top_btn2.className = "new-button";
cb_top_btn3.className = "new-button";
cb_top_btn.innerText = "题解格式";
cb_top_btn2.innerText = "仅显示代码";
cb_top_btn3.innerText = "关闭";
cp.className = "new-button";
cp.innerText = "复制";
cp.onclick = function () { copy(pre.innerText); };
//cb_top_btn.style.padding = cb_top_btn2.style.padding = cb_top_btn3.style.padding = "5px";
//cb_top_btn.style.border = cb_top_btn2.style.border = cb_top_btn3.style.border = "2px solid #000";
cb_top.appendChild(cb_top_btn);
cb_top.appendChild(cb_top_btn2);
cb_top.appendChild(cb_top_btn3);
cb_top.appendChild(cp);
function update() {
if (format == 1) pre.innerHTML = code2;
else pre.innerHTML = code;
}
cb_top_btn.onclick = () => { format = 1; update() };
cb_top_btn2.onclick = () => { format = 2; update() };
cb_top_btn3.onclick = () => { cb.style.display = "none" };
let pre = document.createElement("pre");
document.body.appendChild(cb);
cb.appendChild(cb_top);
cb.appendChild(pre);
function updatecode(c1,c2){code = c1;code2 = c2}
let regexp = new RegExp(location.origin+"/s/\\d*/\\d*");
console.log(regexp);
setInterval(async function () {
// 添加查看按钮
let body = document.getElementsByTagName("tbody")[0];
if (!body) return;
for (let i = 0; i < body.children.length; i++) {
let e = body.children[i];
if (!e.children[2] || !e.children[2].children[0] || !regexp.test(e.children[2].children[0].href)) return 1;
if (e.children[0].dataset.vis == 1) continue;
// 计算题目的 id,如果无效就跳过
let purl = e.children[2].children[0].href, pid = "";
if (purl == undefined) continue;
for (let j = purl.length - 1; purl[j] != "/" && j >= 0; j--) pid = purl[j] + pid;
if (isNaN(Number(pid))) continue;
e.children[0].dataset.vis = 1;
e.children[0].addEventListener("click",function (){
pre.innerHTML = `<p style="text-align:center; height:100px; width:100px; line-height:100px;">查询中,请稍等</p>`;
cb.style.display = "block";
fetch(`/solutions?problemset_id=&user_name=${username}&problem_id=${pid}&score_min=100&score_max=&language=&status=4`)
.then((r) => r.text())
.then((data) => {
let k = data.indexOf("a href=\"/solution");
if (k == -1) {
updatecode(`<p style="text-align:center; height:120px; line-height:120px;">未找到</p>`,`<p style="text-align:center; height:120px; line-height:120px;">未找到</p>`);
//code = code2 = `<p style="text-align:center; height:120px; line-height:120px;">未找到</p>`;
update();
} else {
k += 19;
let sid = "";
while (data[k] != "\"") sid += data[k++];
console.log(sid);
fetch(`/solutions/${sid}`)
.then((response) => response.text())
.then((data) => {
updatecode(data.substring(data.indexOf("<code class=\"language-cpp\">") + 27, data.indexOf("</code>")),
"#### " + e.children[1].innerText + "." + e.children[2].innerText + "\n```cpp\n" + data.substring(data.indexOf("<code class=\"language-cpp\">") + 27, data.indexOf("</code>")) + "\n```");
//code = data.substring(data.indexOf("<code class=\"language-cpp\">") + 27, data.indexOf("</code>"));
//code2 = "#### " + e.children[1].innerText + "." + e.children[2].innerText + "\n```cpp\n" + code + "\n```";
update();
});
}
});
});
}
}, 100);
function calc(i) {
let body = document.getElementsByTagName("tbody")[0];
if (!body) return;
let e = body.children[i];
if (!e.children[2] || !e.children[2].children[0] || !regexp.test(e.children[2].children[0].href)) return 1;
if (e.children[0].innerHTML.includes("fa-check")) return 1;
// 计算题目的 id,如果无效就跳过
let purl = e.children[2].children[0].href, pid = "";
if (purl == undefined) return 1;
for (let j = purl.length - 1; purl[j] != "/" && j >= 0; j--) pid = purl[j] + pid;
if (isNaN(Number(pid))) return 1;
fetch(`/solutions?problemset_id=&user_name=${username}&problem_id=${pid}&score_min=100&score_max=&language=&status=4`)
.then((r) => r.text())
.then((data) => {
if (data.indexOf("a href=\"/solution") != -1) {
if (e.children[0].dataset.in_search != 1) {e.children[0].innerHTML = `<span class="fa fa-check" style="color:
#00800066;"></span>`;}
else e.children[0].innerHTML = `<span class="fa fa-check" style="color: green"></span>`;
// 写入缓存
localStorage.setItem(username + "_" + location.href + "_" + i, "1");
}
});
console.log("get", i);
return 0;
}
function init() {
let tbody = document.getElementsByTagName("tbody")[0];
let tmp = tbody.children[0].children[1];
console.log("init");
// console.log(tmp);
if (tmp && tmp.children[0] && regexp.test(tmp.children[0].href)) {
console.log(tbody);
for (let i = 0; i < tbody.children.length; i++) {
let e = document.createElement("td");
e.dataset.in_search = 1;
tbody.children[i].insertBefore(e, tbody.children[i].children[0]);
}
let thead = document.getElementsByTagName("thead")[0].children[0];
let e = document.createElement("th");
thead.insertBefore(e, thead.children[0]);
}
// 加载缓存
for (let i = 0; i < tbody.children.length; i++) {
let e = tbody.children[i].children[0];
if (e.children[0] && e.children[0].className.includes("fa-check")) continue;
if (localStorage.getItem(username + "_" + location.href + "_" + i) == "1") {
if (e.dataset.in_search != 1) e.innerHTML = `<span class="fa fa-check" style="color: #00800066;"></span>`;
else e.innerHTML = `<span class="fa fa-check" style="color: green"></span>`;
}
}
}
let i = 0;
setInterval(() => {
let body = document.getElementsByTagName("tbody")[0];
if (!body) return;
if (body.dataset.init != 1) {
body.dataset.init = 1;
init();
}
if (i >= body.children.length) return; // 防止数组越界
while (calc(i)) {
i++;
if (i >= body.children.length) return;
}
i++;
}, 200);
}
if (window.location.href.includes("/users/")) {
// 显示排名
let flag = (!document.getElementsByClassName("card-title")[0]); // 未手机验证的用户
let id = window.location.href.substring(window.location.href.indexOf("users") + 6);
for (let page = 1; page <= 20; page++) {
setTimeout(() => {
fetch(`/ranklist?page=${page}`)
.then((r) => r.text())
.then((data) => {
let p = data.indexOf(`data-uid="${id}"`);
if (p != -1) {
while (p > 0 && data.substring(p, p + 5) != "</td>") p--;
if (data.substring(p, p + 5) != "</td>") return;
let p2 = p;
while (p2 > 0 && data[p2] != ">") p2--;
if (data[p2] != ">") return;
let rank = data.substring(p2 + 1, p);
console.log("rank", rank);
let tmp = document.getElementsByClassName(flag ? "container h-100" : "card-title")[0];
let node = document.createElement("span");
node.className="new_div";
node.innerText = "#" + rank;
node.style.marginLeft = "20px";
node.style.fontFamily = "Exo, Consolas";
node.style.fontSize = "12px";
node.style.border = "1px solid #000";
node.style.background = "rgb(255, 245, 245)";
node.style.padding = "5px";
tmp.appendChild(node);
}
});
}, (page - 1) * 200);
}
}
})();