JavaScript// ==UserScript==
// @name 洛谷插件。
// @namespace https://www.luogu.com.cn/article/ehjymnti
// @version 1.5
// @description 干点事情。
// @author Anthony2011sfh
// @match https://www.luogu.com.cn/*
// @match https://www.luogu.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const currentUrl = window.location.href;
const path = window.location.pathname;
const host = window.location.hostname;
if (host === 'www.luogu.com') {
if (path.startsWith('/article/')){
const pt='/a'+path.substr(8,9);
const newUrl = `https://luogu.store${pt}`;
//这里跳转至luogu.store。主要因为luogu.me我的学校机房访问限制导致无法渲染Markdown。如果仍然希望跳转至保存站请自行修改。
window.location.replace(newUrl.substr());
return;
}
if(path.startsWith('/paste/')) {
const pt='/p'+path.substr(8,9);
const newUrl = `https://luogu.store${path}`;
window.location.replace(newUrl);
return;
}
}
if(host == 'www.luogu.com.cn'){
function a(){
var x;
for(x of document.body.getElementsByTagName("a")){
if(x.href.search("luogu.me")!=-1){
x.href=x.href.replace("www.luogu.me","luogu.store").replace("luogu.me","luogu.store").replace("article","a").replace("paste","p");
x.text=x.text.replace("www.luogu.me","luogu.store").replace("luogu.me","luogu.store").replace("article","a").replace("paste","p");
}
if(x.text.search("luogu.me")!=-1){
x.href=x.href.replace("www.luogu.me","luogu.store").replace("luogu.me","luogu.store").replace("article","a").replace("paste","p");
x.text=x.text.replace("www.luogu.me","luogu.store").replace("luogu.me","luogu.store").replace("article","a").replace("paste","p");
}
}}
setInterval(a,100);
if((path.startsWith('/article/') || path.startsWith('/paste/'))) {
const warningTitle = document.querySelector('body > div:nth-child(1) > div > h3');
if (document.title == '安全访问中心 - 洛谷' && warningTitle.textContent.trim() == '即将离开洛谷') {
const urlElement = document.querySelector('#url');
if (urlElement) {
urlElement.textContent = urlElement.textContent
.replace('www.luogu.com/article', 'luogu.store/a')
.replace('www.luogu.com/paste', 'luogu.store/p')
.replace('www.luogu.com.cn/article', 'luogu.store/a')
.replace('www.luogu.com.cn/paste', 'luogu.store/p');
}
//https://www.luogu.com/article/sattz11n
const continueLink = document.querySelector('body > div:nth-child(1) > div > p:nth-child(5) > a');
if (continueLink) {
continueLink.href = continueLink.href
.replace('www.luogu.com/article', 'luogu.store/a')
.replace('www.luogu.com/paste', 'luogu.store/p')
.replace('www.luogu.com.cn/article', 'luogu.store/a')
.replace('www.luogu.com.cn/paste', 'luogu.store/p');
}
}
return;
}
}
//讨论区跳转By Anthony2011sfh。部分内容使用Deepseek。
if(path.startsWith('/discuss/')&&document.title=='Error - 洛谷'){
const pt='/d'+path.substr(8,10);
const newUrl = `https://luogu.store${pt}`;
window.location.replace(newUrl.substr());
return;
}
if (currentUrl.startsWith("https://www.luogu.com.cn/discuss?forum=")&&document.title == 'Error - 洛谷') {
const problemId = currentUrl.substring(39);
const newUrl = "https://www.luogu.com.cn/problem/" + problemId;
const overlay = document.createElement('div');
overlay.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 9999;
display: flex;
justify-content: center;
align-items: center;
`;
const dialog = document.createElement('div');
dialog.style.cssText = `
background: white;
padding: 30px;
border-radius: 12px;
box-shadow: 0 8px 32px rgba(0,0,0,0.3);
max-width: 400px;
text-align: center;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
`;
dialog.innerHTML = `
<h3 style="margin: 0 0 15px 0; color: #333;">跳转提示</h3>
<p style="margin: 0 0 20px 0; color: #666; line-height: 1.5;">
你尚未完成本题,请先做题<br>
<strong>题目ID: ${problemId}</strong>
</p>
<div style="display: flex; gap: 10px; justify-content: center;">
<button id="confirmJump" style="
background: #4CAF50;
color: white;
border: none;
padding: 10px 100px;
border-radius: 6px;
cursor: pointer;
font-size: 14px;
">前往做题</button>
</div>
`;
overlay.appendChild(dialog);
document.body.appendChild(overlay);
document.getElementById('confirmJump').addEventListener('click', function() {
document.body.removeChild(overlay);
window.location.replace(newUrl);
});
document.getElementById('cancelJump').addEventListener('click', function() {
document.body.removeChild(overlay);
});
overlay.addEventListener('click', function(e) {
if (e.target === overlay) {
document.body.removeChild(overlay);
}
});
}
//————————————————————————————————————————————————————————————
//————————————————————————————————————————————————————————————
//—————————————————————————————————————————————————————————————
const featureDict = new Map([
['lgbot1AddProblemsColor', '显示题目颜色']
]);
const problemDifficultyToColor = [[191,191,191],[254,76,97],[243,156,17],[255,193,22],[82,196,26],[52,152,219],[157,61,207],[14,29,105]];
// 添加弹窗样式
function addModalStyles() {
const style = document.createElement('style');
style.textContent = `
.lgbot1-modal.hide {
z-index: -114514;
opacity: 0;
pointer-events: none;
transition: opacity .3s ease, z-index 0s ease .3s;
}
.lgbot1-modal {
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 10000;
overflow: auto;
position: fixed;
transition: opacity .3s ease;
display: flex;
justify-content: center;
align-items: center;
}
.lgbot1-modal > .background {
top: 0;
left: 0;
right: 0;
bottom: 0;
position: absolute;
z-index: -1;
background-color: rgba(0, 0, 0, 0.5);
}
.lgbot1-modal .l-card {
position: relative;
z-index: 1;
background: white;
border-radius: 8px;
box-shadow: 0 4px 16px rgba(0,0,0,0.2);
padding: 24px;
min-width: 560px;
max-width: 600px;
margin: 20px;
animation: modalSlideIn 0.3s ease-out;
}
@keyframes modalSlideIn {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.lgbot1-modal .close {
position: absolute;
right: 16px;
top: 16px;
cursor: pointer;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
color: #999;
transition: color 0.2s;
}
.lgbot1-modal .close:hover {
color: #333;
}
.lgbot1-modal .lfe-h3 {
margin: 0 0 24px 0;
font-size: 20px;
font-weight: 600;
color: #333;
padding-right: 30px;
}
.lgbot1-modal .l-form-layout {
margin-bottom: 20px;
}
.lgbot1-modal .l-form-layout.row {
display: flex;
align-items: flex-start;
}
.lgbot1-modal .l-form-layout.row > span {
width: 120px;
padding-right: 16px;
font-weight: 500;
margin-top: 8px;
color: #555;
font-size: 14px;
}
.lgbot1-modal .l-form-layout.row > div {
flex: 1;
}
.lgbot1-modal #featureSettings label {
display: flex;
align-items: center;
cursor: pointer;
margin-bottom: 12px;
padding: 8px 12px;
border-radius: 6px;
transition: background-color 0.2s;
}
.lgbot1-modal #featureSettings label:hover {
background-color: #f5f5f5;
}
.lgbot1-modal #featureSettings input[type="checkbox"] {
margin-right: 12px;
width: 16px;
height: 16px;
cursor: pointer;
}
.lgbot1-modal .button-group {
display: flex;
gap: 12px;
justify-content: flex-end;
margin-top: 24px;
}
.lgbot1-modal button.lform-size-middle {
padding: 10px 24px;
border: 1px solid #d9d9d9;
background: #fafafa;
border-radius: 6px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
color: #333;
transition: all 0.2s;
min-width: 80px;
}
.lgbot1-modal button.lform-size-middle:hover {
background: #eaeaea;
border-color: #bfbfbf;
}
.lgbot1-modal button.lform-size-middle#saveFeatures {
background: #1890ff;
border-color: #1890ff;
color: white;
}
.lgbot1-modal button.lform-size-middle#saveFeatures:hover {
background: #40a9ff;
border-color: #40a9ff;
}
.lgbot1-save-toast {
position: fixed;
top: 20px;
right: 20px;
z-index: 10001;
background: #f6ffed;
border: 1px solid #b7eb8f;
border-radius: 4px;
padding: 12px 16px;
display: flex;
align-items: center;
gap: 12px;
animation: toastSlideIn 0.3s ease-out;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.lgbot1-save-toast-content {
display: flex;
align-items: center;
gap: 8px;
}
.lgbot1-save-toast-icon {
color: #52c41a;
width: 20px;
height: 20px;
flex-shrink: 0;
}
.lgbot1-save-toast-text {
color: #52c41a;
font-size: 14px;
font-weight: 500;
}
@keyframes toastSlideIn {
from {
opacity: 0;
transform: translateX(100%);
}
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes toastSlideOut {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(100%);
}
}
`;
document.head.appendChild(style);
}
// 显示保存成功提示并刷新
function showSaveToastAndRefresh() {
window.fdsa=document.createElement("div");
window.fdsa.className="swal2-container swal2-top-end";
window.fdsa.style="height:92px;width:360px;margin-left:auto;transition:all .25s linear;opacity:1;";
window.fdsa.innerHTML='<div aria-labelledby="swal2-title" aria-describedby="swal2-html-container" class="swal2-popup swal2-toast swal2-icon-success swal2-show" tabindex="-1" role="alert" aria-live="polite" style="width:360px;display: grid;"><div class="swal2-icon swal2-success swal2-icon-show" style="display:flex;"><div class="swal2-success-circular-line-left" style="background-color: rgb(255, 255, 255);"></div><span class="swal2-success-line-tip"></span> <span class="swal2-success-line-long"></span><div class="swal2-success-ring"></div><div class="swal2-success-fix" style="background-color: rgb(255, 255, 255);"></div><div class="swal2-success-circular-line-right" style="background-color: rgb(255, 255, 255);"></div></div><h2 class="swal2-title" id="swal2-title" style="display: block;">保存成功</h2></div>';
// 添加到页面
document.body.appendChild(window.fdsa);
setTimeout(window.closem,3000);
setTimeout(window.closem2,3250);
location.reload();
}
// 初始化设置
function initializeSettings(){
for(let i of featureDict.keys()){
if(localStorage.getItem(i) === null){
localStorage.setItem(i, 'true');
}
}
}
// 创建弹窗
function createModal() {
// 检查是否已存在弹窗
if (document.querySelector('.lgbot1-modal')) {
return;
}
// 添加样式
addModalStyles();
// 创建弹窗
const modal = document.createElement('div');
modal.className = 'lgbot1-modal hide';
// 弹窗内容
modal.innerHTML = `
<div class="background"></div>
<div class="l-card">
<div style="position: relative;">
<h3 class="lfe-h3">选择插件功能</h3>
<div class="close">
<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
<path d="M12.72 3.28a.75.75 0 10-1.06-1.06L8 6.94 4.34 3.28a.75.75 0 10-1.06 1.06L6.94 8l-3.66 3.66a.75.75 0 101.06 1.06L8 9.06l3.66 3.66a.75.75 0 101.06-1.06L9.06 8l3.66-3.66z"/>
</svg>
</div>
</div>
<div>
<form class="modal">
<div class="l-form-layout row">
<div>
<div id="featureSettings"></div>
</div>
</div>
<div class="button-group">
<button class="lform-size-middle" type="button" id="saveFeatures">保存</button>
</div>
</form>
</div>
</div>
`;
// 添加弹窗到页面
document.body.appendChild(modal);
// 绑定事件
bindModalEvents();
}
// 绑定弹窗事件
function bindModalEvents() {
const modal = document.querySelector('.lgbot1-modal');
if (!modal) return;
const closeBtn = modal.querySelector('.close');
const background = modal.querySelector('.background');
const selectAllBtn = modal.querySelector('#selectAll');
const saveBtn = modal.querySelector('#saveFeatures');
// 关闭弹窗函数
function closeModal() {
modal.classList.add('hide');
}
// 绑定关闭事件
if (closeBtn) closeBtn.onclick = closeModal;
if (background) background.onclick = closeModal;
// 绑定保存按钮
if (saveBtn) {
saveBtn.onclick = function() {
// 保存设置
const checkboxes = modal.querySelectorAll('#featureSettings input[type="checkbox"]');
checkboxes.forEach(cb => {
localStorage.setItem(cb.id, String(cb.checked));
});
// 关闭弹窗
closeModal();
// 显示保存成功提示并刷新
showSaveToastAndRefresh();
};
}
}
// 生成功能选项
function renderFeatureSettings() {
const modal = document.querySelector('.lgbot1-modal');
if (!modal) return;
const featureSettings = modal.querySelector('#featureSettings');
if (!featureSettings) return;
featureSettings.innerHTML = '';
for(let [key, description] of featureDict){
const div = document.createElement('div');
const label = document.createElement('label');
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = key;
checkbox.checked = localStorage.getItem(key) === 'true';
label.appendChild(checkbox);
label.appendChild(document.createTextNode(description));
div.appendChild(label);
featureSettings.appendChild(div);
}
}
// 显示弹窗
function showModal() {
const modal = document.querySelector('.lgbot1-modal');
if (!modal) return;
modal.classList.remove('hide');
renderFeatureSettings();
}
const problemToColorMap = new Map();
class FetchRateLimiter{
constructor(limit) { // limit 以毫秒为单位,表示相邻两次 fetch 操作间的最小间隔
this.limit = limit;
this.queue = [];
this.queuePrior = [];
this.active = false;
this.requestCache = new Map();
}
process() {
this.active = 1;
let resolve, reject, url;
if(this.queuePrior.length > 0){
({resolve, reject, url} = this.queuePrior.shift());
}
else if(this.queue.length > 0){
({resolve, reject, url} = this.queue.shift());
}
else{
this.active = 0;
return;
}
fetch(url)
.then(resolve)
.catch(reject);
setTimeout(this.process.bind(this), this.limit);
}
push(url, prior) {
if(this.requestCache.has(url)) return this.requestCache.get(url);
const request = new Promise((resolve, reject) => {
if(prior) this.queuePrior.push({url, resolve, reject});
else this.queue.push({url, resolve, reject});
if(!this.active) this.process();
})
.then((response) => {
return response.text();
});
this.requestCache.set(url,request);
return request;
}
}
const limiter = new FetchRateLimiter(300);
async function getProblemColor(problemid, prior=false){
if(window.location.href.startsWith('https://www.luogu.com.cn/record/list')){
const resultList = _feInstance.currentData.records.result;
if(!resultList.lgbot1Visited){
for(const item of resultList){
problemToColorMap.set(item.problem.pid,`rgb(${problemDifficultyToColor[item.problem.difficulty].join(',')})`);
}
resultList.lgbot1Visited = true;
}
}
if(/^https:\/\/www\.luogu\.com\.cn\/user\/\d+#practice$/.test(window.location.href)) {
let problemList = _feInstance.currentData.submittedProblems;
if(!problemList.lgbot1Visited){
for(const item of problemList){
problemToColorMap.set(item.pid,`rgb(${problemDifficultyToColor[item.difficulty].join(',')})`);
}
problemList.lgbot1Visited = true;
}
problemList = _feInstance.currentData.passedProblems;
if(!problemList.lgbot1Visited){
for(const item of problemList){
problemToColorMap.set(item.pid,`rgb(${problemDifficultyToColor[item.difficulty].join(',')})`);
}
problemList.lgbot1Visited = true;
}
}
if(problemToColorMap.has(problemid)) return problemToColorMap.get(problemid);
const url = '/problem/'+problemid;
let data;
try{
data = await limiter.push(url, prior);
} catch (error) {
console.error('Error fetching user data:', error);
console.log(problemid);
return;
}
const parser = new DOMParser();
const doc = parser.parseFromString(data, 'text/html');
let scriptText = '';
for(const i of doc.querySelectorAll("script")){
scriptText += i.textContent;
}
scriptText = scriptText.match(/"difficulty":\d/)[0];
debugger;
if(!scriptText) return;
const problemDifficulty = Number(scriptText[scriptText.length-1]);
problemToColorMap.set(problemid,`rgb(${problemDifficultyToColor[problemDifficulty].join(',')})`);
return problemToColorMap.get(problemid);
}
function isProblemId(problemid){
if(problemid.startsWith('AT_')) return true;
if(!/[a-zA-Z]/.test(problemid)) return false;
if(!/[0-9]/.test(problemid)) return false;
return true;
}
async function addProblemColor(item){
let problemid = item.href.split('/').pop();
let prior = false;
if(problemid.includes('?forum=')){
problemid = problemid.split('=').pop();
prior = true;
}
problemid = problemid.split('?')[0];
problemid = problemid.split('=').pop();
if(item.matches('a[data-v-bade3303][data-v-4842157a]')){
if(problemid === "javascript:void 0"){
problemid = item.innerText.split(' ')[0];
}
}
if(!isProblemId(problemid)) return;
if(item.innerText.startsWith(problemid)){
const spanItem = item.children[0];
if(spanItem && spanItem.matches('span.pid') && spanItem.innerText === problemid){
const color = await getProblemColor(problemid, prior);
spanItem.style.color = color;
spanItem.style.fontWeight = 'bold';
}
else{
const color = await getProblemColor(problemid, prior);
const content = item.innerHTML;
item.innerHTML = content.replace(problemid,`<b style="color: ${color};">${problemid}</b>`);
}
}
}
async function addProblemsColor(){
const observer = new MutationObserver(async (mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
for (const addedNode of mutation.addedNodes) {
if (addedNode.nodeType === Node.ELEMENT_NODE) {
const newItems = addedNode.querySelectorAll('a[href]');
if(addedNode.matches('a[href]')) addProblemColor(addedNode);
for (const newItem of newItems) {
addProblemColor(newItem);
}
}
}
}
else if(mutation.type === 'characterData') {
if(mutation.target.parentElement.matches('span.pid')){
mutation.target.parentElement.style.color = await getProblemColor(mutation.target.textContent);
mutation.target.parentElement.style.fontWeight = 'bold';
}
}
}
});
observer.observe(document, {
childList: true,
subtree: true,
characterData: true,
});
const nodelist = document.querySelectorAll('a[href]');
for(const i of nodelist){
addProblemColor(i);
}
}
// 初始化
initializeSettings();
createModal();
// 页面加载后初始化
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function() {
// 启用功能
addProblemsColor();
}, 1000);
});
} else {
// DOMContentLoaded 已经触发
setTimeout(function() {
// 启用功能
addProblemsColor();
}, 1000);
}
})();