Beautiful OpenJudge

使用 BootStrap 库美化 OpenJudge

当前为 2021-04-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Beautiful OpenJudge
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.10
  5. // @description 使用 BootStrap 库美化 OpenJudge
  6. // @author Guyutongxue
  7. // @match http://*.openjudge.cn/*
  8. // @match http://*.test.openjudge.org/*
  9. // @grant none
  10. // @require https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js
  11. // @require https://cdn.bootcdn.net/ajax/libs/jquery.form/4.3.0/jquery.form.min.js
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // Load BootStrap 4
  18. document.head.innerHTML += `<meta name="viewport" content="width=device-width, initial-scale=1">
  19. <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css">
  20. <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>
  21. <style>
  22. /* Fix styles*/
  23. /* Titles*/
  24. h4{font-size:inherit;}
  25. h2,h3{font-size:18px;}
  26. h1,h2,h3{font-weight:bold;}
  27. h1{font-size:2em}
  28. /*Headers*/
  29. #headerTop{background:initial;}
  30. #headerTop a{color:initial;}
  31. #userToolbar, #headerTop .logo {font-size: inherit;}
  32. #headerTop #userToolbar .current a.link{background:initial;}
  33. .practice-search button{background:#545b62;font-size:12px;width:25px;padding:0px;}
  34. #groupBigLogo,#groupBigLogo img{max-width: 100%;height: auto;}
  35. #pageTitle{padding-bottom:0px;border-bottom:0px;margin-bottom:0px;}
  36. .wrapper{width:initial;margin:0 10%;}
  37. .appli-group{height:auto;}
  38. /*Contest title*/
  39. .recently-update, .over-time{position:relative;}
  40. .label h3, .current-contest h3{border-bottom:0;}
  41. .group-setting{font-size:smaller;}
  42. /*Tables*/
  43. table{line-height:initial;font-size:smaller;}
  44. .my-solutions td.result{line-height:2.5em;}
  45. #problemsList table tr{font-size:inherit;line-height:inherit}
  46. .my-solutions{font-size:smaller;}
  47. .my-solutions .time{width:auto;}
  48. .practice-info table tr td{padding:.5em;}
  49. table th{font-weight:bold;}
  50. table thead tr{background:initial;}
  51. /*Personal Page*/
  52. .recently-submit table td{padding:.3rem;}
  53. .recently-submit .contest{width:auto;}
  54. .all-group li{float:left; overflow: initial;}
  55. .my-group-logo{margin-right:15px;}
  56. /*Problem*/
  57. .problem-content pre {margin-bottom:0;}
  58. .problem-content dd {margin-bottom:0;}
  59. </style>`;
  60. // Dealing with main header
  61. $("#headerTop").addClass('bg-light');
  62. $("#userToolbar").css('margin-bottom','0');
  63. $("#userToolbar").addClass('btn-group btn-group-sm');
  64. $("#userToolbar li").addClass('btn btn-sm btn-light');
  65. $(".account-list li").attr('class', 'btn btn-sm btn-link');
  66. $(".search-form").addClass('inline-form');
  67. $(".search-form input").addClass('form-control').attr('value','').attr('placeholder','题目ID, 标题, 描述');
  68. $("button").addClass('btn btn-secondary');
  69. $(".search-form button").html("&#10140;"); // right arrow
  70.  
  71. // Dealing with main container
  72. $("#pagebody,#sitePagebody,#footer .wrapper").attr('class','container');
  73. $("#pagebody .wrapper,#sitePagebody .wrapper").attr('class','row mt-3');
  74. $(".col-2").removeClass('col-2').addClass('col-md-2');
  75. $(".col-3").removeClass('col-3').addClass('col-md-3');
  76. $(".col-4").removeClass('col-4').addClass('col-md-4');
  77. $(".col-8").removeClass('col-8').addClass('col-md-8');
  78. $(".col-9").removeClass('col-9').addClass('col-md-9');
  79. $(".col-10").removeClass('col-10').addClass('col-md-10');
  80. $(".problem-page").removeClass('problem-page');
  81. $(".problem-statistics").removeClass('problem-statistics');
  82. $(".problem-status").removeClass('problem-status');
  83. $(".problem-my-statistics").removeClass('problem-my-statistics');
  84.  
  85. // Dealing with problem header
  86. $("#header").addClass("mb-4");
  87.  
  88. // Group index page
  89. $(".contest-info").removeClass('contest-info').addClass('d-flex justify-content-lg-between flex-lg-row flex-column').css('border-bottom','1px dotted #666666');
  90. $(".recently-update").remove();
  91. $(".practice-info h3,.coming-contest h3,.past-contest h3").css('border-bottom','1px dotted #666666');
  92. console.log($(".group-setting").children().text().replace(/\s+/g, ""));
  93. // If I'm in this group, then change it's style
  94. if($(".group-setting").children().text().replace(/\s+/g, "")!=""){
  95. $(".group-setting").html('\
  96. <ul class="btn-group btn-group-sm">\
  97. <a href="/mine/" class="btn btn-sm btn-outline-secondary ">修改设定</a>\
  98. <a href="javascript:void(0);" onclick="if (confirm(\'你确定要退出小组吗?\')) api.leaveGroup(9,null,local.redirect);" class="btn btn-sm btn-danger">退出小组</a>\
  99. </ul>').addClass('mt-3 mb-0');
  100. }
  101.  
  102. // Site index page fixing
  103. $(".row").children('p').addClass('col-md-12 alert alert-info');
  104. $(".row").children('p').each(function(){if($(this).text().replace(/\s+/g, "")=="")$(this).hide();}); // remove extra spaces
  105. $(".user-group,.my-group-contest").addClass("row");
  106. $(".recently-submit,.my-contest-list").addClass('col-md-10');
  107.  
  108. // Alerts
  109. $(".notification").attr('class','alert alert-warning');
  110. $(".contest-description").attr('class','alert alert-info');
  111. $(".notice").attr('class','alert alert-danger');
  112.  
  113. // Change top menu
  114. var tabs = $("#topMenu ul")
  115. tabs.addClass('nav nav-tabs');
  116. tabs.children('li').addClass('nav-item');
  117. $('.nav a').addClass('nav-link');
  118. $(".current-show").children().addClass('nav-link active');
  119. tabs.children('li').removeClass('current-show');
  120. $("#topMenu").addClass('col-md-12 mb-3 mt-2').removeAttr('id');
  121.  
  122. // Change bottom menu
  123. tabs = $(".bottomMenu");
  124. tabs.addClass('pagination');
  125. tabs.children('li').addClass('page-item');
  126. $('.pagination a').addClass('page-link');
  127. $(".current-show").addClass('active');
  128. tabs.children('li').removeClass('current-show');
  129.  
  130. // Change tables' style
  131. $("table").addClass('table table-sm table-hover table-responsive');
  132. $("#main,#contestStatistics,#problemStatus").children("table").wrapAll("<div class='row justify-content-center'><div class='col-auto'></div></div>");
  133. $(".practice-info table,#problemsList table,.recently-submit table").removeClass('table-responsive');
  134. $("thead tr td").replaceWith(function () {
  135. return $("<th />").append($(this).contents());
  136. });
  137. $("table thead").addClass('thead-light text-center');
  138. $(".practice-info table thead").removeClass('text-center');
  139. $("table td.accepted,table td.submissions,table td.code-length").css('min-width','5em');
  140. $("table td.title").css('min-width','15em');
  141.  
  142.  
  143. // Remove too long text
  144. $("td.class-name,td.className").each(function() {
  145. if ($(this).text().length > 10 && $(this).width() < 150) {
  146. $(this).attr('title',$(this).text());
  147. $(this).html($(this).text().replace(/\s+/g, "").substr(0, 10) + "...")
  148. }
  149. })
  150.  
  151. // Change searching form
  152. $(".status-search form").addClass('form-inline justify-content-center');
  153. $(".status-query-params").addClass('row');
  154. $(".status-query-params input,.status-query-params select").addClass('form-control form-control-sm');
  155. $(".status-query-params button").addClass('btn-sm');
  156.  
  157. // Change page bar
  158. var pageBar = $(".page-bar");
  159. if(pageBar.length > 0){
  160. pageBar.removeClass('page-bar');
  161. pageBar = pageBar.children('.pages').attr('class','pagination pagination-sm justify-content-center');
  162. pageBar.children().each(function(){
  163. if($(this).hasClass('current')){
  164. $(this).attr('class','page-link');
  165. $(this).wrapAll('<span class="page-item active"></span>');
  166. } else if ($(this).is('a')){
  167. $(this).attr('class','page-link');
  168. $(this).wrapAll('<span class="page-item"></span>');
  169. } else {
  170. $(this).attr('class','page-link');
  171. $(this).wrapAll('<span class="page-item disabled"></span>');
  172. }
  173. });
  174. // If it's a contest page, add pagigation in the top
  175. if($(".timeBar").length > 0) {
  176. $(".timeBar").after(pageBar.clone());
  177. }
  178. }
  179.  
  180. var abbr = {
  181. "Accepted": "AC",
  182. "Wrong Answer": "WA",
  183. "Time Limit Exceeded": "TLE",
  184. "Memory Limit Exceeded": "MLE",
  185. "Output Limit Exceeded": "OLE",
  186. "Runtime Error": "RE",
  187. "Compile Error": "CE",
  188. "Presentation Error": "PE",
  189. "Waiting": "W.",
  190. "Problem Disabled": "PD",
  191. "Running And Judging": "R&J.",
  192. "System Error": "SE",
  193. "Validator Error": "VE"
  194. };
  195.  
  196. var color = {
  197. "Accepted": "#52C41A",
  198. "Wrong Answer": "#E74C3C",
  199. "Presentation Error": "#00A497",
  200. "Time Limit Exceeded": "#052242",
  201. "Memory Limit Exceeded": "#052242",
  202. "Output Limit Exceeded": "#E74C3C",
  203. "Runtime Error": "#9D3DCF",
  204. "Compile Error": "#FADB14",
  205. "Waiting": "#14558F",
  206. "Problem Disabled": "#AAAAAA",
  207. "Running And Judging": "#14558F",
  208. "System Error":"#CC317C",
  209. "Validator Error": "#CC317C"
  210. }
  211.  
  212. // Change solution's style
  213. if(/^\/[^\/]+\/solution\/\d+\/?$/.test(window.location.pathname)) {
  214.  
  215. var result = $('.compile-status a').text();
  216. $('.compile-status a').remove();
  217.  
  218. var memory = $('.compile-info dl dd:eq(3)').text();
  219. var time = $('.compile-info dl dd:eq(4)').text();
  220.  
  221. var detail = result == "Compile Error" || result == "Waiting" ? "" : time +"/" + memory;
  222.  
  223. var newStatus = "\
  224. <div class='beautiful-status' title='" + result + "'>\
  225. " + abbr[result] + "\
  226. <div style='font-size:11px;'>" + detail + "</div>\
  227. </div>";
  228.  
  229. $('.compile-status').append(newStatus);
  230.  
  231. $('.beautiful-status').css({
  232. "background-color": color[result],
  233. "height": "100px",
  234. "width": "100px",
  235. "margin-top": "20px",
  236. "display": "flex",
  237. "align-items": "center",
  238. "justify-content": "center",
  239. "flex-direction": "column",
  240. "color": "white",
  241. "font-size": "24px",
  242. "font-family": "-apple-system, BlinkMacSystemFont, 'San Francisco', 'Helvetica Neue', 'Noto Sans CJK SC', 'Noto Sans CJK', 'Source Han Sans', 'PingFang SC', 'Microsoft YaHei', sans-serif"
  243. });
  244. }
  245.  
  246. // Change code's font
  247. if(/^\/[^\/]+\/[^\/]+\/submit\/?$/.test(window.location.pathname)) {
  248. $("textarea#source").css({
  249. "font-family": "Monaco, Menlo, 'Ubuntu Mono', Consolas, source-code-pro, monospace",
  250. "width": "100%"
  251. })
  252. // $("#submit").removeClass('col-md-9');
  253. $("#submit dt:eq(1)").text("语言");
  254. $("#submit form textarea").addClass('form-control');
  255. $("#main").width("100%");
  256. $(".submit-button").removeClass('btn-secondary').addClass('btn-primary');
  257. }
  258. $("pre,span.sh_string").css({
  259. "font-family": "Monaco, Menlo, 'Ubuntu Mono', Consolas, source-code-pro, monospace"
  260. })
  261.  
  262. // Change question('clarify')'s style
  263. if(/^\/[^\/]+\/clarify(\/[^\/]*\/?)?$/.test(window.location.pathname) || /^\/mine\/?$/.test(window.location.pathname)) {
  264. $("#main form").addClass('form');
  265. $("#main form textarea,#main form :text").addClass("form-control").width("90%");
  266. $("#main form :text").height("auto");
  267. }
  268.  
  269. // Change ranking style
  270. if(/^\/[^\/]+\/ranking\/?$/.test(window.location.pathname)) {
  271. let allData = $("td.alpha");
  272. for(let i = 0; i < allData.length; i++) {
  273. let text = allData.eq(i).html();
  274. if(text.indexOf("<a") != -1) continue;
  275. if(text.indexOf(":") != -1 ) { // If passed
  276. let res = '&#8730;';
  277. if(text.indexOf('<br>') != -1) {
  278. res = "&#8730;" + text.split('<br>')[1];
  279. }
  280. allData.eq(i).html(res);
  281. allData.eq(i).css({
  282. "background-color": "#dff0d890",
  283. "color": "#3c763d"
  284. });
  285. } else if(text.indexOf('(-') != -1) { // Or not passed, but tried
  286. let res = text.split('<br>')[1];
  287. allData.eq(i).html(res);
  288. allData.eq(i).css({
  289. "background-color": "#f2dede90",
  290. "color": "#a94442"
  291. });
  292. }
  293. }
  294.  
  295. $(document).ready(function(){
  296. // If too wide, add scrolling event
  297. if($('table')[0].scrollWidth > $('table').width()) {
  298. $('table').attr('id','scroll-horizontally');
  299. {
  300. function scrollHorizontally(e) {
  301. e = window.event || e;
  302. var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
  303. document.getElementById('scroll-horizontally').scrollLeft -= (delta * 60);
  304. e.preventDefault();
  305. }
  306. $('#scroll-horizontally').on('mousewheel',scrollHorizontally);
  307. }
  308. }
  309. });
  310.  
  311. }
  312.  
  313. // Change other status's style
  314. var otherResult = $('.result-wrong,.result-ce,.result-right,.result-pending');
  315. otherResult.css({
  316. "padding": "3px 6px",
  317. "color": "white",
  318. "border-radius": "2px",
  319. "font-style": "normal"
  320. });
  321. for(let i = 0; i < otherResult.length; i++) {
  322. let res = otherResult.eq(i).text();
  323. otherResult.eq(i).text(abbr[res]);
  324. otherResult.eq(i).attr('title',res);
  325. otherResult.eq(i).css({
  326. "background-color": color[res],
  327. });
  328. }
  329. $('.result').css('width','auto');
  330.  
  331.  
  332. // Change finish ratio's style
  333. function getColorByRatio(ratio){
  334. var one = (255+255) / 100;
  335. var r = 0;
  336. var g = 0;
  337. var b = 0;
  338. if (ratio < 50) {
  339. r = one * ratio;
  340. g = 255;
  341. }
  342. if (ratio >= 50) {
  343. g = 255 - ((ratio - 50 ) * one) ;
  344. r = 255;
  345. }
  346. r = parseInt(r);
  347. g = parseInt(g);
  348. b = parseInt(b);
  349. return "rgba(" + r + "," + g + "," + b + ",0.5)";
  350. }
  351. var ratios = $('.ratio');
  352. for(let i = 0; i < ratios.length; i++) {
  353. let value = parseInt(ratios.eq(i).text().replace('%',''));
  354. if (!isNaN(value)) {
  355. ratios.eq(i).css({'background-color':getColorByRatio(100 - value),'min-width':'4em'});
  356. }
  357. }
  358.  
  359. // Change time bar
  360. if($('.timeBar').length > 0) {
  361. let startTime = new Date($(".start-time-dd").text());
  362. let endTime = new Date($(".end-time-dd").text());
  363. let currentState = $(".current-time").text();
  364. function formatDateTime(date) {
  365. var y = date.getFullYear();
  366. var m = date.getMonth() + 1;
  367. m = m < 10 ? ('0' + m) : m;
  368. var d = date.getDate();
  369. d = d < 10 ? ('0' + d) : d;
  370. var h = date.getHours();
  371. h=h < 10 ? ('0' + h) : h;
  372. var minute = date.getMinutes();
  373. minute = minute < 10 ? ('0' + minute) : minute;
  374. var second=date.getSeconds();
  375. second=second < 10 ? ('0' + second) : second;
  376. return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
  377. };
  378. let newTime = `
  379. <div class="row">
  380. <div class="col-md-3 text-left"><small>开始时间</small><br><b>${formatDateTime(startTime)}</b></div>
  381. <div class="col-md-6 text-center"><b>${currentState}</b><p><small id="currentTime"></small></p></div>
  382. <div class="col-md-3 text-right"><small>结束时间</small><br><b>${formatDateTime(endTime)}</b></div>
  383. </div>`;
  384. $('.timeBar').append(newTime).addClass('mt-4 mb-4');
  385. $('.current-contest-info,.past-contest-info').remove();
  386. $('.timeBar').append('<p style="display:none;" id="timeclock"></p>');
  387. if(new Date() < endTime && new Date() > startTime){
  388. let total = endTime - startTime;
  389. let progress = `
  390. <div class="progress">
  391. <div class="progress-bar progress-bar-striped progress-bar-animated" id="timeProgressBar"
  392. role="progressbar" style="width: 0%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">
  393. </div>
  394. </div>`;
  395. $('.timeBar').append(progress);
  396. function updateValue(){
  397. var currentTime = new Date();
  398. var percent = (currentTime - startTime) * 100 / total;
  399. $("#timeProgressBar").attr({'style':'width:' + percent + '%','aria-valuenow':percent + ''});
  400. $("#currentTime").text(formatDateTime(currentTime));
  401. }
  402. updateValue();
  403. setInterval(updateValue,1000);
  404. }
  405. }
  406.  
  407. // Change those old icon to unicode character
  408. var solvedIcon = $('.solved');
  409. for(let i = 0; i < solvedIcon.length; i++) {
  410. let html = solvedIcon.eq(i).html();
  411. if(html.indexOf('accepted') != -1) {
  412. solvedIcon.eq(i).html("<span style='color:green;font-weight:bold;'>&#8730;</span>");
  413. }
  414. else if(html.indexOf('wrong') != -1) {
  415. solvedIcon.eq(i).html("<span style='color:red;;font-weight:bold;'>&#215;</span>");
  416. }
  417. }
  418.  
  419. // Change login page
  420. var loginForm = $("#main form[action='/api/auth/login/']");
  421. if(loginForm.length>0) {
  422. loginForm.parent().removeClass('col-md-8').addClass('col-md-12');
  423. loginForm.html(`
  424. <style>
  425. .form-signin {
  426. width: 100%;
  427. max-width: 330px;
  428. padding: 15px;
  429. margin: auto;
  430. }
  431. .form-signin .checkbox {
  432. font-weight: 400;
  433. }
  434. .form-signin .form-control {
  435. position: relative;
  436. box-sizing: border-box;
  437. height: auto;
  438. padding: 10px;
  439. font-size: 16px;
  440. }
  441. .form-signin .form-control:focus {
  442. z-ind ex: 2;
  443. }
  444. .form-signin input[type="text"] {
  445. margin-bottom: -1px;
  446. border-bottom-right-radius: 0;
  447. border-bottom-left-radius: 0;
  448. }
  449. .form-signin input[type="password"] {
  450. margin-bottom: 10px;
  451. border-top-left-radius: 0;
  452. border-top-right-radius: 0;
  453. }
  454. </style>
  455. <form action="/api/auth/login/" method="post" onsubmit="$(this).ajaxSubmit({dataType:'json',success:local.redirect,'target':'#result'}).find(':submit').each(function(){this.disabled=true;});return false;" class="form-signin">\
  456. <input type="hidden" name="redirectUrl" value="">
  457. <div class="login-message"></div>
  458. <label for="email" class="sr-only">邮箱地址</label>
  459. <input id="email" type="text" name="email" size="20" onfocus="this.select();" class="form-control" placeholder="邮箱地址">
  460. <label for="password" class="sr-only">密码</label>
  461. <input id="password" type="password" name="password" size="20" class="form-control" placeholder="密码">
  462. <button type="submit" class="btn btn-block btn-primary mt-3">登入</button>
  463. <div class="d-flex justify-content-between mt-3">
  464. <p><a href="/register/">点此注册(不可用)</a></p>
  465. <p><a href="http://openjudge.cn/auth/forget/">忘记密码?</a></p>
  466. </div>
  467. </form>`);
  468. }
  469.  
  470. // Change register style
  471. if(/^\/register\/?$/.test(window.location.pathname)) {
  472. var originalStyle = $('#pagebody .row,#sitePagebody .row').children('style').text();
  473. $('#pagebody .row,#sitePagebody .row').children('style').text(originalStyle.replace(/\.btn\s*\{[^}]*\}/,''));
  474. $('#pagebody .row,#sitePagebody .row').addClass('justify-content-center');
  475. $('#main').removeClass('col-md-10').addClass('col-md-4');
  476. $('#main form dd,form dt').remove();
  477. $('#main form').prepend(`
  478. <div class="mb-3">
  479. <label for="regEmail">Email地址</label>
  480. <input id="regEmail" type="text" name="user_email" class="form-control">
  481. </div>
  482. <div class="mb-3">
  483. <label for="regName">用户名</label>
  484. <input id="regName" type="text" name="user_name" class="form-control">
  485. </div>
  486. <div class="mb-3">
  487. <label for="regPasswd">密码</label>
  488. <input id="regPasswd" type="password" name="user_passwd" class="form-control">
  489. </div>
  490. <div class="mb-3">
  491. <label for="regPasswd2">确认密码</label>
  492. <input id="regPasswd2" type="password" name="user_passwd2" class="form-control">
  493. </div>`);
  494. $('#main form').append(`
  495. <p id="wait" class="hide">正在加载验证码......</p>
  496. <p id="notice" class="hide">请先拖动验证码到相应位置</p>
  497. <button type="submit" class="mt-3 btn btn-block btn-primary btn-lg">注册(不可用)</button>`);
  498. };
  499. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址