版本比较
洛谷 IDE 缺省源
正文
增加 207 行删除 99 行
13 行未改动1415161718181920212228 行未改动51525354555655565758596061626363646566676869707172737071727374757677787576777880817980848586878182838485898687888990919293979495969798991001041051061071011021031041051091061071081091141151161171181101111121201211221231241131141151161171181261271281291191201211221331231351241251371381391401411261271281291301311321331431441341351361371381391401411421431441451461471481491501511461471521531541551561571581591601611621631641651661671681691701711721731741491751761511521771781791801541551811821831841851861571581871881891601901911621921931641941951961671971981691992001711721732012022032042052062072081751761771782092102112122132142151811822162172181841851861872192202212222231892242252262272282292302311911921931941951961972322332342352362371992382392402412422 行未改动24524624724821024925025125225325421525525621821922025725825926026126222426326426522722826626723023123226826927027123527227327427527627727827928028128223828328428528628728828929029129229329429524224329629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435513 行未改动```javascript// ==UserScript==// @name 洛谷 IDE 缺省源// @namespace http://tampermonkey.net/// @version 1.5// @version 2.2// @description 自动填充洛谷 IDE 缺省源// @author a_small_OIer// @include *://www.luogu.com.cn/problem/*// @include *://www.luogu.com.cn/contest/*28 行未改动 */
(function() { 'use strict'; if (/^\/problem\/list/.test(location.pathname)) return;
function isIdePage() { return location.hash.startsWith('#ide'); } const DEFAULT_TEMPLATE = `#include <bits/stdc++.h>using namespace std;int main() {
return 0;}`;
function getTemplate() { return GM_getValue('defaultCodeTemplate', DEFAULT_TEMPLATE); } function setTemplate(code) { GM_setValue('defaultCodeTemplate', code); }
function querySelectorAllDeep(selector, root = document) { let results = []; if (root.querySelectorAll) { //怎么还要穿透啊/ll function querySelectorAllDeep(selector, root = document){ const results = []; if (root.querySelectorAll) results.push(...root.querySelectorAll(selector)); } const allNodes = root.querySelectorAll ? root.querySelectorAll('*') : root.childNodes; for (const node of allNodes) { if (node.shadowRoot) { const allNodes = root.querySelectorAll ? root.querySelectorAll('*') : []; for(const node of allNodes) if(node.shadowRoot) results.push(...querySelectorAllDeep(selector, node.shadowRoot)); } } return results; }
function fillCM6(el) { if (!el) return false; if (el.cmView && el.cmView.view) { function fillCM6(el){ if(!el) return false; if(el.cmView && el.cmView.view){ const view = el.cmView.view; if (view.state.doc.length === 0) { if(view.state.doc.length === 0){ view.dispatch({ changes: { from: 0, to: 0, insert: getTemplate() } }); return true; } } const editable = el.querySelector('[contenteditable="true"]'); if (editable && editable.textContent.trim() === '') { if(editable && editable.textContent.trim() === ''){ editable.focus(); document.execCommand('insertText', false, getTemplate()); return true; } return false; }
function tryFillEditor(el) { if (!el || el.dataset.autofilled === 'true') return false; if (fillCM6(el)) { function tryFillEditor(el){ if(!el || el.dataset.autofilled === 'true') return false; if(fillCM6(el)){ el.dataset.autofilled = 'true'; console.log('【缺省源】缺省源填充成功'); return true; } return false; }
function setupContinuousObserver() { const initialEditors = querySelectorAllDeep('.cm-editor'); initialEditors.forEach(tryFillEditor);
function startObserver(){ querySelectorAllDeep('.cm-editor').forEach(tryFillEditor); const observer = new MutationObserver(mutations => { for (const mutation of mutations) { for (const node of mutation.addedNodes) { if (node.nodeType !== 1) continue;
if (node.matches && node.matches('.cm-editor')) { for(const mutation of mutations){ for(const node of mutation.addedNodes){ if(node.nodeType !== 1) continue; if(node.matches && node.matches('.cm-editor')) tryFillEditor(node); }
const nestedEditors = querySelectorAllDeep('.cm-editor', node); nestedEditors.forEach(tryFillEditor); querySelectorAllDeep('.cm-editor', node).forEach(tryFillEditor); } } });
observer.observe(document.body, { childList: true, subtree: true }); console.log('【缺省源】持续监听已启动'); return observer; }
if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', setupContinuousObserver); } else { setupContinuousObserver(); let ui = null; function findButtonByText(text){ const candidates = document.querySelectorAll('button, a, span, div'); for(const el of candidates) if(el.textContent.trim() === text) return el; return null; }
function createConfigUI() { function findToolbar(){ const selectors = [ '.ide-toolbar', '.toolbar', '.editor-toolbar', '.code-toolbar', '.ide-header', '.lfe-ide-toolbar', 'div[class*="toolbar"]', 'div[class*="Toolbar"]', 'header.ide-header' ]; for(const sel of selectors){ const bar = document.querySelector(sel); if(bar) return bar; } return null; } function createUI(){ if(ui) return ui; GM_addStyle(` .autofill-config-btn { position: fixed; bottom: 30px; right: 30px; .autofill-inline-btn { display: inline-flex; align-items: center; cursor: pointer; font-size: 14px; color: #3498db; background: transparent; border: 1px solid #3498db; border-radius: 4px; padding: 4px 12px; margin-left: 6px; transition: all 0.2s; white-space: nowrap; vertical-align: middle; line-height: 1.4; } .autofill-inline-btn:hover { background: #3498db; color: white; } .autofill-fab { position: fixed; bottom: 80px; right: 90px; width: 48px; height: 48px; border-radius: 50%; background: #4f46e5; color: white; font-size: 24px; background: #3498db; color: white; font-size: 22px; display: flex; align-items: center; justify-content: center; cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,0.2); z-index: 9999; user-select: none; transition: transform 0.2s; cursor: pointer; box-shadow: 0 4px 12px rgba(52,152,219,0.3); z-index: 10000; user-select: none; transition: transform 0.2s; border: none; } .autofill-config-btn:hover { transform: scale(1.1); background: #4338ca; } .autofill-modal-overlay { .autofill-fab:hover { transform: scale(1.1); box-shadow: 0 6px 16px rgba(52,152,219,0.4); } .autofill-overlay { display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); z-index: 10000; justify-content: center; align-items: center; background: rgba(0,0,0,0.4); z-index: 10001; justify-content: center; align-items: center; backdrop-filter: blur(3px); } .autofill-modal-overlay.active { display: flex; } .autofill-overlay.active { display: flex; } .autofill-modal { background: white; border-radius: 12px; width: 700px; background: white; border-radius: 16px; width: 700px; max-width: 90vw; max-height: 80vh; display: flex; flex-direction: column; box-shadow: 0 8px 30px rgba(0,0,0,0.3); overflow: hidden; box-shadow: 0 12px 40px rgba(0,0,0,0.2); overflow: hidden; } .autofill-modal-header { padding: 16px 20px; border-bottom: 1px solid #e5e7eb; padding: 20px 24px; border-bottom: 1px solid #f0f0f0; font-size: 18px; font-weight: 600; display: flex; justify-content: space-between; align-items: center; background: #f9fafb; justify-content: space-between; align-items: center; color: #111827; } .autofill-modal-close { cursor: pointer; font-size: 24px; color: #6b7280; } .autofill-modal-close:hover { color: #111827; } .autofill-modal-body { padding: 20px; flex: 1; overflow-y: auto; } .autofill-modal-close { cursor: pointer; font-size: 24px; color: #9ca3af; width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; border-radius: 8px; transition: background 0.15s; } .autofill-modal-close:hover { background: #f3f4f6; color: #111827; } .autofill-modal-body { padding: 24px; flex: 1; overflow-y: auto; } .autofill-modal-body textarea { width: 100%; height: 350px; font-family: 'Fira Code', Consolas, monospace; font-size: 14px; line-height: 1.5; padding: 12px; border: 1px solid #d1d5db; border-radius: 8px; resize: vertical; background: #fafafa; width: 100%; height: 360px; font-family: 'Fira Code', 'Cascadia Code', Consolas, monospace; font-size: 14px; line-height: 1.6; padding: 16px; border: 1px solid #e5e7eb; border-radius: 12px; resize: vertical; background: #fafafa; outline: none; transition: border 0.15s; } .autofill-modal-body textarea:focus { border-color: #3498db; } .autofill-modal-footer { padding: 12px 20px; border-top: 1px solid #e5e7eb; display: flex; justify-content: flex-end; gap: 8px; background: #f9fafb; padding: 16px 24px; border-top: 1px solid #f0f0f0; display: flex; justify-content: flex-end; gap: 12px; background: #fafafa; } .btn-save { padding: 8px 20px; border-radius: 6px; border: none; cursor: pointer; font-weight: 500; background: #4f46e5; color: white; .btn-primary { padding: 10px 24px; border-radius: 10px; border: none; cursor: pointer; font-weight: 500; font-size: 15px; background: #3498db; color: white; transition: background 0.2s; } .btn-save:hover { background: #4338ca; } .btn-primary:hover { background: #2980b9; } .btn-secondary { padding: 10px 24px; border-radius: 10px; border: 1px solid #d1d5db; cursor: pointer; font-weight: 500; font-size: 15px; background: white; color: #374151; transition: background 0.2s; } .btn-secondary:hover { background: #f9fafb; } `);
const btn = document.createElement('div'); btn.className = 'autofill-config-btn'; btn.innerHTML = '⚙️'; btn.title = '修改缺省源'; document.body.appendChild(btn);
//额啊 UI 真是一点都不会设计 const settingsBtn = document.createElement('button'); settingsBtn.className = 'autofill-inline-btn'; settingsBtn.textContent = '编辑缺省源'; settingsBtn.title = '修改 IDE 自动填充的代码模板'; const overlay = document.createElement('div'); overlay.className = 'autofill-modal-overlay'; overlay.className = 'autofill-overlay'; overlay.innerHTML = ` <div class="autofill-modal"> <div class="autofill-modal-header"> <span>编辑缺省源模板</span>2 行未改动 <div class="autofill-modal-body"> <textarea id="autofill-template-textarea" spellcheck="false"></textarea> </div> <div class="autofill-modal-footer"> <button class="btn-save" id="autofill-save-btn">保存</button> <button class="btn-secondary autofill-cancel-btn">取消</button> <button class="btn-primary autofill-save-btn">保存</button> </div> </div> `; document.body.appendChild(overlay);
const textarea = overlay.querySelector('#autofill-template-textarea'); const closeBtn = overlay.querySelector('.autofill-modal-close'); const saveBtn = overlay.querySelector('#autofill-save-btn');
function openModal() { const cancelBtn = overlay.querySelector('.autofill-cancel-btn'); const saveBtn = overlay.querySelector('.autofill-save-btn'); function openModal(){ textarea.value = getTemplate(); overlay.classList.add('active'); } function closeModal() { function closeModal(){ overlay.classList.remove('active'); }
btn.addEventListener('click', openModal); settingsBtn.addEventListener('click', openModal); closeBtn.addEventListener('click', closeModal); overlay.addEventListener('click', (e) => { if (e.target === overlay) closeModal(); }); cancelBtn.addEventListener('click', closeModal); overlay.addEventListener('click', e => { if (e.target === overlay) closeModal(); }); saveBtn.addEventListener('click', () => { setTemplate(textarea.value); alert('缺省源已保存!新打开的编辑器将使用此缺省源。'); const toast = document.createElement('div'); toast.textContent = '✅ 缺省源已保存'; Object.assign(toast.style, { position: 'fixed', bottom: '20px', left: '50%', transform: 'translateX(-50%)', background: '#3498db', color: 'white', padding: '10px 24px', borderRadius: '8px', zIndex: '99999', fontSize: '14px', opacity: '1', transition: 'opacity 0.3s' }); document.body.appendChild(toast); setTimeout(() => { toast.style.opacity = '0'; setTimeout(() => toast.remove(), 300); }, 1500); closeModal(); }); document.addEventListener('keydown', (e) => { document.addEventListener('keydown', e => { if (e.key === 'Escape' && overlay.classList.contains('active')) closeModal(); }); const observer = startObserver(); ui = { settingsBtn, overlay, textarea, observer, placement: 'none' }; return ui; }
createConfigUI(); function placeNextTo(target) { if(!ui || !target || !target.parentNode) return false; if(ui.settingsBtn.previousElementSibling === target) return true; target.parentNode.insertBefore(ui.settingsBtn, target.nextSibling); ui.settingsBtn.style.display = ''; return true; } function tryPlaceButton() { if(!ui) return; const selfTest = findButtonByText('自测'); if(selfTest){ if(placeNextTo(selfTest)){ ui.placement = 'selfTest'; return; } } } function onRouteChange(){ if (isIdePage()){ createUI(); tryPlaceButton(); //监听 const domObserver = new MutationObserver(() => { if (ui.placement !== 'selfTest') { const st = findButtonByText('自测'); if(st){ if(placeNextTo(st)){ ui.placement = 'selfTest'; domObserver.disconnect(); } } } }); domObserver.observe(document.body, { childList: true, subtree: true }); setTimeout(() => domObserver.disconnect(), 10000); }else{ if (ui){ if (ui.settingsBtn.parentNode) ui.settingsBtn.parentNode.removeChild(ui.settingsBtn); ui.settingsBtn.style.display = 'none'; ui.overlay.classList.remove('active'); } } } function init(){ createUI(); onRouteChange(); window.addEventListener('hashchange', onRouteChange); } if(document.readyState === 'loading'){ document.addEventListener('DOMContentLoaded', init); }else{ init(); }})();```
~~正常人谁用洛谷 IDE 啊。~~