你是否有被迫在艰苦环境在洛谷 IDE 写代码懒得敲缺省源的经历?
现隆重推出洛谷 IDE 缺省源!源代码复制后粘贴至油猴即可使用。
本代码使用 MIT License 开源协议,已经在源代码注明。这次学聪明了。
对于 IDE 自动保存
保证缺省源有 的概率上次写了一半的自动保存代码覆盖。
因为洛谷的 IDE 是基于 React 的,CodeMirror 6 的 EditorState 在组件挂载时同步创建,创建时必须传入初始文档字符串,而脚本是在页面完全加载完成后启动的,故不可能出现覆盖的情况。
源代码:
// ==UserScript==
// @name 洛谷 IDE 缺省源
// @namespace http://tampermonkey.net/
// @version 1.5
// @description 自动填充洛谷 IDE 缺省源
// @author a_small_OIer
// @include *://www.luogu.com.cn/problem/*
// @include *://www.luogu.com.cn/contest/*
// @run-at document-end
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// ==/UserScript==
/*
* MIT License
*
* Copyright (c) 2026 a_small_OIer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
(function() {
'use strict';
if (/^\/problem\/list/.test(location.pathname)) return;
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) {
results.push(...root.querySelectorAll(selector));
}
const allNodes = root.querySelectorAll ? root.querySelectorAll('*') : root.childNodes;
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) {
const view = el.cmView.view;
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() === '') {
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)) {
el.dataset.autofilled = 'true';
console.log('【缺省源】缺省源填充成功');
return true;
}
return false;
}
function setupContinuousObserver() {
const initialEditors = querySelectorAllDeep('.cm-editor');
initialEditors.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')) {
tryFillEditor(node);
}
const nestedEditors = querySelectorAllDeep('.cm-editor', node);
nestedEditors.forEach(tryFillEditor);
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
console.log('【缺省源】持续监听已启动');
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', setupContinuousObserver);
} else {
setupContinuousObserver();
}
function createConfigUI() {
GM_addStyle(`
.autofill-config-btn {
position: fixed; bottom: 30px; right: 30px;
width: 48px; height: 48px; border-radius: 50%;
background: #4f46e5; color: white; font-size: 24px;
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;
}
.autofill-config-btn:hover { transform: scale(1.1); background: #4338ca; }
.autofill-modal-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;
}
.autofill-modal-overlay.active { display: flex; }
.autofill-modal {
background: white; border-radius: 12px; 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;
}
.autofill-modal-header {
padding: 16px 20px; border-bottom: 1px solid #e5e7eb;
font-size: 18px; font-weight: 600; display: flex;
justify-content: space-between; align-items: center; background: #f9fafb;
}
.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-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;
}
.autofill-modal-footer {
padding: 12px 20px; border-top: 1px solid #e5e7eb;
display: flex; justify-content: flex-end; gap: 8px; background: #f9fafb;
}
.btn-save {
padding: 8px 20px; border-radius: 6px; border: none;
cursor: pointer; font-weight: 500;
background: #4f46e5; color: white;
}
.btn-save:hover { background: #4338ca; }
`);
const btn = document.createElement('div');
btn.className = 'autofill-config-btn';
btn.innerHTML = '⚙️';
btn.title = '修改缺省源';
document.body.appendChild(btn);
const overlay = document.createElement('div');
overlay.className = 'autofill-modal-overlay';
overlay.innerHTML = `
<div class="autofill-modal">
<div class="autofill-modal-header">
<span>编辑缺省源模板</span>
<span class="autofill-modal-close">×</span>
</div>
<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>
</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() {
textarea.value = getTemplate();
overlay.classList.add('active');
}
function closeModal() {
overlay.classList.remove('active');
}
btn.addEventListener('click', openModal);
closeBtn.addEventListener('click', closeModal);
overlay.addEventListener('click', (e) => {
if (e.target === overlay) closeModal();
});
saveBtn.addEventListener('click', () => {
setTemplate(textarea.value);
alert('缺省源已保存!新打开的编辑器将使用此缺省源。');
closeModal();
});
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && overlay.classList.contains('active')) closeModal();
});
}
createConfigUI();
})();正常人谁用洛谷 IDE 啊。