Beautiful OpenJudge

使用 BootStrap 库美化 OpenJudge

当前为 2021-03-11 提交的版本,查看 最新版本

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

QingJ © 2025

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