ex124OJ

Extend 124OJ!

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

  1. // ==UserScript==
  2. // @name ex124OJ
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.6
  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 = [];
  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. if (BackgroundSettings[0]) {
  37. 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;">';
  38. document.getElementsByClassName('container')[0].children[0].children[1].innerHTML = document.getElementsByClassName('container')[0].children[0].children[1].innerHTML.match(/(.*> ){0,1}(.*)/)[2];
  39. GM_addStyle('\
  40. body {\
  41. background: url("' + getBackground() + '");\
  42. background-repeat: no-repeat;\
  43. background-attachment: fixed;\
  44. background-position: 50% 50%;\
  45. background-size: cover;\
  46. }\
  47. .uoj-content {\
  48. background-color: #fff;\
  49. margin: 16px -16px;\
  50. padding: 16px 16px;\
  51. opacity: ' + getOpacity() + ';\
  52. border-radius: 8px;\
  53. }\
  54. .navbar {\
  55. margin: 16px -16px;\
  56. padding: 8px 16px;\
  57. opacity: ' + getOpacity() + ';\
  58. border-radius: 8px;\
  59. }');
  60. }
  61. }
  62.  
  63. function changeIcon() {
  64. var Links = document.getElementsByTagName('link');
  65. for (var link in Links) {
  66. if (Links[link] && Links[link].nodeType && Links[link].getAttribute('rel') == 'shortcut icon') {
  67. Links[link].setAttribute('href', 'http://124.221.194.184/images/logo_small.png');
  68. }
  69. }
  70. }
  71.  
  72. function CodeBlock() {
  73. GM_addStyle('\
  74. @import url(https://cdn.jsdelivr.net/npm/firacode@6.2.0/distr/fira_code.css);\
  75. code {\
  76. font-family: "Fira Code";\
  77. }\
  78. code.sh_cpp>span {\
  79. font-style: normal !important;\
  80. font-weight: 400 !important;\
  81. }\
  82. code.sh_cpp>span.sh_preproc,\
  83. code.sh_cpp>span.sh_keyword,\
  84. code.sh_cpp>span.sh_type {\
  85. color: #8959a8;\
  86. }\
  87. code.sh_cpp>span.sh_string {\
  88. color: #718c00;\
  89. }\
  90. code.sh_cpp>span.sh_cbracket {\
  91. color: #4d4d4c;\
  92. }\
  93. code.sh_cpp>span.sh_symbol {\
  94. color: #3e999f;\
  95. }\
  96. code.sh_cpp>span.sh_number {\
  97. color: #f5871f;\
  98. }\
  99. code.sh_cpp>span.sh_function {\
  100. color: #4271ae;\
  101. }\
  102. code.sh_cpp>span.sh_comment {\
  103. color: #8e908c;\
  104. }');
  105. }
  106.  
  107. function NameColor() {
  108. var Names = document.getElementsByClassName('uoj-username');
  109. var NeedsRepeat = false;
  110. for (var i in Names) {
  111. if (Names[i].innerHTML && NameColorList[Names[i].innerHTML]) {
  112. Names[i].style = 'color:' + NameColorList[Names[i].innerHTML];
  113. if (Names[i].parentElement.getAttribute('class') == 'legendLabel') {
  114. NeedsRepeat = true;
  115. }
  116. }
  117. }
  118. if (NeedsRepeat) {
  119. setInterval(function(){
  120. var Names = document.getElementsByClassName('uoj-username');
  121. for (var i in Names) {
  122. if (Names[i].innerHTML && Names[i].parentElement.getAttribute('class') == 'legendLabel' && NameColorList[Names[i].innerHTML]) {
  123. Names[i].style = 'color:' + NameColorList[Names[i].innerHTML];
  124. }
  125. }
  126. }, 200);
  127. }
  128. var Honors = document.getElementsByClassName('uoj-honor');
  129. for (var j in Honors) {
  130. if (Honors[j].innerHTML && NameColorList[Honors[j].innerHTML]) {
  131. Honors[j].style = "color:" + NameColorList[Honors[j].innerHTML];
  132. }
  133. }
  134. }
  135.  
  136. const NameWithBadge = /^(.{1,}?)( <.*){0,1}$/;
  137. function CCFBadge() {
  138. var Names = document.getElementsByClassName('uoj-username');
  139. for (var i in Names) {
  140. if (!Names[i].innerHTML || !NameWithBadge.test(Names[i].innerHTML)) continue;
  141. var name = Names[i].innerHTML.match(NameWithBadge)[1];
  142. if (CCFBadgeList[name] && Names[i].parentElement.getAttribute('class') != 'legendLabel') {
  143. 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>';
  144. }
  145. }
  146. var Honors = document.getElementsByClassName('uoj-honor');
  147. for (var j in Honors) {
  148. if (!Honors[j].innerHTML || !NameWithBadge.test(Honors[j].innerHTML)) continue;
  149. var honor = Honors[j].innerHTML.match(NameWithBadge)[1];
  150. if (CCFBadgeList[honor]) {
  151. 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>';
  152. }
  153. }
  154. }
  155. function TagBadge() {
  156. var Names = document.getElementsByClassName('uoj-username');
  157. for (var i in Names) {
  158. if (!Names[i].innerHTML || !NameWithBadge.test(Names[i].innerHTML)) continue;
  159. var name = Names[i].innerHTML.match(NameWithBadge)[1];
  160. if (TagBadgeList[name] && Names[i].parentElement.getAttribute('class') != 'legendLabel') {
  161. 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>';
  162. }
  163. }
  164. var Honors = document.getElementsByClassName('uoj-honor');
  165. for (var j in Honors) {
  166. if (!Honors[j].innerHTML || !NameWithBadge.test(Honors[j].innerHTML)) continue;
  167. var honor = Honors[j].innerHTML.match(NameWithBadge)[1];
  168. if (TagBadgeList[honor]) {
  169. 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>';
  170. }
  171. }
  172. }
  173.  
  174. function TableStyle() {
  175. var Tables = document.getElementsByTagName('table');
  176. for (var table in Tables) {
  177. if (Tables[table].nodeType && Tables[table].parentNode.getAttribute('class') != 'legend' && !Tables[table].classList.length) {
  178. Tables[table].classList.add('table', 'table-bordered');
  179. }
  180. }
  181. var ths = document.getElementsByTagName('th');
  182. for (var th in ths) {
  183. if (ths[th].nodeType && ths[th].getAttribute('align')) {
  184. ths[th].setAttribute('style', 'text-align:' + ths[th].getAttribute('align'));
  185. }
  186. }
  187. }
  188.  
  189. const submissionRegExp = /^http:\/\/124.221.194.184\/submission\/\d{1,}/;
  190. function isSubmission() {
  191. return submissionRegExp.test(window.location.href);
  192. }
  193. function SubmissionCard() {
  194. setTimeout(function() {
  195. var Content = document.getElementsByClassName('uoj-content')[0];
  196. if (Content.childElementCount == 3) {
  197. var Table = Content.children[0];
  198. var TabList = document.createElement('ul');
  199. TabList.setAttribute('class', 'nav nav-tabs');
  200. TabList.setAttribute('role', 'tablist');
  201. 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>';
  202. var TabContent = document.createElement('div');
  203. TabContent.setAttribute('class', 'tab-content');
  204. 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>';
  205. Content.innerHTML = "";
  206. Content.appendChild(Table);
  207. Content.appendChild(TabList);
  208. Content.appendChild(TabContent);
  209. }
  210. }, 10);
  211. }
  212.  
  213. const ProblemRegExp = /^http:\/\/124.221.194.184.*\/problem\/(\d{1,})/;
  214. function isProblem() {
  215. return ProblemRegExp.test(window.location.href);
  216. }
  217. function downloadData() {
  218. var DownloadTag = document.createElement('a');
  219. DownloadTag.setAttribute('role', 'button');
  220. DownloadTag.setAttribute('class', 'btn btn-primary float-right');
  221. DownloadTag.setAttribute('href', '/download.php?type=problem&id=' + window.location.href.match(ProblemRegExp)[1]);
  222. DownloadTag.setAttribute('target', '_blank');
  223. DownloadTag.innerHTML = '<span class="glyphicon glyphicon-download-alt"></span> 下载数据';
  224. document.getElementsByClassName("btn btn-info float-right")[0].after(DownloadTag);
  225. }
  226.  
  227. const UserProfileRegExp = /^http:\/\/124.221.194.184\/user\/profile\/.{1,}/;
  228. function isUserProfile() {
  229. return UserProfileRegExp.test(window.location.href);
  230. }
  231. const BlogRegExp = /^http:\/\/124.221.194.184\/blog\/.{1,}/;
  232. function isBlog() {
  233. return BlogRegExp.test(window.location.href);
  234. }
  235. const GravatarURLRegExp = /\/\/cn.gravatar.com\/avatar\/(.*)$/;
  236. function changeGravatarURL() {
  237. var Gravatar = document.getElementsByClassName('img-thumbnail')[0];
  238. if (Gravatar && Gravatar.nodeType){
  239. console.log(Gravatar);
  240. Gravatar.setAttribute('src', '//gravatar.loli.net/avatar/' + Gravatar.getAttribute('src').match(GravatarURLRegExp)[1]);
  241. }
  242. var Gravatars = document.getElementsByClassName('img-rounded');
  243. for (var gravatar in Gravatars) {
  244. if (Gravatars[gravatar] && Gravatars[gravatar].nodeType && GravatarURLRegExp.test(Gravatars[gravatar].getAttribute('src'))) {
  245. Gravatars[gravatar].setAttribute('src', '//gravatar.loli.net/avatar/' + Gravatars[gravatar].getAttribute('src').match(GravatarURLRegExp)[1]);
  246. }
  247. }
  248. }
  249.  
  250. (function() {
  251. 'use strict';
  252. BackgroundImage();
  253. changeIcon();
  254. CodeBlock();
  255. NameColor();
  256. CCFBadge();
  257. TagBadge();
  258. TableStyle();
  259. if (isSubmission()) SubmissionCard();
  260. if (isProblem()) downloadData();
  261. if (isUserProfile() || isBlog()) changeGravatarURL();
  262. })();