版本比较
洛谷题目难度自定义脚本
标题
增加 1 行删除 1 行
11洛谷题目难度自定义(稳定版)洛谷题目难度自定义脚本正文
增加 1497 行删除 229 行
123456789101112131415161718192032122567232425262728294 行未改动34353637202122232425262728293839404142434445464748493233505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719821 行未改动22022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525665257258259260261262263264265266757677782672682692702712728127327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735891359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482964834844854864874881024894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615621085635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056061131146071161171181196086096106116126136146156161271281291301311321336176186196206216226236246256261411421436276286296306311451461471481491501516326336346356366371531541551566386396406411581596426436446456461611621631646476486496501661671681691706516526536546556566576586596606616621751761771786636646656666676686696706716726731801816746756766776786796806816826836846856866876881831841856896906916926936946951881896966976981921931946997007017027031961971981997047057067077087092022032042052062072082092107107117127137147152127162142152162172182192202212222237177187197207212297227237247257267277287297307317327337347357367377387397407417427437447457467477482367497507517527537547557562392407577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987992478008018028032502512522532542558048058068078088098108118128138148158168178188198208218228238248258268278288298308318322652662672682698338348358368378388398408418422712722738438448458468478488498508518528538548558568572768588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898902788918928938948958968972828988992852869009019029039049056 行未改动91291391491530130230330430591691791891992092130730892292392492592692792892993093193293393493593631131231331431593793893994094194231731831932032132294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999032532632732832933033199199299399499599633599799833799910003391001100234134234334410031004100510061007100810091010101110123463473481013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079350351352353108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118835735835936036111891190119111921193119411951196119711981199120012011202120312041205120636712073693703713721208120912101211121212131214121512161217376121812191220378379380122112221223122412253823831226122712283853863871229123012311232123312341235123612371238123912401241124212431244124539139239339439539612461247124812491250398399400401125112521253125412551256125712581259126012611262126340340440540640740812641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413754114124134141376137713781379138041641713811382138313841385138642042142242342442542642713871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214834314324331484148514861487148814894364374381490149114921493149414951496442443444149714981499150015014461502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634449450163516361637163816394521640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679454455168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739我看了许多优化,可以让洛谷更美丽,比如氧洛谷,或是[洛谷提交优化](https://www.luogu.com.cn/article/xscqjzbk),对于我这个喜爱后室的人来说,这些可以帮我体验后室风格的洛谷。我用氧洛谷将背景改为了后室层级 level 0,提交记录改成了别的层级,让洛谷更具有后室味。
但,我总觉得还是少了点什么。
可到底少了什么呢?
我想啊想,终于,我想到了,对啊,后室那么多个层级是有生存等级的,洛谷的这些难度显然对不上,没感觉,所以,我决定,在 Deepseek 的帮助下,以及其他机房成员的辅助下,我们成功制造出了一个难度主题管理器脚本,用法还是直接放入篡改猴。
脚本启动后,我们要刷新页面,接着,右下角会出现几个选项,点击管理,即可选择难度的主题,很好操作。
同时,我们不只搞了后室,我们还搞了 SCP,对于 SCP 了解较深的可以使用,否则我怕你看不懂难易程度,当然,你也可以现场记忆下来。
同时,大家也可以自定义主题,选择颜色,文字或者可以放上图片。让洛谷变得更美好
不过脚本有个缺陷,每到一个新的页面,我们都需要刷新,这是一个小缺点,不过习惯就好。
欢迎大家尝试同时提出更改意见,希望这个脚本可以发展的越来越好。
```// ==UserScript==// @name 洛谷题目难度自定义(稳定版)// @name 洛谷难度主题管理器 v10.3// @namespace http://tampermonkey.net/// @version 8.2// @description 自定义洛谷题目难度图标,稳定可靠// @author You// @version 10.3// @description 自定义洛谷题目难度图标,支持多主题创建、切换和保存,新增后室和SCP主题// @author 鸡腿,PCY和祥子// @match https://www.luogu.com.cn/*// @match https://luogu.com.cn/*// @grant GM_getValue// @grant GM_setValue4 行未改动
(function() { 'use strict';
// 默认配置 const defaultConfig = { '入门': { type: 'text', value: '🔰入门', color: '#3498db' }, '普及-': { type: 'text', value: '📘普及-', color: '#2ecc71' }, '普及/提高-': { type: 'text', value: '📗普及/提高-', color: '#f1c40f' }, '普及+/提高': { type: 'text', value: '📒普及+/提高', color: '#f39c12' }, '提高+/省选-': { type: 'text', value: '📙提高+/省选-', color: '#e67e22' }, '省选/NOI-': { type: 'text', value: '📕省选/NOI-', color: '#e74c3c' }, 'NOI/NOI+/CTSC': { type: 'text', value: '💎NOI/NOI+/CTSC', color: '#9b59b6' }, '暂无评定': { type: 'text', value: '❓暂无评定', color: '#95a5a6' } // 后室主题图片URL const BACKROOMS_IMAGES = { '暂无评定': 'https://cdn.luogu.com.cn/upload/image_hosting/k83scvdj.png', '入门': 'https://cdn.luogu.com.cn/upload/image_hosting/3pkufhwg.png', '普及-': 'https://cdn.luogu.com.cn/upload/image_hosting/bnpb43dd.png', '普及/提高-': 'https://cdn.luogu.com.cn/upload/image_hosting/dkj0la0b.png', '普及+/提高': 'https://cdn.luogu.com.cn/upload/image_hosting/w6fakokc.png', '提高+/省选-': 'https://cdn.luogu.com.cn/upload/image_hosting/95438ck0.png', '省选/NOI-': 'https://cdn.luogu.com.cn/upload/image_hosting/0pzb08o8.png', 'NOI/NOI+/CTSC': 'https://cdn.luogu.com.cn/upload/image_hosting/i21hupri.png' };
// 加载配置 let config = GM_getValue('luogu_difficulty_config', defaultConfig); // 默认主题配置 const DEFAULT_THEMES = { '经典图标主题': { name: '经典图标主题', created: new Date().toISOString(), updated: new Date().toISOString(), config: { '入门': { type: 'text', value: '🔰入门', color: '#3498db' }, '普及-': { type: 'text', value: '📘普及-', color: '#2ecc71' }, '普及/提高-': { type: 'text', value: '📗普及/提高-', color: '#f1c40f' }, '普及+/提高': { type: 'text', value: '📒普及+/提高', color: '#f39c12' }, '提高+/省选-': { type: 'text', value: '📙提高+/省选-', color: '#e67e22' }, '省选/NOI-': { type: 'text', value: '📕省选/NOI-', color: '#e74c3c' }, 'NOI/NOI+/CTSC': { type: 'text', value: '💎NOI/NOI+/CTSC', color: '#9b59b6' }, '暂无评定': { type: 'text', value: '❓暂无评定', color: '#95a5a6' } } }, '简约文字主题': { name: '简约文字主题', created: new Date().toISOString(), updated: new Date().toISOString(), config: { '入门': { type: 'text', value: '入门', color: '#3498db' }, '普及-': { type: 'text', value: '普及-', color: '#2ecc71' }, '普及/提高-': { type: 'text', value: '普及/提高-', color: '#f1c40f' }, '普及+/提高': { type: 'text', value: '普及+/提高', color: '#f39c12' }, '提高+/省选-': { type: 'text', value: '提高+/省选-', color: '#e67e22' }, '省选/NOI-': { type: 'text', value: '省选/NOI-', color: '#e74c3c' }, 'NOI/NOI+/CTSC': { type: 'text', value: 'NOI/NOI+/CTSC', color: '#9b59b6' }, '暂无评定': { type: 'text', value: '暂无评定', color: '#95a5a6' } } }, 'Emoji主题': { name: 'Emoji主题', created: new Date().toISOString(), updated: new Date().toISOString(), config: { '入门': { type: 'text', value: '🐣入门', color: '#3498db' }, '普及-': { type: 'text', value: '📚普及-', color: '#2ecc71' }, '普及/提高-': { type: 'text', value: '📖普及/提高-', color: '#f1c40f' }, '普及+/提高': { type: 'text', value: '📝普及+/提高', color: '#f39c12' }, '提高+/省选-': { type: 'text', value: '🏆提高+/省选-', color: '#e67e22' }, '省选/NOI-': { type: 'text', value: '🎯省选/NOI-', color: '#e74c3c' }, 'NOI/NOI+/CTSC': { type: 'text', value: '👑NOI/NOI+/CTSC', color: '#9b59b6' }, '暂无评定': { type: 'text', value: '❓暂无评定', color: '#95a5a6' } } }, '后室主题': { name: '后室主题', created: new Date().toISOString(), updated: new Date().toISOString(), config: { '入门': { type: 'image', value: BACKROOMS_IMAGES['入门'], color: '#3498db' }, '普及-': { type: 'image', value: BACKROOMS_IMAGES['普及-'], color: '#2ecc71' }, '普及/提高-': { type: 'image', value: BACKROOMS_IMAGES['普及/提高-'], color: '#f1c40f' }, '普及+/提高': { type: 'image', value: BACKROOMS_IMAGES['普及+/提高'], color: '#f39c12' }, '提高+/省选-': { type: 'image', value: BACKROOMS_IMAGES['提高+/省选-'], color: '#e67e22' }, '省选/NOI-': { type: 'image', value: BACKROOMS_IMAGES['省选/NOI-'], color: '#e74c3c' }, 'NOI/NOI+/CTSC': { type: 'image', value: BACKROOMS_IMAGES['NOI/NOI+/CTSC'], color: '#9b59b6' }, '暂无评定': { type: 'image', value: BACKROOMS_IMAGES['暂无评定'], color: '#95a5a6' } } }, 'SCP主题': { name: 'SCP主题', created: new Date().toISOString(), updated: new Date().toISOString(), config: { '入门': { type: 'text', value: 'Explained', color: '#ffffff' }, '普及-': { type: 'text', value: 'Safe', color: '#2ecc71' }, '普及/提高-': { type: 'text', value: 'Euclid', color: '#f1c40f' }, '普及+/提高': { type: 'text', value: 'Keter', color: '#e74c3c' }, '提高+/省选-': { type: 'text', value: 'Thaumiel', color: '#9b59b6' }, '省选/NOI-': { type: 'text', value: 'Archon', color: '#3498db' }, 'NOI/NOI+/CTSC': { type: 'text', value: 'Apollyon', color: '#000000' }, '暂无评定': { type: 'text', value: 'Pending', color: '#95a5a6' } } } };
// 难度等级列表 const DIFFICULTIES = [ '入门', '普及-', '普及/提高-', '普及+/提高', '提高+/省选-', '省选/NOI-', 'NOI/NOI+/CTSC', '暂无评定' ];
// 存储结构 let storage = { themes: {}, currentTheme: '', settings: { autoRefresh: true, showRefreshButton: true } };
// 加载存储数据 function loadStorage() { try { const saved = GM_getValue('luogu_difficulty_storage'); if (saved && saved.themes && Object.keys(saved.themes).length > 0) { storage = saved; console.log('已加载主题存储数据'); return true; } } catch (e) { console.error('加载存储数据失败:', e); }
// 初始化为默认主题 storage.themes = JSON.parse(JSON.stringify(DEFAULT_THEMES)); storage.currentTheme = '经典图标主题'; saveStorage(); console.log('使用默认主题数据'); return false; }
// 保存存储数据 function saveStorage() { try { GM_setValue('luogu_difficulty_storage', storage); console.log('存储数据已保存'); return true; } catch (e) { console.error('保存存储数据失败:', e); return false; } }
// 获取当前主题配置 function getCurrentConfig() { const theme = storage.themes[storage.currentTheme]; return theme ? theme.config : DEFAULT_THEMES['经典图标主题'].config; }
// 更新当前主题配置 function updateCurrentConfig(config) { if (storage.themes[storage.currentTheme]) { storage.themes[storage.currentTheme].config = config; storage.themes[storage.currentTheme].updated = new Date().toISOString(); saveStorage(); return true; } return false; }
// 添加样式 GM_addStyle(` /* 自定义难度标签样式 */ .luogu-custom-difficulty {21 行未改动 margin: 0 !important; display: inline-block !important; }
/* 后室主题图片放大样式 - 用于题目背景之上 */ .luogu-backrooms-enlarged { max-width: 200px !important; max-height: 200px !important; width: auto !important; height: auto !important; display: block !important; margin: 10px auto !important; border: 2px solid #666 !important; border-radius: 8px !important; box-shadow: 0 4px 8px rgba(0,0,0,0.3) !important; }
/* 后室主题标题样式 */ .luogu-backrooms-title { font-size: 20px !important; font-weight: bold !important; text-align: center !important; color: #333 !important; margin: 15px 0 10px 0 !important; padding-bottom: 8px !important; border-bottom: 2px solid #3498db !important; }
/* SCP主题特殊文字样式 */ .luogu-scp-text { font-family: 'Courier New', monospace !important; font-weight: bold !important; letter-spacing: 1px !important; text-transform: uppercase !important; }
/* 设置面板样式 */ #luogu-settings-panel { #luogu-main-panel { position: fixed !important; top: 50% !important; left: 50% !important; transform: translate(-50%, -50%) !important; background: white !important; padding: 20px !important; border-radius: 8px !important; box-shadow: 0 0 20px rgba(0,0,0,0.3) !important; z-index: 10000 !important; width: 500px !important; max-width: 90vw !important; max-height: 80vh !important; overflow-y: auto !important; width: 800px !important; max-width: 95vw !important; max-height: 90vh !important; overflow: hidden !important; }
.luogu-settings-overlay { .luogu-panel-overlay { position: fixed !important; top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important; background: rgba(0,0,0,0.5) !important; z-index: 9999 !important; }
/* 选项卡样式 */ .luogu-tabs { display: flex !important; border-bottom: 2px solid #eee !important; margin-bottom: 20px !important; }
.luogu-tab { padding: 10px 20px !important; cursor: pointer !important; border: none !important; background: none !important; font-size: 14px !important; color: #666 !important; border-bottom: 2px solid transparent !important; transition: all 0.3s !important; margin-right: 5px !important; }
.luogu-tab:hover { color: #2196F3 !important; }
.luogu-tab.active { color: #2196F3 !important; border-bottom: 2px solid #2196F3 !important; font-weight: bold !important; }
/* 主题管理样式 */ .luogu-themes-list { max-height: 300px !important; overflow-y: auto !important; margin-bottom: 20px !important; border: 1px solid #eee !important; border-radius: 5px !important; }
.luogu-theme-item { padding: 12px 15px !important; border-bottom: 1px solid #f0f0f0 !important; display: flex !important; justify-content: space-between !important; align-items: center !important; transition: background 0.3s !important; }
.luogu-theme-item:hover { background: #f9f9f9 !important; }
.luogu-theme-item.current { background: #e3f2fd !important; border-left: 4px solid #2196F3 !important; }
.luogu-theme-info { flex: 1 !important; }
.luogu-theme-name { font-weight: bold !important; color: #333 !important; margin-bottom: 3px !important; }
.luogu-theme-meta { font-size: 12px !important; color: #888 !important; }
.luogu-theme-actions { display: flex !important; gap: 5px !important; }
.luogu-refresh-btn { .luogu-action-btn { padding: 5px 10px !important; border: none !important; border-radius: 3px !important; cursor: pointer !important; font-size: 12px !important; }
.luogu-action-btn.apply { background: #4CAF50 !important; color: white !important; }
.luogu-action-btn.delete { background: #f44336 !important; color: white !important; }
.luogu-action-btn.rename { background: #ff9800 !important; color: white !important; }
/* 主题编辑样式 */ .luogu-difficulty-editor { display: grid !important; grid-template-columns: repeat(auto-fill, minmax(350px, 1fr)) !important; gap: 15px !important; max-height: 400px !important; overflow-y: auto !important; padding: 10px !important; }
.luogu-difficulty-item { padding: 15px !important; border: 1px solid #ddd !important; border-radius: 5px !important; background: #f9f9f9 !important; }
.luogu-difficulty-header { font-weight: bold !important; color: #333 !important; margin-bottom: 10px !important; padding-bottom: 5px !important; border-bottom: 1px solid #eee !important; }
.luogu-option-group { display: flex !important; align-items: center !important; gap: 10px !important; margin-bottom: 8px !important; flex-wrap: wrap !important; }
.luogu-option-label { min-width: 60px !important; font-size: 13px !important; }
.luogu-option-input { flex: 1 !important; min-width: 200px !important; padding: 6px !important; border: 1px solid #ddd !important; border-radius: 3px !important; }
.luogu-color-input { width: 50px !important; height: 30px !important; cursor: pointer !important; }
/* 按钮样式 */ .luogu-panel-btn { padding: 8px 15px !important; border: none !important; border-radius: 4px !important; cursor: pointer !important; font-weight: bold !important; margin-left: 5px !important; }
.luogu-panel-btn.primary { background: #2196F3 !important; color: white !important; }
.luogu-panel-btn.success { background: #4CAF50 !important; color: white !important; }
.luogu-panel-btn.danger { background: #f44336 !important; color: white !important; }
.luogu-panel-btn.secondary { background: #9e9e9e !important; color: white !important; }
.luogu-panel-footer { margin-top: 20px !important; padding-top: 15px !important; border-top: 1px solid #eee !important; text-align: right !important; }
/* 快速切换菜单 */ .luogu-quick-menu { position: fixed !important; bottom: 15px !important; right: 15px !important; display: flex !important; gap: 5px !important; z-index: 1000 !important; }
.luogu-quick-btn { padding: 6px 12px !important; background: #4CAF50 !important; background: #2196F3 !important; color: white !important; border: none !important; border-radius: 4px !important; cursor: pointer !important; font-size: 12px !important; z-index: 1000 !important; font-weight: bold !important; }
.luogu-quick-btn.theme-switch { background: #9c27b0 !important; }
.luogu-quick-btn.reprocess { background: #ff9800 !important; }
.luogu-quick-btn:hover { opacity: 0.9 !important; }
/* 模态框样式 */ .luogu-modal { position: fixed !important; top: 50% !important; left: 50% !important; transform: translate(-50%, -50%) !important; background: white !important; padding: 20px !important; border-radius: 8px !important; box-shadow: 0 0 20px rgba(0,0,0,0.3) !important; z-index: 10001 !important; min-width: 300px !important; }
.luogu-modal-title { font-size: 18px !important; font-weight: bold !important; margin-bottom: 15px !important; color: #333 !important; }
.luogu-modal-input { width: 100% !important; padding: 8px !important; margin-bottom: 15px !important; border: 1px solid #ddd !important; border-radius: 4px !important; box-sizing: border-box !important; }
.luogu-modal-footer { text-align: right !important; margin-top: 20px !important; }
/* 全屏刷新遮罩 */ .luogu-refresh-overlay { position: fixed !important; top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important; background: rgba(0, 0, 0, 0.8) !important; z-index: 100000 !important; display: flex !important; justify-content: center !important; align-items: center !important; color: white !important; font-size: 24px !important; flex-direction: column !important; }
.luogu-refresh-overlay span { margin-bottom: 20px !important; text-align: center !important; } `);
// 注册菜单命令 GM_registerMenuCommand('自定义难度显示', showSettings); GM_registerMenuCommand('主题管理器', showThemeManager); GM_registerMenuCommand('快速切换主题', showQuickSwitch); GM_registerMenuCommand('编辑当前主题', showThemeEditor); GM_registerMenuCommand('重新处理难度标签', () => { processAllDifficulties(); alert('已重新处理当前页面的难度标签'); }); GM_registerMenuCommand('刷新页面', fullRefreshPage);
// 所有可能的颜色类名 const COLOR_CLASSES = [ 'lcolor--grey-3', // 暂无评定 'lcolor--pink-3', // 入门 'lcolor--orange-3', // 普及- 'lcolor--gold-3', // 普及/提高- 'lcolor--green-3', // 普及+/提高 'lcolor--blue-3', // 提高+/省选- 'lcolor--purple-3', // 省选/NOI- 'lcolor--lapis-4', // NOI/NOI+/CTSC 'lcolor--red-3', 'lcolor--yellow-3', 'lcolor--black-3' ];
// 获取对比色 function getContrastColor(hexColor) { if (!hexColor) return '#ffffff'; hexColor = hexColor.replace('#', ''); if (hexColor.length === 3) { hexColor = hexColor.split('').map(c => c + c).join(''); } try { const r = parseInt(hexColor.substr(0, 2), 16); const g = parseInt(hexColor.substr(2, 2), 16); const b = parseInt(hexColor.substr(4, 2), 16); return (r * 299 + g * 587 + b * 114) / 1000 > 128 ? '#000000' : '#ffffff'; } catch (e) { return '#ffffff'; } }
// 标准化难度文本 function normalizeDifficultyText(text) { if (!text) return '';
// 移除空白字符 text = text.replace(/\s+/g, '');
// 将Unicode减号转换为普通减号 text = text.replace(/[\u2212]/g, '-');
text = text.replace(/[\u2212\u2013\u2014]/g, '-'); text = text.replace(/[\uFF0D\uFE63\u2212]/g, '-'); return text; }
// 获取难度等级 function getDifficultyLevel(text) { const normalized = normalizeDifficultyText(text);
const difficulties = [ '入门', '普及-', '普及/提高-', '普及+/提高', '提高+/省选-', '省选/NOI-', 'NOI/NOI+/CTSC', '暂无评定' ];
for (const diff of difficulties) { if (normalized === normalizeDifficultyText(diff)) { for (const diff of DIFFICULTIES) { const normalizedDiff = normalizeDifficultyText(diff); if (normalized === normalizedDiff) { return diff; } }
return null; }
// 查找难度元素 - 简化版 function findDifficultyElements() { const elements = []; // 判断是否为题目页面 function isProblemPage() { return window.location.pathname.includes('/problem/'); }
// 1. 题目列表页的难度标签 const difficultySpans = document.querySelectorAll('.difficulty'); difficultySpans.forEach(el => { if (!el.classList.contains('luogu-processed')) { elements.push(el); } }); // 判断是否为题目背景之上的难度元素 function isProblemHeaderDifficulty(element) { // 检查元素是否在题目标题附近 const problemTitle = document.querySelector('.header .title, .problem-title, h1'); if (!problemTitle) return false;
// 2. 包含难度文本的span const allSpans = document.querySelectorAll('span'); allSpans.forEach(span => { if (span.classList.contains('luogu-processed')) return; // 检查元素是否在标题附近 const rect1 = element.getBoundingClientRect(); const rect2 = problemTitle.getBoundingClientRect();
const text = span.textContent.trim(); const difficulty = getDifficultyLevel(text); // 判断两个元素是否在垂直方向上相近 const verticalDistance = Math.abs(rect1.top - rect2.top); return verticalDistance < 100; // 在100像素范围内视为标题附近的元素 }
if (difficulty) { // 检查是否有背景色或特定类名,避免误匹配 const style = span.getAttribute('style') || ''; const className = span.className || ''; // 查找难度元素 function findDifficultyElements() { const elements = new Set();
if (style.includes('background-color') || className.includes('lcolor--') || span.parentElement?.classList?.contains('difficulty') || span.closest('a[href*="difficulty"]')) { elements.push(span); // 方法1: 专门处理.difficulty-tags结构 const difficultyTags = document.querySelectorAll('.difficulty-tags .row span:not(.problem-count)');
difficultyTags.forEach(el => { if (!el.classList.contains('luogu-processed')) { const text = el.textContent.trim(); if (text && getDifficultyLevel(text)) { elements.add(el); } } });
// 3. 包含难度文本的a标签 const allLinks = document.querySelectorAll('a'); allLinks.forEach(link => { if (link.classList.contains('luogu-processed')) return; // 方法2: 通过颜色类名查找 COLOR_CLASSES.forEach(className => { try { document.querySelectorAll(`.${className}`).forEach(el => { if (!el.classList.contains('luogu-processed')) { elements.add(el); } }); } catch (e) {} });
const text = link.textContent.trim(); const difficulty = getDifficultyLevel(text); // 方法3: 通过常见选择器查找 const selectors = [ '.difficulty', '.tag.difficulty', 'span.difficulty', 'a[href*="difficulty"]', 'span[class*="difficulty"]', '.problem-tags span', '.am-badge' ];
selectors.forEach(selector => { try { document.querySelectorAll(selector).forEach(el => { const text = el.textContent.trim(); if (difficulty && link.href.includes('difficulty')) { elements.push(link); } if (text && getDifficultyLevel(text) && !el.classList.contains('luogu-processed')) { elements.add(el); } }); } catch (e) {} });
// 去重 return [...new Set(elements)]; return Array.from(elements); }
// 获取对比色 function getContrastColor(hexColor) { if (!hexColor) return '#ffffff'; // 替换难度元素 - 增强版,支持后室主题的特殊显示 function replaceDifficultyElement(element) { // 获取难度文本 let difficultyText = element.textContent.trim();
hexColor = hexColor.replace('#', '');
if (hexColor.length === 3) { hexColor = hexColor.split('').map(c => c + c).join(''); // 获取难度等级 const difficulty = getDifficultyLevel(difficultyText); if (!difficulty) { return false; }
try { const r = parseInt(hexColor.substr(0, 2), 16); const g = parseInt(hexColor.substr(2, 2), 16); const b = parseInt(hexColor.substr(4, 2), 16); const brightness = (r * 299 + g * 587 + b * 114) / 1000;
return brightness > 128 ? '#000000' : '#ffffff'; } catch (e) { return '#ffffff'; // 获取当前主题配置 const config = getCurrentConfig(); const configItem = config[difficulty]; if (!configItem) { return false; } }
// 替换难度元素 function replaceDifficultyElement(element) { const text = element.textContent.trim(); const difficulty = getDifficultyLevel(text);
if (!difficulty) return false;
const configItem = config[difficulty]; if (!configItem) return false;
try { // 标记为已处理 element.classList.add('luogu-processed');
// 保存原始内容 element.setAttribute('data-luogu-original', element.innerHTML); const originalHTML = element.innerHTML; const originalStyle = element.getAttribute('style');
element.setAttribute('data-luogu-original', originalHTML); if (originalStyle) { element.setAttribute('data-luogu-original-style', originalStyle); }
// 保存problem-count兄弟元素(如果有) const parent = element.parentElement; let problemCountElement = null; if (parent && parent.classList.contains('row')) { const problemCount = parent.querySelector('.problem-count'); if (problemCount) { problemCountElement = problemCount.cloneNode(true); element.setAttribute('data-luogu-problem-count', problemCount.outerHTML); } }
// 清空内容 element.innerHTML = '';
// 清除样式 const currentStyle = window.getComputedStyle(element); element.style.cssText = '';
// 恢复必要的布局样式 element.style.display = 'inline-block'; element.style.display = currentStyle.display; element.style.verticalAlign = 'middle'; element.style.margin = '0'; element.style.padding = '0'; element.style.border = 'none'; element.style.boxShadow = 'none'; element.style.backgroundColor = 'transparent';
// 创建新内容 if (configItem.type === 'image' && configItem.value) { // 检查是否为后室主题且在题目背景之上 const isBackroomsTheme = storage.currentTheme === '后室主题'; const isProblemHeader = isProblemPage() && isProblemHeaderDifficulty(element);
if (isBackroomsTheme && isProblemHeader) { // 后室主题在题目背景之上的特殊显示 const container = document.createElement('div'); container.style.textAlign = 'center'; container.style.margin = '10px 0';
// 添加标题 const title = document.createElement('div'); title.className = 'luogu-backrooms-title'; title.textContent = difficulty; container.appendChild(title);
// 添加放大图片 const img = document.createElement('img'); img.src = configItem.value || BACKROOMS_IMAGES[difficulty] || ''; img.alt = difficulty; img.title = difficulty; img.className = 'luogu-backrooms-enlarged'; container.appendChild(img);
element.appendChild(container); element.style.display = 'block'; element.style.textAlign = 'center'; } else if (configItem.type === 'image' && configItem.value) { // 图片模式 const img = document.createElement('img'); img.src = configItem.value; img.alt = difficulty; img.title = difficulty; img.className = 'luogu-custom-img'; img.style.display = 'inline-block'; img.style.verticalAlign = 'middle';
// 如果是SCP主题,不应用图片 if (storage.currentTheme === 'SCP主题') { console.log('SCP主题使用文字模式,忽略图片配置'); // 回退到文字模式 createTextElement(element, difficulty, configItem); } else { element.appendChild(img); element.appendChild(img); } } else { // 文字模式 const span = document.createElement('span'); span.textContent = configItem.value || difficulty; span.className = 'luogu-custom-difficulty'; span.style.backgroundColor = configItem.color; span.style.color = getContrastColor(configItem.color); element.appendChild(span); createTextElement(element, difficulty, configItem); }
// 恢复problem-count元素 if (problemCountElement && parent && parent.classList.contains('row')) { const existingProblemCount = parent.querySelector('.problem-count'); if (existingProblemCount) { existingProblemCount.style.display = 'inline'; } else { parent.appendChild(problemCountElement); } }
return true; } catch (error) { console.error('替换难度元素出错:', error);
try { const original = element.getAttribute('data-luogu-original'); if (original) { element.innerHTML = original; } element.classList.remove('luogu-processed'); } catch (e) {}
return false; } }
// 恢复难度元素 function restoreDifficultyElement(element) { const original = element.getAttribute('data-luogu-original'); if (original) { element.innerHTML = original; // 创建文字元素 function createTextElement(element, difficulty, configItem) { const span = document.createElement('span'); span.textContent = configItem.value || difficulty; span.className = 'luogu-custom-difficulty';
// SCP主题特殊样式 if (storage.currentTheme === 'SCP主题') { span.classList.add('luogu-scp-text'); } element.classList.remove('luogu-processed'); element.removeAttribute('data-luogu-original'); element.style.cssText = '';
span.style.backgroundColor = configItem.color; span.style.color = getContrastColor(configItem.color);
// 保持原有的内联样式 if (element.hasAttribute('data-luogu-original-style')) { const originalStyleText = element.getAttribute('data-luogu-original-style'); if (originalStyleText && originalStyleText.includes('border-color')) { span.style.border = '1px solid ' + configItem.color; } }
element.appendChild(span); }
// 主处理函数 // 恢复所有已处理的元素 function restoreAllProcessedElements() { const processedElements = document.querySelectorAll('.luogu-processed');
processedElements.forEach(element => { const original = element.getAttribute('data-luogu-original'); const originalStyle = element.getAttribute('data-luogu-original-style');
if (original) { element.innerHTML = original; }
if (originalStyle) { element.setAttribute('style', originalStyle); }
element.classList.remove('luogu-processed'); element.removeAttribute('data-luogu-original'); element.removeAttribute('data-luogu-original-style');
// 恢复problem-count显示 const parent = element.parentElement; if (parent && parent.classList.contains('row')) { const problemCount = parent.querySelector('.problem-count'); if (problemCount) { problemCount.style.display = 'inline'; } } }); }
// 处理所有难度元素 function processAllDifficulties() { console.log('开始处理难度标签...'); console.log('开始处理难度标签...当前主题:', storage.currentTheme);
// 恢复所有已处理的元素 restoreAllProcessedElements();
// 查找所有难度元素 const elements = findDifficultyElements(); console.log(`找到 ${elements.length} 个难度元素`);
if (elements.length === 0) { // 如果没有找到,稍后再试一次 setTimeout(processAllDifficulties, 1000); console.log('没有找到难度元素,稍后重试'); setTimeout(processAllDifficulties, 2000); return; }
// 处理每个元素6 行未改动
console.log(`成功处理 ${processedCount} 个难度标签`); }
// 强制刷新 function forceRefresh() { // 恢复所有已处理的元素 const processedElements = document.querySelectorAll('.luogu-processed'); processedElements.forEach(restoreDifficultyElement); // 全屏刷新页面 function fullRefreshPage() { // 创建全屏遮罩 const overlay = document.createElement('div'); overlay.className = 'luogu-refresh-overlay';
// 重新处理 setTimeout(processAllDifficulties, 100); const message = document.createElement('span'); message.textContent = '正在应用主题更改,刷新页面...'; overlay.appendChild(message);
document.body.appendChild(overlay);
// 保存当前主题到本地存储 saveStorage();
// 等待一下让用户看到消息 setTimeout(() => { location.reload(); }, 1500); }
// 添加刷新按钮 function addRefreshButton() { // 移除已存在的按钮 const existingBtn = document.querySelector('.luogu-refresh-btn'); if (existingBtn) existingBtn.remove(); // 添加快速切换菜单 function addQuickMenu() { // 移除已存在的菜单 const existingMenu = document.querySelector('.luogu-quick-menu'); if (existingMenu) existingMenu.remove();
// 创建新按钮 const btn = document.createElement('button'); btn.className = 'luogu-refresh-btn'; btn.textContent = '🔄 刷新难度显示'; btn.onclick = forceRefresh; document.body.appendChild(btn); // 创建菜单容器 const menu = document.createElement('div'); menu.className = 'luogu-quick-menu';
// 主题切换按钮 const themeBtn = document.createElement('button'); themeBtn.className = 'luogu-quick-btn theme-switch'; themeBtn.textContent = `主题: ${storage.currentTheme}`; themeBtn.title = '点击切换主题'; themeBtn.addEventListener('click', showQuickSwitch);
// 重新处理按钮 const reprocessBtn = document.createElement('button'); reprocessBtn.className = 'luogu-quick-btn reprocess'; reprocessBtn.textContent = '🔄 重处理'; reprocessBtn.title = '重新处理当前页面的难度标签'; reprocessBtn.addEventListener('click', () => { reprocessBtn.textContent = '处理中...'; reprocessBtn.disabled = true; setTimeout(() => { processAllDifficulties(); reprocessBtn.textContent = '🔄 重处理'; reprocessBtn.disabled = false; }, 300); });
// 刷新页面按钮 const refreshBtn = document.createElement('button'); refreshBtn.className = 'luogu-quick-btn'; refreshBtn.textContent = '♻️ 刷新'; refreshBtn.title = '刷新整个页面'; refreshBtn.addEventListener('click', fullRefreshPage);
// 管理按钮 const manageBtn = document.createElement('button'); manageBtn.className = 'luogu-quick-btn'; manageBtn.textContent = '⚙️ 管理'; manageBtn.title = '打开主题管理器'; manageBtn.addEventListener('click', showThemeManager);
menu.appendChild(themeBtn); menu.appendChild(reprocessBtn); menu.appendChild(refreshBtn); menu.appendChild(manageBtn);
document.body.appendChild(menu); }
// 显示设置面板 function showSettings() { // 移除已存在的面板 const oldOverlay = document.querySelector('.luogu-settings-overlay'); const oldPanel = document.querySelector('#luogu-settings-panel'); if (oldOverlay) oldOverlay.remove(); if (oldPanel) oldPanel.remove(); // 显示主题管理器 function showThemeManager() { removeExistingPanels();
// 创建遮罩层 const overlay = document.createElement('div'); overlay.className = 'luogu-settings-overlay'; overlay.className = 'luogu-panel-overlay';
// 创建面板 // 创建主面板 const panel = document.createElement('div'); panel.id = 'luogu-settings-panel'; panel.id = 'luogu-main-panel';
// 构建设置内容 let html = '<h2 style="margin-top:0">洛谷难度自定义设置</h2>'; html += '<p>为每个难度等级设置自定义显示:</p>'; html += '<div style="max-height:400px;overflow-y:auto">'; // 构建选项卡 let html = ` <div class="luogu-tabs"> <button class="luogu-tab active" data-tab="themes">主题管理</button> <button class="luogu-tab" data-tab="editor">主题编辑</button> <button class="luogu-tab" data-tab="settings">设置</button> </div> <div id="luogu-tab-content" style="min-height: 400px;"></div> `;
const difficulties = Object.keys(defaultConfig); difficulties.forEach(diff => { const configItem = config[diff] || defaultConfig[diff]; panel.innerHTML = html;
// 添加到页面 document.body.appendChild(overlay); document.body.appendChild(panel);
// 初始化选项卡内容 showTabContent('themes');
// 选项卡点击事件 panel.querySelectorAll('.luogu-tab').forEach(tab => { tab.addEventListener('click', function() { panel.querySelectorAll('.luogu-tab').forEach(t => t.classList.remove('active')); this.classList.add('active'); showTabContent(this.getAttribute('data-tab')); }); });
// 点击遮罩层关闭 overlay.addEventListener('click', () => { removeExistingPanels(); }); }
// 显示选项卡内容 function showTabContent(tabName) { const content = document.querySelector('#luogu-tab-content'); if (!content) return;
switch(tabName) { case 'themes': content.innerHTML = getThemesManagementContent(); setupThemesManagementEvents(); break; case 'editor': content.innerHTML = getThemeEditorContent(); setupThemeEditorEvents(); break; case 'settings': content.innerHTML = getSettingsContent(); setupSettingsEvents(); break; } }
// 获取主题管理内容 function getThemesManagementContent() { const themes = storage.themes; const currentTheme = storage.currentTheme;
let html = ` <h3 style="margin-top:0">主题管理</h3> <div style="margin-bottom:15px"> <button id="luogu-new-theme" class="luogu-panel-btn primary">新建主题</button> <button id="luogu-duplicate-theme" class="luogu-panel-btn secondary">复制当前主题</button> <button id="luogu-reprocess-btn" class="luogu-panel-btn success">重新处理标签</button> <button id="luogu-full-refresh" class="luogu-panel-btn danger">刷新页面</button> </div> <div class="luogu-themes-list"> `;
Object.values(themes).forEach(theme => { const isCurrent = theme.name === currentTheme; const createdDate = new Date(theme.created).toLocaleDateString(); const updatedDate = new Date(theme.updated).toLocaleDateString();
html += ` <div style="margin-bottom:10px;padding:10px;border:1px solid #ddd;border-radius:5px"> <div style="font-weight:bold;margin-bottom:5px">${diff}</div> <div> <select style="margin-right:10px" data-diff="${diff}"> <div class="luogu-theme-item ${isCurrent ? 'current' : ''}" data-theme="${theme.name}"> <div class="luogu-theme-info"> <div class="luogu-theme-name">${theme.name}</div> <div class="luogu-theme-meta"> 创建: ${createdDate} | 更新: ${updatedDate} </div> </div> <div class="luogu-theme-actions"> ${!isCurrent ? `<button class="luogu-action-btn apply" data-action="apply">应用</button>` : `<span style="color:#4CAF50;font-weight:bold">✓ 当前</span>` } <button class="luogu-action-btn rename" data-action="rename">重命名</button> ${Object.keys(themes).length > 1 ? `<button class="luogu-action-btn delete" data-action="delete">删除</button>` : '' } </div> </div> `; });
html += ` </div> <div class="luogu-panel-footer"> <button id="luogu-close-panel" class="luogu-panel-btn secondary">关闭</button> </div> `;
return html; }
// 设置主题管理事件 function setupThemesManagementEvents() { const panel = document.querySelector('#luogu-main-panel'); if (!panel) return;
// 新建主题 panel.querySelector('#luogu-new-theme')?.addEventListener('click', () => { showCreateThemeModal(); });
// 复制当前主题 panel.querySelector('#luogu-duplicate-theme')?.addEventListener('click', () => { showDuplicateThemeModal(); });
// 重新处理标签 panel.querySelector('#luogu-reprocess-btn')?.addEventListener('click', () => { removeExistingPanels(); processAllDifficulties(); alert('已重新处理难度标签'); });
// 刷新页面 panel.querySelector('#luogu-full-refresh')?.addEventListener('click', () => { removeExistingPanels(); fullRefreshPage(); });
// 主题操作按钮 panel.querySelectorAll('.luogu-action-btn').forEach(btn => { btn.addEventListener('click', function() { const themeItem = this.closest('.luogu-theme-item'); const themeName = themeItem.getAttribute('data-theme'); const action = this.getAttribute('data-action');
switch(action) { case 'apply': applyTheme(themeName); break; case 'rename': showRenameThemeModal(themeName); break; case 'delete': deleteTheme(themeName); break; } }); });
// 关闭面板 panel.querySelector('#luogu-close-panel')?.addEventListener('click', () => { removeExistingPanels(); }); }
// 获取主题编辑内容 function getThemeEditorContent() { const config = getCurrentConfig();
let html = ` <h3 style="margin-top:0">编辑当前主题: ${storage.currentTheme}</h3> <p style="color:#666;margin-bottom:15px">修改当前主题的难度显示配置</p> <div class="luogu-difficulty-editor"> `;
DIFFICULTIES.forEach(diff => { const configItem = config[diff] || { type: 'text', value: diff, color: '#95a5a6' };
html += ` <div class="luogu-difficulty-item" data-difficulty="${diff}"> <div class="luogu-difficulty-header">${diff}</div> <div class="luogu-option-group"> <span class="luogu-option-label">类型:</span> <select class="luogu-option-input type-select"> <option value="text" ${configItem.type === 'text' ? 'selected' : ''}>文字</option> <option value="image" ${configItem.type === 'image' ? 'selected' : ''}>图片</option> </select> <input type="text" data-diff="${diff}" value="${configItem.value || ''}" placeholder="${configItem.type === 'text' ? '输入文字' : '输入图片URL'}" style="width:200px;margin-right:10px"> ${configItem.type === 'text' ? `<input type="color" data-diff="${diff}" value="${configItem.color}" style="vertical-align:middle">` : ''} </div> <div class="luogu-option-group"> <span class="luogu-option-label">内容:</span> <input type="text" class="luogu-option-input content-input" value="${configItem.value || diff}" placeholder="${configItem.type === 'text' ? '输入文字' : '输入图片URL'}"> </div> ${configItem.type === 'text' ? ` <div class="luogu-option-group"> <span class="luogu-option-label">颜色:</span> <input type="color" class="luogu-color-input color-input" value="${configItem.color || '#95a5a6'}"> </div> ` : ''} </div> `; });
html += '</div>'; html += ` <div style="margin-top:15px;text-align:right"> <button id="luogu-save" style="margin-right:10px">保存</button> <button id="luogu-reset">恢复默认</button> <button id="luogu-close" style="margin-left:10px">关闭</button> </div> <div class="luogu-panel-footer"> <button id="luogu-save-config" class="luogu-panel-btn success">保存更改</button> <button id="luogu-reset-config" class="luogu-panel-btn secondary">重置为默认</button> <button id="luogu-reprocess-btn2" class="luogu-panel-btn primary">重新处理</button> <button id="luogu-full-refresh2" class="luogu-panel-btn danger">刷新页面</button> <button id="luogu-close-panel2" class="luogu-panel-btn secondary">关闭</button> </div> `;
panel.innerHTML = html; return html; }
// 添加到页面 document.body.appendChild(overlay); document.body.appendChild(panel); // 设置主题编辑事件 function setupThemeEditorEvents() { const panel = document.querySelector('#luogu-main-panel'); if (!panel) return;
// 事件处理 panel.querySelectorAll('select').forEach(select => { // 类型选择改变事件 panel.querySelectorAll('.type-select').forEach(select => { select.addEventListener('change', function() { const diff = this.getAttribute('data-diff'); const input = panel.querySelector(`input[type="text"][data-diff="${diff}"]`); input.placeholder = this.value === 'image' ? '输入图片URL' : '输入文字'; const item = this.closest('.luogu-difficulty-item'); const colorInput = item.querySelector('.color-input'); const contentInput = item.querySelector('.content-input');
if (this.value === 'image') { if (colorInput) colorInput.style.display = 'none'; contentInput.placeholder = '输入图片URL'; } else { if (colorInput) { colorInput.style.display = 'block'; colorInput.value = colorInput.value || '#95a5a6'; } contentInput.placeholder = '输入文字'; } }); });
panel.querySelector('#luogu-save').addEventListener('click', function() { const newConfig = {}; difficulties.forEach(diff => { const select = panel.querySelector(`select[data-diff="${diff}"]`); const input = panel.querySelector(`input[type="text"][data-diff="${diff}"]`); const colorInput = panel.querySelector(`input[type="color"][data-diff="${diff}"]`); // 保存配置 panel.querySelector('#luogu-save-config')?.addEventListener('click', () => { saveThemeConfig(); });
if (select && input) { const type = select.value; const value = input.value.trim(); const color = colorInput ? colorInput.value : undefined; // 重置配置 panel.querySelector('#luogu-reset-config')?.addEventListener('click', () => { if (confirm('确定要重置当前主题的配置为默认值吗?')) { const defaultTheme = DEFAULT_THEMES[storage.currentTheme] || DEFAULT_THEMES['经典图标主题']; updateCurrentConfig(JSON.parse(JSON.stringify(defaultTheme.config))); showTabContent('editor'); // 刷新编辑器 setTimeout(() => { processAllDifficulties(); updateQuickMenu(); }, 100); } });
newConfig[diff] = { type: type, value: value || defaultConfig[diff].value, color: type === 'text' ? (color || defaultConfig[diff].color) : undefined }; } // 重新处理 panel.querySelector('#luogu-reprocess-btn2')?.addEventListener('click', () => { removeExistingPanels(); processAllDifficulties(); alert('已重新处理难度标签'); });
// 刷新页面 panel.querySelector('#luogu-full-refresh2')?.addEventListener('click', () => { removeExistingPanels(); fullRefreshPage(); });
// 关闭面板 panel.querySelector('#luogu-close-panel2')?.addEventListener('click', () => { removeExistingPanels(); }); }
// 获取设置内容 function getSettingsContent() { return ` <h3 style="margin-top:0">设置</h3> <div style="margin-bottom:15px"> <label style="display:flex;align-items:center;margin-bottom:10px"> <input type="checkbox" id="auto-refresh" ${storage.settings.autoRefresh ? 'checked' : ''} style="margin-right:8px"> <span>自动刷新页面变化</span> </label> <label style="display:flex;align-items:center;margin-bottom:10px"> <input type="checkbox" id="show-refresh-btn" ${storage.settings.showRefreshButton ? 'checked' : ''} style="margin-right:8px"> <span>显示快速切换菜单</span> </label> </div> <div class="luogu-panel-footer"> <button id="luogu-save-settings" class="luogu-panel-btn success">保存设置</button> <button id="luogu-close-panel3" class="luogu-panel-btn secondary">关闭</button> </div> `; }
// 设置设置事件 function setupSettingsEvents() { const panel = document.querySelector('#luogu-main-panel'); if (!panel) return;
// 保存设置 panel.querySelector('#luogu-save-settings')?.addEventListener('click', () => { const autoRefresh = panel.querySelector('#auto-refresh').checked; const showRefreshBtn = panel.querySelector('#show-refresh-btn').checked;
storage.settings.autoRefresh = autoRefresh; storage.settings.showRefreshButton = showRefreshBtn; saveStorage();
if (showRefreshBtn) { addQuickMenu(); } else { const quickMenu = document.querySelector('.luogu-quick-menu'); if (quickMenu) quickMenu.remove(); }
alert('设置已保存'); });
// 关闭面板 panel.querySelector('#luogu-close-panel3')?.addEventListener('click', () => { removeExistingPanels(); }); }
// 显示快速切换主题面板 function showQuickSwitch() { removeExistingPanels();
const overlay = document.createElement('div'); overlay.className = 'luogu-panel-overlay';
const modal = document.createElement('div'); modal.className = 'luogu-modal'; modal.innerHTML = ` <div class="luogu-modal-title">快速切换主题</div> <div style="max-height:300px;overflow-y:auto;margin-bottom:15px"> ${Object.values(storage.themes).map(theme => { const isCurrent = theme.name === storage.currentTheme; return ` <div style="padding:10px;border-bottom:1px solid #eee;cursor:pointer;background:${isCurrent ? '#e3f2fd' : 'white'}" class="luogu-quick-theme" data-theme="${theme.name}"> <div style="font-weight:bold">${theme.name}</div> ${isCurrent ? '<div style="color:#4CAF50;font-size:12px">✓ 当前主题</div>' : ''} </div> `; }).join('')} </div> <div class="luogu-modal-footer"> <button id="luogu-cancel-switch" class="luogu-panel-btn secondary">取消</button> </div> `;
document.body.appendChild(overlay); document.body.appendChild(modal);
// 主题点击事件 modal.querySelectorAll('.luogu-quick-theme').forEach(item => { item.addEventListener('click', function() { const themeName = this.getAttribute('data-theme'); applyTheme(themeName); removeExistingPanels(); }); });
GM_setValue('luogu_difficulty_config', newConfig); config = newConfig; forceRefresh(); alert('设置已保存!'); // 取消按钮 modal.querySelector('#luogu-cancel-switch')?.addEventListener('click', () => { removeExistingPanels(); });
overlay.remove(); panel.remove(); // 遮罩层点击关闭 overlay.addEventListener('click', () => { removeExistingPanels(); }); }
panel.querySelector('#luogu-reset').addEventListener('click', function() { if (confirm('恢复默认设置?')) { GM_setValue('luogu_difficulty_config', defaultConfig); config = defaultConfig; forceRefresh(); alert('已恢复默认'); overlay.remove(); panel.remove(); // 应用主题 function applyTheme(themeName) { if (!storage.themes[themeName]) { alert('主题不存在'); return; }
storage.currentTheme = themeName; saveStorage();
console.log(`已切换到主题: ${themeName}`); processAllDifficulties(); updateQuickMenu();
// 如果主题管理器开着,刷新它 const panel = document.querySelector('#luogu-main-panel'); if (panel) { showTabContent('themes'); } }
// 更新快速菜单 function updateQuickMenu() { const themeBtn = document.querySelector('.luogu-quick-btn.theme-switch'); if (themeBtn) { themeBtn.textContent = `主题: ${storage.currentTheme}`; } }
// 显示创建主题模态框 function showCreateThemeModal() { removeExistingPanels();
const overlay = document.createElement('div'); overlay.className = 'luogu-panel-overlay';
const modal = document.createElement('div'); modal.className = 'luogu-modal'; modal.innerHTML = ` <div class="luogu-modal-title">新建主题</div> <input type="text" id="luogu-new-theme-name" class="luogu-modal-input" placeholder="输入主题名称" maxlength="20"> <div style="margin-bottom:15px"> <label style="display:block;margin-bottom:5px">基于现有主题:</label> <select id="luogu-base-theme" class="luogu-modal-input"> <option value="">空主题</option> ${Object.values(storage.themes).map(theme => `<option value="${theme.name}">${theme.name}</option>` ).join('')} </select> </div> <div class="luogu-modal-footer"> <button id="luogu-confirm-create" class="luogu-panel-btn success">创建</button> <button id="luogu-cancel-create" class="luogu-panel-btn secondary">取消</button> </div> `;
document.body.appendChild(overlay); document.body.appendChild(modal);
// 创建按钮 modal.querySelector('#luogu-confirm-create')?.addEventListener('click', () => { const themeName = modal.querySelector('#luogu-new-theme-name').value.trim(); const baseTheme = modal.querySelector('#luogu-base-theme').value;
if (!themeName) { alert('请输入主题名称'); return; }
if (storage.themes[themeName]) { alert('主题名称已存在'); return; }
// 创建新主题 const now = new Date().toISOString(); let config;
if (baseTheme && storage.themes[baseTheme]) { config = JSON.parse(JSON.stringify(storage.themes[baseTheme].config)); } else { config = JSON.parse(JSON.stringify(DEFAULT_THEMES['经典图标主题'].config)); }
storage.themes[themeName] = { name: themeName, created: now, updated: now, config: config };
saveStorage(); removeExistingPanels(); showThemeManager(); });
panel.querySelector('#luogu-close').addEventListener('click', function() { overlay.remove(); panel.remove(); // 取消按钮 modal.querySelector('#luogu-cancel-create')?.addEventListener('click', () => { removeExistingPanels(); showThemeManager(); });
overlay.addEventListener('click', function() { overlay.remove(); panel.remove(); // 遮罩层点击关闭 overlay.addEventListener('click', () => { removeExistingPanels(); showThemeManager(); }); }
// 初始化 function init() { console.log('洛谷难度自定义脚本启动'); // 显示复制主题模态框 function showDuplicateThemeModal() { const currentTheme = storage.themes[storage.currentTheme]; const newName = `${currentTheme.name} 副本`;
// 初始处理 showCreateThemeModal();
// 设置默认值 setTimeout(() => { const nameInput = document.querySelector('#luogu-new-theme-name'); const baseSelect = document.querySelector('#luogu-base-theme'); if (nameInput && baseSelect) { nameInput.value = newName; baseSelect.value = storage.currentTheme; } }, 50); }
// 显示重命名主题模态框 function showRenameThemeModal(themeName) { removeExistingPanels();
const overlay = document.createElement('div'); overlay.className = 'luogu-panel-overlay';
const modal = document.createElement('div'); modal.className = 'luogu-modal'; modal.innerHTML = ` <div class="luogu-modal-title">重命名主题</div> <input type="text" id="luogu-rename-theme-name" class="luogu-modal-input" value="${themeName}" placeholder="输入新的主题名称" maxlength="20"> <div class="luogu-modal-footer"> <button id="luogu-confirm-rename" class="luogu-panel-btn success">确定</button> <button id="luogu-cancel-rename" class="luogu-panel-btn secondary">取消</button> </div> `;
document.body.appendChild(overlay); document.body.appendChild(modal);
// 重命名按钮 modal.querySelector('#luogu-confirm-rename')?.addEventListener('click', () => { const newName = modal.querySelector('#luogu-rename-theme-name').value.trim();
if (!newName) { alert('请输入新的主题名称'); return; }
if (newName === themeName) { removeExistingPanels(); showThemeManager(); return; }
if (storage.themes[newName]) { alert('主题名称已存在'); return; }
// 重命名主题 const theme = storage.themes[themeName]; theme.name = newName; theme.updated = new Date().toISOString();
// 更新主题列表 storage.themes[newName] = theme; delete storage.themes[themeName];
// 如果当前主题被重命名,更新当前主题 if (storage.currentTheme === themeName) { storage.currentTheme = newName; }
saveStorage(); removeExistingPanels(); showThemeManager(); });
// 取消按钮 modal.querySelector('#luogu-cancel-rename')?.addEventListener('click', () => { removeExistingPanels(); showThemeManager(); });
// 遮罩层点击关闭 overlay.addEventListener('click', () => { removeExistingPanels(); showThemeManager(); }); }
// 删除主题 function deleteTheme(themeName) { if (Object.keys(storage.themes).length <= 1) { alert('不能删除最后一个主题'); return; }
if (storage.currentTheme === themeName) { alert('不能删除当前正在使用的主题,请先切换到其他主题'); return; }
if (confirm(`确定要删除主题 "${themeName}" 吗?`)) { delete storage.themes[themeName]; saveStorage();
// 刷新主题管理器 const panel = document.querySelector('#luogu-main-panel'); if (panel) { showTabContent('themes'); } } }
// 保存主题配置 function saveThemeConfig() { const config = {};
DIFFICULTIES.forEach(diff => { const item = document.querySelector(`[data-difficulty="${diff}"]`); if (item) { const type = item.querySelector('.type-select').value; const value = item.querySelector('.content-input').value.trim(); const color = type === 'text' ? item.querySelector('.color-input')?.value : undefined;
config[diff] = { type: type, value: value || diff, color: color || '#95a5a6' }; } });
if (updateCurrentConfig(config)) { alert('配置已保存'); processAllDifficulties(); addRefreshButton(); }, 2000); } else { alert('保存失败'); } }
// 监听DOM变化 // 显示主题编辑器 function showThemeEditor() { removeExistingPanels();
const overlay = document.createElement('div'); overlay.className = 'luogu-panel-overlay';
const panel = document.createElement('div'); panel.id = 'luogu-main-panel'; panel.innerHTML = getThemeEditorContent();
document.body.appendChild(overlay); document.body.appendChild(panel);
setupThemeEditorEvents();
overlay.addEventListener('click', () => { removeExistingPanels(); }); }
// 移除所有面板 function removeExistingPanels() { const panels = [ '.luogu-panel-overlay', '#luogu-main-panel', '.luogu-modal' ];
panels.forEach(selector => { const element = document.querySelector(selector); if (element) element.remove(); }); }
// 监听页面变化 function setupPageChangeListener() { if (!storage.settings.autoRefresh) return;
const observer = new MutationObserver(() => { // 延迟处理,避免频繁执行 setTimeout(processAllDifficulties, 500); // 检查是否有新的难度标签出现 const selectors = [ '.difficulty-tags .row span:not(.problem-count):not(.luogu-processed)', '.lcolor--grey-3:not(.luogu-processed)', '.lcolor--pink-3:not(.luogu-processed)', '.lcolor--orange-3:not(.luogu-processed)', '.lcolor--gold-3:not(.luogu-processed)', '.lcolor--green-3:not(.luogu-processed)', '.lcolor--blue-3:not(.luogu-processed)', '.lcolor--purple-3:not(.luogu-processed)', '.lcolor--lapis-4:not(.luogu-processed)', '.difficulty:not(.luogu-processed)' ].join(',');
const unprocessed = document.querySelectorAll(selectors);
if (unprocessed.length > 0) { console.log(`检测到 ${unprocessed.length} 个新的难度元素,重新处理`); setTimeout(processAllDifficulties, 500); } });
observer.observe(document.body, { childList: true, subtree: true }); }
// 初始化 function init() { console.log('洛谷难度主题管理器 v10.3 启动 - 作者:鸡腿,PCY和祥子');
// 加载存储数据 loadStorage();
// 初始处理 setTimeout(() => { console.log('开始初始处理难度标签'); processAllDifficulties(); if (storage.settings.showRefreshButton) { addQuickMenu(); } }, 2000);
// 监听页面变化 setupPageChangeListener(); }
// 启动 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { setTimeout(init, 1000); }})();```
事实上,脚本是 Deepseek 写的,不过,你会发现,版本号为 10.3,其实我们反复调教你好几次,确认每个页面的难度都可以正常显示,同时保证不会有内容识别出错,所以,即使是 AI 来干,我们也花费了三天时间来完善和扩展,也就变成了你现在看到的这样。
希望大家能喜欢这个脚本,谢谢了。我今天入谷时长两年半,感谢各位。