ex124OJ

Extend 124OJ!

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

  1. // ==UserScript==
  2. // @name ex124OJ
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.5.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 https://www.imageoss.com/images/2022/11/29/ex124OJba563861978a769d.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','zsq147258369':'#52c41a','zhouchuan':'#b0e0e6'};
  21. // Add CCF level badge.
  22. const CCFBadgeList = {'zsq147258369':'#52c41a'};
  23. // Add name tag badge.
  24. const TagBadgeList = {'Sukwants':['#8e44ad','旅客'],'Star32':['#e74c3c','陌生人'],'2745518585':['#996600','作弊者'],'syysongyuyang':['#e67e22','原批'],'zsq147258369':['#52c41a','boring'],'zhouchuan':['#b0e0e6','Feyn']};
  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. const ManageStatementExp = /^.*\/manage\/statement(\?.*){0,1}$/
  73. function CodeBlock() {
  74. GM_addStyle('\
  75. @import url(https://cdn.jsdelivr.net/npm/firacode@6.2.0/distr/fira_code.css);\
  76. code {\
  77. font-family: "Fira Code";\
  78. }\
  79. code.sh_cpp>span {\
  80. font-style: normal !important;\
  81. font-weight: 400 !important;\
  82. }\
  83. code.sh_cpp>span.sh_preproc,\
  84. code.sh_cpp>span.sh_keyword,\
  85. code.sh_cpp>span.sh_type {\
  86. color: #8959a8;\
  87. }\
  88. code.sh_cpp>span.sh_string {\
  89. color: #718c00;\
  90. }\
  91. code.sh_cpp>span.sh_cbracket {\
  92. color: #4d4d4c;\
  93. }\
  94. code.sh_cpp>span.sh_symbol {\
  95. color: #3e999f;\
  96. }\
  97. code.sh_cpp>span.sh_number {\
  98. color: #f5871f;\
  99. }\
  100. code.sh_cpp>span.sh_function {\
  101. color: #4271ae;\
  102. }\
  103. code.sh_cpp>span.sh_comment {\
  104. color: #8e908c;\
  105. }\
  106. .copybutton {\
  107. float: right;\
  108. background-color: rgb(0,0,0,.1);\
  109. padding: .25em .625em;\
  110. border: 0 solid transparent;\
  111. border-radius: .28571429rem;\
  112. }\
  113. .copybutton:hover {\
  114. background-color: rgb(0,0,0,.2);\
  115. }\
  116. .copybutton:focus {\
  117. background-color: rgb(0,0,0,.2);\
  118. outline: none;\
  119. }');
  120. if (!ManageStatementExp.test(window.location.href)) {
  121. setTimeout(function(){
  122. var CodeBlocks = document.getElementsByTagName('pre');
  123. for (var cb in CodeBlocks) {
  124. if (CodeBlocks[cb] && CodeBlocks[cb].nodeType) {
  125. var Content = CodeBlocks[cb].textContent;
  126. var InputId = Math.round(Math.random() * 998244353);
  127. var ButtonId = Math.round(Math.random() * 998244353);
  128. CodeBlocks[cb].innerHTML = '<button class="copybutton" id = "' + ButtonId + '" onclick="\
  129. var ib = document.getElementById(\'' + InputId + '\');\
  130. ib.setAttribute(\'style\', \'display:initial\');\
  131. ib.value = \'' + Content.replace(/\\/g, '\\\\').replace(/\n/g, '\\n').replace(/'/g, '\\&#39;').replace(/"/g, '&#34;') + '\';\
  132. ib.select();\
  133. document.execCommand(\'copy\');\
  134. ib.setAttribute(\'style\', \'display:none\');\
  135. var ic = document.getElementById(\'' + ButtonId + '\');\
  136. ic.innerHTML = \'已复制\';\
  137. setTimeout(function() { ic.innerHTML = \'复制\'; }, 500);">复制</button>' + CodeBlocks[cb].innerHTML;
  138. var InputBox = document.createElement('textarea');
  139. InputBox.setAttribute('id', InputId);
  140. InputBox.setAttribute('style', 'display:none');
  141. CodeBlocks[cb].appendChild(InputBox);
  142. }
  143. }
  144. }, 30);
  145. }
  146. }
  147.  
  148. function NameColor() {
  149. var Names = document.getElementsByClassName('uoj-username');
  150. var NeedsRepeat = false;
  151. for (var i in Names) {
  152. if (Names[i].innerHTML && NameColorList[Names[i].innerHTML]) {
  153. Names[i].style = 'color:' + NameColorList[Names[i].innerHTML];
  154. if (Names[i].parentElement.getAttribute('class') == 'legendLabel') {
  155. NeedsRepeat = true;
  156. }
  157. }
  158. }
  159. if (NeedsRepeat) {
  160. setInterval(function(){
  161. var Names = document.getElementsByClassName('uoj-username');
  162. for (var i in Names) {
  163. if (Names[i].innerHTML && Names[i].parentElement.getAttribute('class') == 'legendLabel' && NameColorList[Names[i].innerHTML]) {
  164. Names[i].style = 'color:' + NameColorList[Names[i].innerHTML];
  165. }
  166. }
  167. }, 200);
  168. }
  169. var Honors = document.getElementsByClassName('uoj-honor');
  170. for (var j in Honors) {
  171. if (Honors[j].innerHTML && NameColorList[Honors[j].innerHTML]) {
  172. Honors[j].style = "color:" + NameColorList[Honors[j].innerHTML];
  173. }
  174. }
  175. }
  176.  
  177. const NameWithBadge = /^(.{1,}?)( <.*){0,1}$/;
  178. function CCFBadge() {
  179. var Names = document.getElementsByClassName('uoj-username');
  180. for (var i in Names) {
  181. if (!Names[i].innerHTML || !NameWithBadge.test(Names[i].innerHTML)) continue;
  182. var name = Names[i].innerHTML.match(NameWithBadge)[1];
  183. if (CCFBadgeList[name] && Names[i].parentElement.getAttribute('class') != 'legendLabel') {
  184. 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>';
  185. }
  186. }
  187. var Honors = document.getElementsByClassName('uoj-honor');
  188. for (var j in Honors) {
  189. if (!Honors[j].innerHTML || !NameWithBadge.test(Honors[j].innerHTML)) continue;
  190. var honor = Honors[j].innerHTML.match(NameWithBadge)[1];
  191. if (CCFBadgeList[honor]) {
  192. 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>';
  193. }
  194. }
  195. }
  196. function TagBadge() {
  197. var Names = document.getElementsByClassName('uoj-username');
  198. for (var i in Names) {
  199. if (!Names[i].innerHTML || !NameWithBadge.test(Names[i].innerHTML)) continue;
  200. var name = Names[i].innerHTML.match(NameWithBadge)[1];
  201. if (TagBadgeList[name] && Names[i].parentElement.getAttribute('class') != 'legendLabel') {
  202. 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>';
  203. }
  204. }
  205. var Honors = document.getElementsByClassName('uoj-honor');
  206. for (var j in Honors) {
  207. if (!Honors[j].innerHTML || !NameWithBadge.test(Honors[j].innerHTML)) continue;
  208. var honor = Honors[j].innerHTML.match(NameWithBadge)[1];
  209. if (TagBadgeList[honor]) {
  210. 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>';
  211. }
  212. }
  213. }
  214.  
  215. function TableStyle() {
  216. var Tables = document.getElementsByTagName('table');
  217. for (var table in Tables) {
  218. if (Tables[table].nodeType && Tables[table].parentNode.getAttribute('class') != 'legend' && !Tables[table].classList.length) {
  219. Tables[table].classList.add('table', 'table-bordered');
  220. }
  221. }
  222. var ths = document.getElementsByTagName('th');
  223. for (var th in ths) {
  224. if (ths[th].nodeType && ths[th].getAttribute('align')) {
  225. ths[th].setAttribute('style', 'text-align:' + ths[th].getAttribute('align'));
  226. }
  227. }
  228. }
  229.  
  230. const submissionRegExp = /^http:\/\/124.221.194.184\/submission\/\d{1,}/;
  231. function isSubmission() {
  232. return submissionRegExp.test(window.location.href);
  233. }
  234. function SubmissionCard() {
  235. setTimeout(function() {
  236. var Content = document.getElementsByClassName('uoj-content')[0];
  237. if (Content.childElementCount == 3) {
  238. var Table = Content.children[0];
  239. var TabList = document.createElement('ul');
  240. TabList.setAttribute('class', 'nav nav-tabs');
  241. TabList.setAttribute('role', 'tablist');
  242. 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>';
  243. var TabContent = document.createElement('div');
  244. TabContent.setAttribute('class', 'tab-content');
  245. 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>';
  246. Content.innerHTML = "";
  247. Content.appendChild(Table);
  248. Content.appendChild(TabList);
  249. Content.appendChild(TabContent);
  250. }
  251. }, 50);
  252. }
  253.  
  254. const ProblemRegExp = /^http:\/\/124.221.194.184.*\/problem\/(\d{1,})(\?.*){0,1}$/;
  255. function isProblem() {
  256. return ProblemRegExp.test(window.location.href);
  257. }
  258. function downloadData() {
  259. var DownloadTag = document.createElement('a');
  260. DownloadTag.setAttribute('role', 'button');
  261. DownloadTag.setAttribute('class', 'btn btn-primary float-right');
  262. DownloadTag.setAttribute('href', '/download.php?type=problem&id=' + window.location.href.match(ProblemRegExp)[1]);
  263. DownloadTag.setAttribute('target', '_blank');
  264. DownloadTag.innerHTML = '<span class="glyphicon glyphicon-download-alt"></span> 下载数据';
  265. document.getElementsByClassName("btn btn-info float-right")[0].after(DownloadTag);
  266. }
  267.  
  268. const UserProfileRegExp = /^http:\/\/124.221.194.184\/user\/profile\/.{1,}/;
  269. function isUserProfile() {
  270. return UserProfileRegExp.test(window.location.href);
  271. }
  272. const BlogRegExp = /^http:\/\/124.221.194.184\/blog\/.{1,}/;
  273. function isBlog() {
  274. return BlogRegExp.test(window.location.href);
  275. }
  276. const GravatarURLRegExp = /\/\/cn.gravatar.com\/avatar\/(.*)$/;
  277. function changeGravatarURL() {
  278. var Gravatar = document.getElementsByClassName('img-thumbnail')[0];
  279. if (Gravatar && Gravatar.nodeType){
  280. console.log(Gravatar);
  281. Gravatar.setAttribute('src', '//gravatar.loli.net/avatar/' + Gravatar.getAttribute('src').match(GravatarURLRegExp)[1]);
  282. }
  283. var Gravatars = document.getElementsByClassName('img-rounded');
  284. for (var gravatar in Gravatars) {
  285. if (Gravatars[gravatar] && Gravatars[gravatar].nodeType && GravatarURLRegExp.test(Gravatars[gravatar].getAttribute('src'))) {
  286. Gravatars[gravatar].setAttribute('src', '//gravatar.loli.net/avatar/' + Gravatars[gravatar].getAttribute('src').match(GravatarURLRegExp)[1]);
  287. }
  288. }
  289. }
  290.  
  291. (function() {
  292. 'use strict';
  293. BackgroundImage();
  294. changeIcon();
  295. CodeBlock();
  296. NameColor();
  297. CCFBadge();
  298. TagBadge();
  299. TableStyle();
  300. if (isSubmission()) SubmissionCard();
  301. if (isProblem()) downloadData();
  302. if (isUserProfile() || isBlog()) changeGravatarURL();
  303. })();