OJ题面 & 题解复制面板 v1.0
JavaScript// ==UserScript==
// @name OJ题面&题解复制面板
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 提取UiContextNew题面 / 批量拉取solution raw,悬浮剪贴板样式块,点击复制(兼容http内网)
// @author zhangxiaoyu008
// @match http://192.168.110.12/p/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 悬浮容器,固定在右下角
const panelWrap = document.createElement('div');
panelWrap.style.position = 'fixed';
panelWrap.style.bottom = '20px';
panelWrap.style.right = '20px';
panelWrap.style.zIndex = '99999';
panelWrap.style.maxWidth = '320px';
panelWrap.style.maxHeight = '70vh';
panelWrap.style.overflowY = 'auto';
panelWrap.style.display = 'flex';
panelWrap.style.flexDirection = 'column';
panelWrap.style.gap = '8px';
document.body.appendChild(panelWrap);
// ✅ 兼容HTTP页面的复制函数(解决navigator.clipboard undefined)
async function copyText(text) {
const ta = document.createElement('textarea');
ta.value = text;
ta.style.position = 'absolute';
ta.style.left = '-9999px';
document.body.appendChild(ta);
ta.select();
const ok = document.execCommand('copy');
document.body.removeChild(ta);
return ok;
}
// 创建单个复制卡片
function createCopyBlock(text, title) {
const block = document.createElement('div');
block.style.background = '#fff';
block.style.border = '1px solid #ccc';
block.style.borderRadius = '6px';
block.style.padding = '10px';
block.style.cursor = 'pointer';
block.style.boxShadow = '0 2px 8px #00000022';
block.style.fontSize = '13px';
block.style.whiteSpace = 'pre-wrap';
block.style.overflow = 'hidden';
block.style.textOverflow = 'ellipsis';
block.style.maxHeight = '120px';
block.title = `点击复制【${title}】`;
block.textContent = text.slice(0,300) + (text.length>300 ? '\n...(太长截断)' : '');
block.onclick = async () => {
try {
const success = await copyText(text);
if(success){
block.style.background = '#d9f7be';
setTimeout(()=>block.style.background='#fff', 800);
}else{
alert("复制失败");
}
}catch(e){
console.error(e);
alert("复制异常:"+e.message);
}
}
panelWrap.appendChild(block);
}
// ---------------------- 题目页面 /p/* ----------------------
async function handleProblemPage(){
if(!window.UiContextNew) return;
try{
const outer = JSON.parse(window.UiContextNew);
const pdocContent = JSON.parse(outer.pdoc.content);
const zhText = pdocContent.zh;
createCopyBlock(zhText, "中文题面 zh");
}catch(err){
console.error("解析UiContextNew失败",err);
createCopyBlock("解析失败:"+err.message, "题面解析错误");
}
}
// ---------------------- 题解页面 /p/*/solution ----------------------
async function get_raw(url) {
try {
const resp = await fetch(url);
if (!resp.ok) {
console.error("获取失败,HTTP状态码:", resp.status);
return `请求失败 status:${resp.status}`;
}
const text = await resp.text();
console.log("拿到raw文本内容:\n", text);
return text;
} catch (err) {
console.error("请求异常", err);
return "网络异常:"+err.message;
}
}
async function handleSolutionPage(){
const t = document.getElementsByClassName('typo richmedia collapsible-content');
for(let i = 0; i < t.length; i ++){
const rawUrl = t[i].dataset.rawUrl;
const fullUrl = location.origin + rawUrl;
const rawTxt = await get_raw(fullUrl);
createCopyBlock(rawTxt, `题解raw #${i+1}`);
}
}
// 路由判断
function routeCheck(){
const path = location.pathname;
if(path.match(/^\/p\/.+\/solution$/)){
handleSolutionPage();
}else if(path.match(/^\/p\//)){
handleProblemPage();
}
}
window.addEventListener('load', ()=>{
setTimeout(routeCheck, 800);
})
})();
OJ题面 & 题解复制面板 v2.0
C++// ==UserScript==
// @name OJ题面&题解复制面板
// @namespace http://tampermonkey.net/
// @version 2.0
// @description 提取UiContextNew题面 / 批量拉取solution raw,精简面板并深度优化首屏卡顿
// @author zhangxiaoyu008
// @match http://192.168.110.12/p/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
let panelWrap = null;
// 懒加载悬浮容器(避免一打开页面就无条件操作 DOM)
function getOrCreatePanel() {
if (panelWrap) return panelWrap;
panelWrap = document.createElement('div');
panelWrap.style.cssText = `
position: fixed;
bottom: 24px;
right: 24px;
z-index: 99999;
min-width: 200px;
max-width: 280px;
max-height: 65vh;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 6px;
padding: 8px;
background: #ffffff;
border: 1px solid #e2e8f0;
border-radius: 8px;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
will-change: transform;
`;
document.body.appendChild(panelWrap);
return panelWrap;
}
// 轻量 Toast 提示
function showToast(message) {
const toast = document.createElement('div');
toast.textContent = message;
toast.style.cssText = `
position: fixed;
top: 24px;
left: 50%;
transform: translateX(-50%);
background-color: #10b981;
color: #fff;
padding: 6px 16px;
border-radius: 6px;
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.12);
font-size: 13px;
z-index: 100000;
pointer-events: none;
opacity: 0;
transition: opacity 0.2s ease;
`;
document.body.appendChild(toast);
requestAnimationFrame(() => {
toast.style.opacity = '1';
});
setTimeout(() => {
toast.style.opacity = '0';
setTimeout(() => toast.remove(), 200);
}, 1500);
}
// HTTP 兼容版剪贴板复制
function copyText(text) {
const ta = document.createElement('textarea');
ta.value = text;
ta.setAttribute('readonly', '');
ta.style.cssText = 'position:fixed;left:-9999px;top:-9999px;opacity:0;';
document.body.appendChild(ta);
ta.select();
let ok = false;
try {
ok = document.execCommand('copy');
} catch (_) {}
document.body.removeChild(ta);
return ok;
}
// 构建单个条目 DOM
function buildRowElement(text, title) {
const row = document.createElement('div');
row.style.cssText = `
display: flex;
align-items: center;
justify-content: space-between;
padding: 5px 8px;
background: #f8fafc;
border: 1px solid #f1f5f9;
border-radius: 5px;
gap: 8px;
`;
const label = document.createElement('span');
label.textContent = title;
label.style.cssText = `
font-size: 12px;
color: #334155;
font-weight: 500;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
flex: 1;
`;
label.title = title;
const btn = document.createElement('button');
btn.textContent = '复制';
btn.style.cssText = `
border: none;
background-color: #3b82f6;
color: #fff;
padding: 3px 8px;
font-size: 12px;
border-radius: 4px;
cursor: pointer;
flex-shrink: 0;
`;
btn.onclick = () => {
if (copyText(text)) {
showToast(`已复制:${title}`);
} else {
alert('复制失败,请重试');
}
};
row.appendChild(label);
row.appendChild(btn);
return row;
}
// 题目页提取
function handleProblemPage() {
if (!window.UiContextNew) return;
try {
const outer = JSON.parse(window.UiContextNew);
const pdocContent = JSON.parse(outer.pdoc.content);
const zhText = pdocContent.zh;
const panel = getOrCreatePanel();
panel.appendChild(buildRowElement(zhText, '中文题面 (zh)'));
} catch (err) {
console.error('解析UiContextNew失败', err);
}
}
// 题解页并发提取
async function handleSolutionPage() {
const elements = document.getElementsByClassName('typo richmedia collapsible-content');
if (!elements.length) return;
// 并发请求所有 raw,避免串行网络等待
const fetchTasks = Array.from(elements).map(async (el, idx) => {
const rawUrl = el.dataset.rawUrl;
if (!rawUrl) return null;
try {
const resp = await fetch(location.origin + rawUrl);
if (!resp.ok) return null;
const text = await resp.text();
return { title: `题解 Raw #${idx + 1}`, text };
} catch (_) {
return null;
}
});
const results = await Promise.all(fetchTasks);
const validResults = results.filter(Boolean);
if (!validResults.length) return;
// 利用 DocumentFragment 单次更新 DOM,消除多次重排
const frag = document.createDocumentFragment();
validResults.forEach(item => {
frag.appendChild(buildRowElement(item.text, item.title));
});
const panel = getOrCreatePanel();
panel.appendChild(frag);
}
// 空闲调度执行
function run() {
const schedule = window.requestIdleCallback || ((cb) => setTimeout(cb, 100));
schedule(() => {
const path = location.pathname;
if (/^\/p\/.+\/solution$/.test(path)) {
handleSolutionPage();
} else if (/^\/p\//.test(path)) {
handleProblemPage();
}
});
}
// 只要 DOM 骨架就绪即进入调度队列,无需等待高开销静态资源完全加载
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', run, { once: true });
} else {
run();
}
})();
OJ题面 & 题解复制面板 v2.5
JavaScript// ==UserScript==
// @name OJ题面&题解复制面板
// @namespace http://tampermonkey.net/
// @version 2.5
// @description 提取UiContextNew题面 / 批量拉取solution raw,精简面板并深度优化首屏卡顿
// @author zhangxiaoyu008
// @match http://192.168.110.118/p/*
// @match http://192.168.110.20/p/*
// @match http://sloj.cn/p/*
// @match http://superoj.cn/p/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
let panelWrap = null;
// 懒加载悬浮容器(避免一打开页面就无条件操作 DOM)
function getOrCreatePanel() {
if (panelWrap) return panelWrap;
panelWrap = document.createElement('div');
panelWrap.style.cssText = `
position: fixed;
bottom: 24px;
right: 24px;
z-index: 99999;
min-width: 200px;
max-width: 280px;
max-height: 65vh;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 6px;
padding: 8px;
background: #ffffff;
border: 1px solid #e2e8f0;
border-radius: 8px;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
will-change: transform;
`;
document.body.appendChild(panelWrap);
return panelWrap;
}
// 轻量 Toast 提示
function showToast(message) {
const toast = document.createElement('div');
toast.textContent = message;
toast.style.cssText = `
position: fixed;
top: 24px;
left: 50%;
transform: translateX(-50%);
background-color: #10b981;
color: #fff;
padding: 6px 16px;
border-radius: 6px;
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.12);
font-size: 13px;
z-index: 100000;
pointer-events: none;
opacity: 0;
transition: opacity 0.2s ease;
`;
document.body.appendChild(toast);
requestAnimationFrame(() => {
toast.style.opacity = '1';
});
setTimeout(() => {
toast.style.opacity = '0';
setTimeout(() => toast.remove(), 200);
}, 1500);
}
// HTTP 兼容版剪贴板复制
function copyText(text) {
const ta = document.createElement('textarea');
ta.value = text;
ta.setAttribute('readonly', '');
ta.style.cssText = 'position:fixed;left:-9999px;top:-9999px;opacity:0;';
document.body.appendChild(ta);
ta.select();
let ok = false;
try {
ok = document.execCommand('copy');
} catch (_) {}
document.body.removeChild(ta);
return ok;
}
// 构建单个条目 DOM
function buildRowElement(text, title) {
const row = document.createElement('div');
row.style.cssText = `
display: flex;
align-items: center;
justify-content: space-between;
padding: 5px 8px;
background: #f8fafc;
border: 1px solid #f1f5f9;
border-radius: 5px;
gap: 8px;
`;
const label = document.createElement('span');
label.textContent = title;
label.style.cssText = `
font-size: 12px;
color: #334155;
font-weight: 500;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
flex: 1;
`;
label.title = title;
const btn = document.createElement('button');
btn.textContent = '复制';
btn.style.cssText = `
border: none;
background-color: #3b82f6;
color: #fff;
padding: 3px 8px;
font-size: 12px;
border-radius: 4px;
cursor: pointer;
flex-shrink: 0;
`;
btn.onclick = () => {
if (copyText(text)) {
showToast(`已复制:${title}`);
} else {
alert('复制失败,请重试');
}
};
row.appendChild(label);
row.appendChild(btn);
return row;
}
// 题目页提取
function handleProblemPage() {
if (!window.UiContextNew) return;
try {
const outer = JSON.parse(window.UiContextNew);
const zhText = JSON.parse(outer.pdoc.content).zh;
const panel = getOrCreatePanel();
panel.appendChild(buildRowElement(zhText, '中文题面 (zh)'));
} catch (err) {
console.error('解析UiContextNew失败', err);
}
}
// 题目页提取2
function handleProblemPage2() {
if (!UiContext) return;
try {
const pdocContent = JSON.parse(UiContext.pdoc.content);
const zhText = pdocContent.zh;
const panel = getOrCreatePanel();
panel.appendChild(buildRowElement(zhText, '中文题面 (zh)'));
} catch (err) {
console.error('解析UiContextNew失败', err);
}
}
// 题解页并发提取
async function handleSolutionPage() {
const ele = document.getElementsByClassName('typo richmedia collapsible-content');
const elements = Array.from(ele);
if (!elements.length) return;
// 并发请求所有 raw,避免串行网络等待
const fetchTasks = elements.map(async (el, idx) => {
const rawUrl = el.dataset.rawUrl;
if (!rawUrl) return null;
try {
const resp = await fetch(location.origin + rawUrl);
if (!resp.ok) return null;
const text = await resp.text();
return { title: `题解 Raw #${idx + 1}`, text };
} catch (_) {
return null;
}
});
const results = await Promise.all(fetchTasks);
const validResults = results.filter(Boolean);
if (!validResults.length) return;
// 利用 DocumentFragment 单次更新 DOM,消除多次重排
const frag = document.createDocumentFragment();
validResults.forEach(item => {
frag.appendChild(buildRowElement(item.text, item.title));
});
const panel = getOrCreatePanel();
panel.appendChild(frag);
}
// 题解页并发提取2
async function handleSolutionPage2() {
const ele = document.getElementsByClassName('typo');
const elements = Array.from(ele);
elements.pop();
if (!elements.length) return;
// 并发请求所有 raw,避免串行网络等待
const fetchTasks = elements.map(async (el, idx) => {
const rawUrl = el.dataset.rawUrl;
if (!rawUrl) return null;
try {
const resp = await fetch(location.origin + rawUrl);
if (!resp.ok) return null;
const text = await resp.text();
return { title: `题解 Raw #${idx + 1}`, text };
} catch (_) {
return null;
}
});
const results = await Promise.all(fetchTasks);
const validResults = results.filter(Boolean);
if (!validResults.length) return;
// 利用 DocumentFragment 单次更新 DOM,消除多次重排
const frag = document.createDocumentFragment();
validResults.forEach(item => {
frag.appendChild(buildRowElement(item.text, item.title));
});
const panel = getOrCreatePanel();
panel.appendChild(frag);
}
// 空闲调度执行
function run() {
const schedule = window.requestIdleCallback || ((cb) => setTimeout(cb, 100));
schedule(() => {
const path = location.pathname;
if (/^\/p\/.+\/solution$/.test(path)) {
if(location.host === '192.168.110.20' || location.host === 'superoj.cn') handleSolutionPage2();
else handleSolutionPage();
} else if (/^\/p\//.test(path)) {
if(location.host === '192.168.110.20' || location.host === 'superoj.cn') handleProblemPage2();
else handleProblemPage();
}
});
}
// 只要 DOM 骨架就绪即进入调度队列,无需等待高开销静态资源完全加载
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', run, { once: true });
} else {
run();
}
})();