EYanIDE 优化
当前为
// ==UserScript==
// @name OptiYan
// @namespace http://lunarine.cc/
// @version 2024-08-17.2
// @description EYanIDE 优化
// @author Liu Baicheng
// @match http://121.36.38.167/
// @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant unsafeWindow
// @license MIT
// ==/UserScript==
(function () {
'use strict';
function classOptimize() {
// 自动 O2 优化
var compilerOptionsInput = document.getElementById('compiler-options');
compilerOptionsInput.value = "-O2";
}
function formatCppTemplateLine(line) {
const templateRegex = /(\btemplate\b|\bvector\b|\bmap\b|\bset\b|\bp(?:air|queue)\b|<.*>)/;
if (templateRegex.test(line.trim())) {
return line
.replace(/\s*(<|>)\s*/g, '$1')
.replace(/>(\w)/g, '> $1');
}
return line;
}
function formatCppCode(code) {
code = code.replace(/\/\*[\s\S]*?\*\//g, '');
code = code.replace(/\/\/.*$/gm, '');
const lines = code.split('\n');
let formattedLines = [];
let indentLevel = 0;
const indentSpace = ' ';
let inStruct = false;
for (let i = 0; i < lines.length; i++) {
let line = lines[i].trim();
const isEndOfStructWithVar = line.match(/}\s*[a-zA-Z_][a-zA-Z0-9_]*\s*\[?\s*[^;]*;/);
if (line.startsWith('struct')) {
inStruct = true;
formattedLines.push(indentSpace.repeat(indentLevel) + line);
indentLevel++;
continue;
}
if (line.endsWith('};') || isEndOfStructWithVar) {
if (indentLevel) indentLevel--;
formattedLines.push(indentSpace.repeat(indentLevel) + line);
inStruct = false;
if (isEndOfStructWithVar) {
continue;
}
formattedLines.push('');
continue;
}
if (inStruct) {
formattedLines.push(indentSpace.repeat(indentLevel) + line);
continue;
}
if (line.startsWith('#include')) {
line = line.replace(/#include\s*<\s*([\w./+-]+)\s*>/g, '#include <$1>');
formattedLines.push(line);
continue;
}
if (line === 'using namespace std;') {
formattedLines.push(indentSpace.repeat(indentLevel) + line);
formattedLines.push('');
continue;
}
if (line.endsWith(')') && lines[i + 1] && lines[i + 1].trim() === '{') {
line += ' {';
i++;
}
line = line.replace(/\s*([+\-*/=<>&|]+)\s*/g, ' $1 ');
line = line.replace(/\s*,\s*/g, ', ');
line = line.replace(/\s*([+\-]{2})\s*/g, '$1');
line = line.replace(/\s*;\s*/g, '; ');
line = line.replace(/\)\s*(?=[a-zA-Z+\-*/])/g, ') ');
line = line.replace(/(?<=[+\*/])\s*\(/g, ' (');
line = line.replace(/(?<!\S)-\s*\(/g, '-(');
line = line.replace(/\b(for|while|if|else|switch|case|do)\s*(?=[({])/g, '$1 ');
line = formatCppTemplateLine(line);
if (line.endsWith('}')) {
indentLevel--;
formattedLines.push(indentSpace.repeat(indentLevel) + line);
if (indentLevel === 0 && !inStruct) {
formattedLines.push('');
}
continue;
}
if (line.includes(') {')) {
if (indentLevel === 0 && !inStruct && formattedLines[formattedLines.length - 1] != '') {
formattedLines.push('');
formattedLines.push(indentSpace.repeat(indentLevel) + line);
indentLevel++;
continue;
}
}
if (line) {
formattedLines.push(indentSpace.repeat(indentLevel) + line);
}
if (line.endsWith('{')) {
indentLevel++;
}
}
return formattedLines.join('\n').trim();
}
function formatCode() {
const res = formatCppCode(unsafeWindow.sourceEditor.getValue());
unsafeWindow.sourceEditor.setValue(res);
}
classOptimize();
document.addEventListener('keydown', function (event) {
if (event.ctrlKey && event.altKey) {
formatCode(); // 调用目标函数
}
});
formatCode();
window.addEventListener('load', function () {
// 查找 site-footer 元素
var footer = document.querySelector('#site-footer');
if (footer) {
// 创建新的 span 元素
var span = document.createElement('span');
span.id = 'optiyan-line';
span.textContent = 'OptiYan 已加载 Version: V1.0.0';
span.style.color = "#fff";
span.style.float = "left";
span.style.left = "0";
span.style.textAlign = "left";
span.style.width = "fit-content";
// 将新的 span 添加到 site-footer 内
footer.appendChild(span);
}
});
function updateColor() {
if (unsafeWindow.stdoutEditor.getValue().includes('error')) {
const sitefooter = document.querySelector('#site-footer');
const linefooter = document.querySelector('#status-line');
sitefooter.style.backgroundColor = "#c14343";
linefooter.style.backgroundColor = "#c14343";
} else if (unsafeWindow.stdoutEditor.getValue() != '') {
const sitefooter = document.querySelector('#site-footer');
const linefooter = document.querySelector('#status-line');
sitefooter.style.backgroundColor = "#05a705";
linefooter.style.backgroundColor = "#05a705";
} else {
const sitefooter = document.querySelector('#site-footer');
const linefooter = document.querySelector('#status-line');
sitefooter.style.backgroundColor = "#9775fa";
linefooter.style.backgroundColor = "#9775fa";
}
}
setInterval(updateColor, 500);
})();