// ==UserScript== // @name 洛谷讨论区颜色自定义 // @namespace https://www.luogu.com.cn/user/542457 // @description 可以更改源代码,设置洛谷各个讨论区的颜色 // @author cff_0102 // @run-at document-end // @version 0.1.7 // @license MIT // @match https://www.luogu.com.cn/* // @icon https://cdn.luogu.com.cn/upload/usericon/542457.png // @downloadURL none // ==/UserScript== (function() { 'use strict'; function hexToRgb(hex) {hex = hex.replace(/^#/, '');const bigint = parseInt(hex, 16);const r = (bigint >> 16) & 255;const g = (bigint >> 8) & 255;const b = bigint & 255;return { r, g, b };} // 更改颜色 function changeColors(){//↓ 更改这里的七个十六进制颜色即可更改洛谷相应版块的颜色 const newcolor1 = '#34495e'; // “全部板块”的颜色 const newcolor2 = '#8e44ad'; // “站务版”的颜色 const newcolor3 = '#52c41a'; // “题目总版”的颜色 const newcolor4 = '#e74c3c'; // “学术版”的颜色 const newcolor5 = '#3ba4a4'; // “灌水区”的颜色 const newcolor6 = '#f39c11'; // “反馈、申请、工单专版”的颜色 const newcolor7 = '#996600'; // “小黑屋”的颜色 // 更改上面的颜色(并保存更改后的脚本)即可实现更改洛谷对应讨论区的颜色 const elementsWithCustomStyle = document.querySelectorAll('[style*="--forum-color: #f39c11; color: var(--forum-color);"]');// 题目讨论版 elementsWithCustomStyle.forEach(function (element) { element.style.cssText = `--forum-color: ${newcolor3}; color: var(--forum-color);`; }); const forumElement = document.querySelector("section.side"); if(forumElement){ // 获取所属板块的文本内容 const forumTextElement = forumElement.querySelector("a.color-default"); const forumText = forumTextElement.textContent.trim(); const temp = document.querySelector("section.main"); const temp1 = temp.querySelector("div.card.padding-default"); // 根据所属板块的文本内容判断是否需要修改颜色 if (forumText === '站务版') { const newColor = newcolor2; // 设置新的颜色值 const rgbColor = hexToRgb(newColor); // 将十六进制颜色转换为 RGB 形式 // 修改元素的颜色属性 temp1.style.cssText = `border-bottom: 2px solid rgb(${rgbColor.r}, ${rgbColor.g}, ${rgbColor.b});`; }else if (forumText === '题目总版') { const newColor = newcolor3; // 设置新的颜色值 const rgbColor = hexToRgb(newColor); // 将十六进制颜色转换为 RGB 形式 // 修改元素的颜色属性 temp1.style.cssText = `border-bottom: 2px solid rgb(${rgbColor.r}, ${rgbColor.g}, ${rgbColor.b});`; }else if (forumText === '学术版') { const newColor = newcolor4; // 设置新的颜色值 const rgbColor = hexToRgb(newColor); // 将十六进制颜色转换为 RGB 形式 // 修改元素的颜色属性 temp1.style.cssText = `border-bottom: 2px solid rgb(${rgbColor.r}, ${rgbColor.g}, ${rgbColor.b});`; }else if (forumText === '灌水区') { const newColor = newcolor5; // 设置新的颜色值 const rgbColor = hexToRgb(newColor); // 将十六进制颜色转换为 RGB 形式 // 修改元素的颜色属性 temp1.style.cssText = `border-bottom: 2px solid rgb(${rgbColor.r}, ${rgbColor.g}, ${rgbColor.b});`; }else if (forumText === '反馈、申请、工单专版') { const newColor = newcolor6; // 设置新的颜色值 const rgbColor = hexToRgb(newColor); // 将十六进制颜色转换为 RGB 形式 // 修改元素的颜色属性 temp1.style.cssText = `border-bottom: 2px solid rgb(${rgbColor.r}, ${rgbColor.g}, ${rgbColor.b});`; }else if (forumText === '小黑屋') { const newColor = newcolor7; // 设置新的颜色值 const rgbColor = hexToRgb(newColor); // 将十六进制颜色转换为 RGB 形式 // 修改元素的颜色属性 temp1.style.cssText = `border-bottom: 2px solid rgb(${rgbColor.r}, ${rgbColor.g}, ${rgbColor.b});`; }else if (window.location.href.startsWith('https://www.luogu.com.cn/discuss/')){// 在题目详情页中 // console.log(forumText); const newColor = newcolor3; // 设置新的颜色值 const rgbColor = hexToRgb(newColor); // 将十六进制颜色转换为 RGB 形式 // 修改元素的颜色属性 temp1.style.cssText = `border-bottom: 2px solid rgb(${rgbColor.r}, ${rgbColor.g}, ${rgbColor.b});`; }// 如果不这样判断,图床、提交记录等地方都会出现不该有的边框 } const a=document.querySelectorAll("[title=全部板块]"); for(let x of a) x.style.cssText = `--forum-color: ${newcolor1}; color: var(--forum-color);`; //洛谷蓝 const b=document.querySelectorAll("[title=站务版]"); for(let x of b) x.style.cssText = `--forum-color: ${newcolor2}; color: var(--forum-color);`; //紫名紫 const c=document.querySelectorAll("[title=题目总版]"); for(let x of c) x.style.cssText = `--forum-color: ${newcolor3}; color: var(--forum-color);`; // AC 绿 const d=document.querySelectorAll("[title=学术版]"); for(let x of d) x.style.cssText = `--forum-color: ${newcolor4}; color: var(--forum-color);`; //红名红 const e=document.querySelectorAll("[title=灌水区]"); for(let x of e) x.style.cssText = `--forum-color: ${newcolor5}; color: var(--forum-color);`; //cff 青 const f=document.querySelectorAll("[title=反馈、申请、工单专版]"); for(let x of f) x.style.cssText = `--forum-color: ${newcolor6}; color: var(--forum-color);`; //排行橙 const g=document.querySelectorAll("[title=小黑屋]"); for(let x of g){x.style.cssText = `--forum-color: ${newcolor7}; color: var(--forum-color);`; //棕名棕 const svgElement = x.querySelector("svg"); const pathElement = svgElement.querySelector("path"); pathElement.setAttribute("d", "M400 32H48C21.5 32 0 53.5 0 80v352c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48"); //更改小黑屋图标(从四个方块改成一个方块) } } //每隔 500 毫秒执行一次颜色更改函数 setInterval(changeColors, 500); })();