// ==UserScript== // @name 编程网格++ // @namespace http://tampermonkey.net/ // @version 0.3.2 // @description Enhance programming.pku.edu.cn // @author Guyutongxue // @match https://programming.pku.edu.cn/* // @require https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js // @require https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js // @downloadURL none // ==/UserScript== (function() { 'use strict'; // Remove extra newline $('.highin,.highout').children('br').remove(); // Add copy buttons if (location.pathname.endsWith("show.do")) { $('.fieldname').filter(function(i) { return $(this).text() === "例子输入"; }).append(``); $('.fieldname').filter(function(i) { return $(this).text() === "例子输出"; }).append(``); new ClipboardJS('.copy-btn'); } // For loading ace editor function injectScript(src) { return new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = src; script.onload = resolve; script.onerror = reject; document.head.append(script); }); } // Load Ace Editor before textarea let editor = null; function loadEditor() { const formJq = $('#submitInPage form'); const srcTextarea = $('[name="sourceCode"]'); srcTextarea.before(`
`); ace.config.set("basePath", "/x/ajax/libs/ace/1.4.9"); ace.require("ace/ext/language_tools"); editor = ace.edit("aceEditor"); editor.setOptions({ enableBasicAutocompletion: true, enableSnippets: true, enableLiveAutocompletion: true }); editor.setTheme("ace/theme/monokai"); editor.getSession().setMode("ace/mode/c_cpp"); editor.setValue(srcTextarea.val()); srcTextarea.parent().removeAttr('align'); srcTextarea.hide(); formJq.submit(function() { srcTextarea.val(editor.getValue()); return true; }); } if (location.pathname.endsWith("show.do")) { // Load editor injectScript('/x/ajax/libs/ace/1.4.9/ace.min.js') .then(() => injectScript('/x/ajax/libs/ace/1.4.9/ext-language_tools.min.js')) .then(loadEditor); // pgx & ocui const pgxLink = $('a[href^="/x/x.html"]'); const pgxHref = pgxLink.attr('href'); pgxLink.parent().hide().before(`启动【编程网格X】 在线编译`); $('#ocuiLink').click(() => { let code = ""; if (editor !== null) { code = editor.getValue(); } const param = new URLSearchParams(); param.append('code', code) if ($('input[value="C"]').prop('checked')) { window.open(`https://guyutongxue.gitee.io/cppocui/c?${param}`); } else { window.open(`https://guyutongxue.gitee.io/cppocui?${param}`); } }); // Select C++ and remove other $('input[value="C++"]').attr('checked', ''); // .parent().parent().attr('title', '禁用“编程网格++”脚本以使用其它语言') $('input[value="Java"],input[value="Python"],label[for="Java"],label[for="Python"]').hide(); } if (location.pathname.endsWith("solution.do")) { const pgxLink = $('a[href^="/x/x.html"]'); const pgxHref = pgxLink.attr('href'); const urlParam = new URLSearchParams(location.search); const solutionId = urlParam.get("solutionId"); pgxLink.parent().html(`启动【编程网格X】`); fetch(`/programming/problem/solution.do?solutionId=${solutionId}&sourceCode=text`) .then(r => r.text()) .then(t => $('a[href^="/x/x.html"]').parent().append(` 在线编译`)); } // Replace CSS $('link[type="text/css"]').remove(); $(document.head).append(` `); })();