Beautiful OpenJudge

使用 BootStrap 库美化 OpenJudge

当前为 2020-09-28 提交的版本,查看 最新版本

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

QingJ © 2025

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