ex124OJ

Extend 124OJ!

当前为 2022-11-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name ex124OJ
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.4
  5. // @description Extend 124OJ!
  6. // @author Sukwants
  7. // @license MIT
  8. // @match http://124.221.194.184
  9. // @match http://124.221.194.184/*
  10. // @icon http://124.221.194.184/images/logo_small.png
  11. // @grant GM_addStyle
  12. // ==/UserScript==
  13.  
  14.  
  15. /* ==User Settings== */
  16.  
  17. // Change Page Background. (Image URL, Opacity)
  18. const BackgroundSettings = ['https://www.imageoss.com/images/2022/11/22/Waterleidingduinen_ZH-CN1430683267_1920x1080a4af60c7b974ccd7.jpg'];
  19. // Change username color.
  20. const NameColorList = {'Sukwants':'#8e44ad','Star32':'#e74c3c','2745518585':'#996600','syysongyuyang':'#e67e22'};
  21. // Add CCF level badge.
  22. const CCFBadgeList = {};
  23. // Add name tag badge.
  24. const TagBadgeList = {'Sukwants':['#8e44ad','旅客'],'Star32':['#e74c3c','陌生人'],'2745518585':['#996600','作弊者'],'syysongyuyang':['#e67e22','原批']};
  25.  
  26.  
  27. function getBackground() {
  28. if (BackgroundSettings[0]) return BackgroundSettings[0];
  29. else return '';
  30. }
  31. function getOpacity() {
  32. if (BackgroundSettings[1]) return BackgroundSettings[1];
  33. else return '0.85';
  34. }
  35. function BackgroundImage() {
  36. document.getElementsByClassName('navbar-brand')[0].innerHTML = '<img src="http://124.221.194.184/images/logo_small.png" alt="Logo" class="img-rounded" style="width:39px; height:39px;">';
  37. document.getElementsByClassName('container')[0].children[0].children[1].innerHTML = document.getElementsByClassName('container')[0].children[0].children[1].innerHTML.match(/(.*> ){0,1}(.*)/)[2];
  38. GM_addStyle('\
  39. body {\
  40. background: url("' + getBackground() + '");\
  41. background-repeat: no-repeat;\
  42. background-attachment: fixed;\
  43. background-position: 50% 50%;\
  44. background-size: cover;\
  45. }\
  46. .uoj-content {\
  47. background-color: #fff;\
  48. margin: 16px -16px;\
  49. padding: 16px 16px;\
  50. opacity: ' + getOpacity() + ';\
  51. border-radius: 8px;\
  52. }\
  53. .navbar {\
  54. margin: 16px -16px;\
  55. padding: 8px 16px;\
  56. opacity: ' + getOpacity() + ';\
  57. border-radius: 8px;\
  58. }');
  59. }
  60.  
  61. function CodeBlock() {
  62. GM_addStyle('\
  63. @import url(https://cdn.jsdelivr.net/npm/firacode@6.2.0/distr/fira_code.css);\
  64. code {\
  65. font-family: "Fira Code";\
  66. }\
  67. code.sh_cpp>span {\
  68. font-style: normal !important;\
  69. font-weight: 400 !important;\
  70. }\
  71. code.sh_cpp>span.sh_preproc,\
  72. code.sh_cpp>span.sh_keyword,\
  73. code.sh_cpp>span.sh_type {\
  74. color: #8959a8;\
  75. }\
  76. code.sh_cpp>span.sh_string {\
  77. color: #718c00;\
  78. }\
  79. code.sh_cpp>span.sh_cbracket {\
  80. color: #4d4d4c;\
  81. }\
  82. code.sh_cpp>span.sh_symbol {\
  83. color: #3e999f;\
  84. }\
  85. code.sh_cpp>span.sh_number {\
  86. color: #f5871f;\
  87. }\
  88. code.sh_cpp>span.sh_function {\
  89. color: #4271ae;\
  90. }\
  91. code.sh_cpp>span.sh_comment {\
  92. color: #8e908c;\
  93. }');
  94. }
  95.  
  96. function NameColor() {
  97. var Names = document.getElementsByClassName('uoj-username');
  98. var NeedsRepeat = false;
  99. for (var i in Names) {
  100. if (Names[i].innerHTML && NameColorList[Names[i].innerHTML]) {
  101. Names[i].style = 'color:' + NameColorList[Names[i].innerHTML];
  102. if (Names[i].parentElement.getAttribute('class') == 'legendLabel') {
  103. NeedsRepeat = true;
  104. }
  105. }
  106. }
  107. if (NeedsRepeat) {
  108. setInterval(function(){
  109. var Names = document.getElementsByClassName('uoj-username');
  110. for (var i in Names) {
  111. if (Names[i].innerHTML && Names[i].parentElement.getAttribute('class') == 'legendLabel' && NameColorList[Names[i].innerHTML]) {
  112. Names[i].style = 'color:' + NameColorList[Names[i].innerHTML];
  113. }
  114. }
  115. }, 200);
  116. }
  117. var Honors = document.getElementsByClassName('uoj-honor');
  118. for (var j in Honors) {
  119. if (Honors[j].innerHTML && NameColorList[Honors[j].innerHTML]) {
  120. Honors[j].style = "color:" + NameColorList[Honors[j].innerHTML];
  121. }
  122. }
  123. }
  124.  
  125. const NameWithBadge = /^(.{1,}?)( <.*){0,1}$/;
  126. function CCFBadge() {
  127. var Names = document.getElementsByClassName('uoj-username');
  128. for (var i in Names) {
  129. if (!Names[i].innerHTML || !NameWithBadge.test(Names[i].innerHTML)) continue;
  130. var name = Names[i].innerHTML.match(NameWithBadge)[1];
  131. if (CCFBadgeList[name] && Names[i].parentElement.getAttribute('class') != 'legendLabel') {
  132. Names[i].innerHTML = Names[i].innerHTML + ' <svg width="1em" height="1em" data-v-303bbf52="" aria-hidden="true" focusable="false" data-prefix="fad" data-icon="badge-check" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" style="margin-bottom:.25em!important;bottom:10px;--fa-primary-color:#fff;--fa-secondary-color:' + CCFBadgeList[name] + ';--fa-secondary-opacity:1;"><g data-v-303bbf52="" class="fa-group"><path data-v-303bbf52="" fill="var(--fa-secondary-color)" d="M512 256a88 88 0 0 0-57.1-82.4A88 88 0 0 0 338.4 57.1a88 88 0 0 0-164.8 0A88 88 0 0 0 57.1 173.6a88 88 0 0 0 0 164.8 88 88 0 0 0 116.5 116.5 88 88 0 0 0 164.8 0 88 88 0 0 0 116.5-116.5A88 88 0 0 0 512 256zm-144.8-44.25l-131 130a11 11 0 0 1-15.55-.06l-75.72-76.33a11 11 0 0 1 .06-15.56L171 224a11 11 0 0 1 15.56.06l42.15 42.49 97.2-96.42a11 11 0 0 1 15.55.06l25.82 26a11 11 0 0 1-.08 15.56z" class="fa-secondary"></path></g></svg>';
  133. }
  134. }
  135. var Honors = document.getElementsByClassName('uoj-honor');
  136. for (var j in Honors) {
  137. if (!Honors[j].innerHTML || !NameWithBadge.test(Honors[j].innerHTML)) continue;
  138. var honor = Honors[j].innerHTML.match(NameWithBadge)[1];
  139. if (CCFBadgeList[honor]) {
  140. Honors[j].innerHTML = Honors[j].innerHTML + ' <svg width="1em" height="1em" data-v-303bbf52="" aria-hidden="true" focusable="false" data-prefix="fad" data-icon="badge-check" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" style="margin-bottom:.25em!important;bottom:10px;--fa-primary-color:#fff;--fa-secondary-color:' + CCFBadgeList[honor] + ';--fa-secondary-opacity:1;"><g data-v-303bbf52="" class="fa-group"><path data-v-303bbf52="" fill="var(--fa-secondary-color)" d="M512 256a88 88 0 0 0-57.1-82.4A88 88 0 0 0 338.4 57.1a88 88 0 0 0-164.8 0A88 88 0 0 0 57.1 173.6a88 88 0 0 0 0 164.8 88 88 0 0 0 116.5 116.5 88 88 0 0 0 164.8 0 88 88 0 0 0 116.5-116.5A88 88 0 0 0 512 256zm-144.8-44.25l-131 130a11 11 0 0 1-15.55-.06l-75.72-76.33a11 11 0 0 1 .06-15.56L171 224a11 11 0 0 1 15.56.06l42.15 42.49 97.2-96.42a11 11 0 0 1 15.55.06l25.82 26a11 11 0 0 1-.08 15.56z" class="fa-secondary"></path></g></svg>';
  141. }
  142. }
  143. }
  144. function TagBadge() {
  145. var Names = document.getElementsByClassName('uoj-username');
  146. for (var i in Names) {
  147. if (!Names[i].innerHTML || !NameWithBadge.test(Names[i].innerHTML)) continue;
  148. var name = Names[i].innerHTML.match(NameWithBadge)[1];
  149. if (TagBadgeList[name] && Names[i].parentElement.getAttribute('class') != 'legendLabel') {
  150. Names[i].innerHTML = Names[i].innerHTML + ' <span style="background-color:' + TagBadgeList[name][0] + ';padding:0.2em 0.6em;border-radius:.2em;color:#fff;font-size:0.7em;font-weight:bold;display:inline-block">' + TagBadgeList[name][1] + '</span>';
  151. }
  152. }
  153. var Honors = document.getElementsByClassName('uoj-honor');
  154. for (var j in Honors) {
  155. if (!Honors[j].innerHTML || !NameWithBadge.test(Honors[j].innerHTML)) continue;
  156. var honor = Honors[j].innerHTML.match(NameWithBadge)[1];
  157. if (TagBadgeList[honor]) {
  158. Honors[j].innerHTML = Honors[j].innerHTML + ' <span style="background-color:' + TagBadgeList[honor][0] + ';padding:0.2em 0.6em;border-radius:.2em;color:#fff;font-size:0.7em;font-weight:bold;display:inline-block">' + TagBadgeList[honor][1] + '</span>';
  159. }
  160. }
  161. }
  162.  
  163. function TableStyle() {
  164. var Tables = document.getElementsByTagName('table');
  165. for (var table in Tables) {
  166. if (Tables[table].nodeType && Tables[table].parentNode.getAttribute('class') != 'legend' && !Tables[table].classList.length) {
  167. Tables[table].classList.add('table', 'table-bordered');
  168. }
  169. }
  170. var ths = document.getElementsByTagName('th');
  171. for (var th in ths) {
  172. if (ths[th].nodeType && ths[th].getAttribute('align')) {
  173. ths[th].setAttribute('style', 'text-align:' + ths[th].getAttribute('align'));
  174. }
  175. }
  176. }
  177.  
  178. const submissionRegExp = /^http:\/\/124.221.194.184\/submission\/\d{1,}/;
  179. function isSubmission() {
  180. return submissionRegExp.test(window.location.href);
  181. }
  182. function SubmissionCard() {
  183. setTimeout(function() {
  184. var Content = document.getElementsByClassName('uoj-content')[0];
  185. if (Content.childElementCount == 3) {
  186. var Table = Content.children[0];
  187. var TabList = document.createElement('ul');
  188. TabList.setAttribute('class', 'nav nav-tabs');
  189. TabList.setAttribute('role', 'tablist');
  190. TabList.innerHTML = '<li class="nav-item"><a class="nav-link active" href="#test-cases" role="tab" data-toggle="tab" aria-selected="true"><span class="glyphicon glyphicon-check"></span> 测试点信息</a></li><li class="nav-item"><a class="nav-link" href="#source" role="tab" data-toggle="tab" aria-selected="false"><span class="glyphicon glyphicon-file"></span> 源代码</a></li>';
  191. var TabContent = document.createElement('div');
  192. TabContent.setAttribute('class', 'tab-content');
  193. TabContent.innerHTML = '<div class="tab-pane card active" id="test-cases"><div class="card-body">' + Content.children[2].children[1].innerHTML + '</div></div><div class="tab-pane card" id="source"><div class="card-body">' + Content.children[1].children[1].innerHTML + '</div></div>';
  194. Content.innerHTML = "";
  195. Content.appendChild(Table);
  196. Content.appendChild(TabList);
  197. Content.appendChild(TabContent);
  198. }
  199. }, 10);
  200. }
  201.  
  202. const ProblemRegExp = /^http:\/\/124.221.194.184.*\/problem\/(\d{1,})/;
  203. function isProblem() {
  204. return ProblemRegExp.test(window.location.href);
  205. }
  206. function downloadData() {
  207. var DownloadTag = document.createElement('a');
  208. DownloadTag.setAttribute('role', 'button');
  209. DownloadTag.setAttribute('class', 'btn btn-primary float-right');
  210. DownloadTag.setAttribute('href', '/download.php?type=problem&id=' + window.location.href.match(ProblemRegExp)[1]);
  211. DownloadTag.setAttribute('target', '_blank');
  212. DownloadTag.innerHTML = '<span class="glyphicon glyphicon-download-alt"></span> 下载数据';
  213. document.getElementsByClassName("btn btn-info float-right")[0].after(DownloadTag);
  214. }
  215.  
  216. const UserProfileRegExp = /^http:\/\/124.221.194.184\/user\/profile\/.{1,}/;
  217. function isUserProfile() {
  218. return UserProfileRegExp.test(window.location.href);
  219. }
  220. const BlogRegExp = /^http:\/\/124.221.194.184\/blog\/.{1,}/;
  221. function isBlog() {
  222. return BlogRegExp.test(window.location.href);
  223. }
  224. const GravatarURLRegExp = /\/\/cn.gravatar.com\/avatar\/(.*)$/;
  225. function changeGravatarURL() {
  226. var Gravatar = document.getElementsByClassName('img-thumbnail')[0];
  227. if (Gravatar && Gravatar.nodeType){
  228. console.log(Gravatar);
  229. Gravatar.setAttribute('src', '//gravatar.loli.net/avatar/' + Gravatar.getAttribute('src').match(GravatarURLRegExp)[1]);
  230. }
  231. var Gravatars = document.getElementsByClassName('img-rounded');
  232. for (var gravatar in Gravatars) {
  233. if (Gravatars[gravatar] && Gravatars[gravatar].nodeType && GravatarURLRegExp.test(Gravatars[gravatar].getAttribute('src'))) {
  234. Gravatars[gravatar].setAttribute('src', '//gravatar.loli.net/avatar/' + Gravatars[gravatar].getAttribute('src').match(GravatarURLRegExp)[1]);
  235. }
  236. }
  237. }
  238.  
  239. (function() {
  240. 'use strict';
  241. if (BackgroundSettings[0]) BackgroundImage();
  242. CodeBlock();
  243. NameColor();
  244. CCFBadge();
  245. TagBadge();
  246. TableStyle();
  247. if (isSubmission()) SubmissionCard();
  248. if (isProblem()) downloadData();
  249. if (isUserProfile() || isBlog()) changeGravatarURL();
  250. })();