IITU Contester tools

Customize your contester

  1. // ==UserScript==
  2. // @name IITU Contester tools
  3. // @namespace https://greasyfork.org/ru/users/77226
  4. // @version 0.4.3
  5. // @description Customize your contester
  6. // @author Diasonti
  7. // @match http://contester.iitu.kz/*
  8. // @license Creative Commons; http://creativecommons.org/licenses/by/4.0/
  9. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
  10. // @require https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
  11. // @require https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/highlight.min.js
  12. // @require https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/languages/cpp.min.js
  13. // @resource highlightJsCss https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.8.0/styles/atom-one-light.min.css
  14. // @grant GM_addStyle
  15. // @grant GM_getResourceText
  16. // ==/UserScript==
  17. //-----------------GM_set/getValue fix------------------------------------------------------------------
  18. if (!this.GM_getValue || (this.GM_getValue.toString && this.GM_getValue.toString().indexOf('not supported') > - 1)) {
  19. this.GM_getValue = function (key, def) {
  20. return localStorage[key] || def;
  21. };
  22. this.GM_setValue = function (key, value) {
  23. localStorage[key] = value;
  24. };
  25. this.GM_deleteValue = function (key) {
  26. return delete localStorage[key];
  27. };
  28. } //----------------Global Variables-------------------------------------------------------------------
  29.  
  30. var defaultCompiler = '' + GM_getValue('ctdc', 'cpp');
  31. var defaultSubmType = '' + GM_getValue('ctdsb', 'text');
  32. var defaultStudent = '' + GM_getValue('ctdst', 'Admin');
  33. var enableSH = GM_getValue('ctsh', 1);
  34. /*
  35. console.log("Defaults loaded");
  36. console.log("defaultCompiler: " + defaultCompiler);
  37. console.log("defaultSubmType: " + defaultSubmType);
  38. console.log("defaultStudent: " + defaultStudent);
  39. console.log("enableSH: " + enableSH);
  40. */
  41. //-------------------------MAIN----------------------------------------------------------------------
  42. GM_addStyle(GM_getResourceText('highlightJsCss'));
  43. hljs.configure({
  44. tabReplace: ' ',
  45. useBR: true,
  46. languages: [
  47. 'cpp'
  48. ]
  49. });
  50. $(document).ready(function () {
  51. initSettingsBlock();
  52. initSettingsButton();
  53. $('button#save').click(function () {
  54. saveSettings();
  55. });
  56. $('button#cancel').click(function () {
  57. $('div#ctsetb').show();
  58. $('div#ctsettings').hide();
  59. });
  60. $('div#ctsetb').click(function () {
  61. $('div#ctsetb').hide();
  62. $('div#ctsettings').show();
  63. });
  64. $('body').click(function () {
  65. setStudent(defaultStudent);
  66. setSubmType(defaultSubmType);
  67. setComp(defaultCompiler);
  68. if (defaultCompiler == 'cpp') {
  69. highlightCode();
  70. }
  71. });
  72. /*
  73. setTimeout(function(){
  74. console.log("TIMER");
  75. setSubmType(defaultSubmType);
  76. setComp(defaultCompiler);
  77. }, 200);
  78. */
  79. });
  80. //----------------------------FUNCTIONS-------------------------------------------------------------
  81. function setSubmType(st) { //----SET SUBMISSION TYPE
  82. if (st == 'file') {
  83. //show('m_code_as_file');
  84. document.getElementById('m_code_as_file').style = '';
  85. //hide('m_code_as_text');
  86. document.getElementById('m_code_as_text').style = 'display:none;';
  87. //obj('m_take').value = 'file'; //to file
  88. document.getElementById('m_take').value = 'file';
  89. }
  90. else if (st == 'text') {
  91. //show('m_code_as_text');
  92. document.getElementById('m_code_as_text').style = '';
  93. //hide('m_code_as_file');
  94. document.getElementById('m_code_as_file').style = 'display:none;';
  95. //obj('m_take').value = 'text'; // to text
  96. document.getElementById('m_take').value = 'text';
  97. } //console.log('Submtype set: ' + st);
  98.  
  99. return;
  100. }
  101. function setComp(dc) { //----SET COMPILER
  102. if (dc == 'cpp') {
  103. $('#m_acompiler option:contains("C++ (C++)")').attr('selected', true);
  104. }
  105. else if (dc === 'pascal') {
  106. $('#m_acompiler option:contains("Pascal (Free Pascal 2.6.0)")').attr('selected', true);
  107. } //console.log('Compiler set: ' + dc);
  108.  
  109. return;
  110. }
  111. function setStudent(ds) { //-------SET STUDENT
  112. $('#sgr_uid option:contains(' + ds + ')').attr('selected', true);
  113. $('#m_uid option:contains(' + ds + ')').attr('selected', true);
  114. //console.log('Student set: ' + ds);
  115. return;
  116. }
  117. function highlightCode() { //----------HIGHLIGHT CODE
  118. if (document.getElementsByTagName('nobr') [0] !== undefined && enableSH == 1) {
  119. for (var j = 0; j < document.getElementsByTagName('nobr').length; j++) {
  120. var pre = document.createElement('pre');
  121. var newcode = document.createElement('code');
  122. var code = document.getElementsByTagName('nobr') [j];
  123. pre.appendChild(newcode);
  124. code.parentElement.appendChild(pre);
  125. newcode.appendChild(code);
  126. $('pre code nobr').each(function (i, block) {
  127. hljs.highlightBlock(block);
  128. });
  129. $('pre').css({
  130. 'margin': '0'
  131. });
  132. $('code').css({
  133. 'padding': '0'
  134. });
  135. $('nobr').css({
  136. 'padding': '0',
  137. 'background': 'none'
  138. });
  139. pre.parentElement.parentElement.parentElement.parentElement.parentElement.style = pre.parentElement.parentElement.parentElement.parentElement.parentElement.style + 'width:100%;';
  140. //console.log('Code Highlighted');
  141. }
  142. }
  143. }
  144. function initSettingsBlock() { //----SETTINGS BLOCK
  145. var set = document.createElement('div');
  146. document.body.appendChild(set);
  147. set.id = 'ctsettings';
  148. $('div#ctsettings').css({
  149. 'position': 'fixed',
  150. 'bottom': '0',
  151. 'right': '0',
  152. 'height': '350px',
  153. 'width': '300px',
  154. 'background': 'white',
  155. 'border': '1px solid black'
  156. });
  157. var mainb = document.createElement('div');
  158. var footb = document.createElement('div');
  159. document.getElementById('ctsettings').appendChild(mainb);
  160. document.getElementById('ctsettings').appendChild(footb);
  161. mainb.id = 'mainb';
  162. footb.id = 'footb';
  163. $('div#mainb').css({
  164. 'height': '310px',
  165. 'padding': '7px'
  166. });
  167. $('div#footb').css({
  168. 'height': '40px',
  169. 'padding': '2px'
  170. });
  171. //----Main Section
  172. var header = document.createElement('p');
  173. document.getElementById('mainb').appendChild(header);
  174. header.id = 'ctheader';
  175. header.innerHTML = '<b>Contester Tools</b>';
  176. $('p#ctheader').css({
  177. 'margin': '2px'
  178. });
  179. var compilerLabel = document.createElement('p');
  180. document.getElementById('mainb').appendChild(compilerLabel);
  181. compilerLabel.id = 'ctclabel';
  182. compilerLabel.innerHTML = 'Default compiler: <br>';
  183. var compilerSelect = document.createElement('select');
  184. document.getElementById('mainb').appendChild(compilerSelect);
  185. compilerSelect.id = 'ctcsel';
  186. /*
  187. var defopt = document.createElement("option");
  188. document.getElementById("ctcsel").appendChild(defopt);
  189. defopt.id = "def";
  190. defopt.innerHTML = "def";
  191. $("option#def").attr("value","java");
  192. */
  193. var cppopt = document.createElement('option');
  194. document.getElementById('ctcsel').appendChild(cppopt);
  195. cppopt.id = 'cpp';
  196. cppopt.innerHTML = 'C++';
  197. $('option#cpp').attr('value', 'cpp');
  198. var pascalopt = document.createElement('option');
  199. document.getElementById('ctcsel').appendChild(pascalopt);
  200. pascalopt.id = 'pascal';
  201. pascalopt.innerHTML = 'Pascal';
  202. $('option#pascal').attr('value', 'pascal');
  203. $('option#' + defaultCompiler).attr('selected', true);
  204. var submLabel = document.createElement('p');
  205. document.getElementById('mainb').appendChild(submLabel);
  206. submLabel.id = 'ctsblabel';
  207. submLabel.innerHTML = 'Default submission type: <br>';
  208. var submSelect = document.createElement('select');
  209. document.getElementById('mainb').appendChild(submSelect);
  210. submSelect.id = 'ctsbsel';
  211. var textopt = document.createElement('option');
  212. document.getElementById('ctsbsel').appendChild(textopt);
  213. textopt.id = 'textopt';
  214. textopt.innerHTML = 'Text';
  215. $('option#textopt').attr('value', 'text');
  216. var fileopt = document.createElement('option');
  217. document.getElementById('ctsbsel').appendChild(fileopt);
  218. fileopt.id = 'fileopt';
  219. fileopt.innerHTML = 'File';
  220. $('option#fileopt').attr('value', 'file');
  221. $('option#' + defaultSubmType + 'opt').attr('selected', true);
  222. var studentLabel = document.createElement('p');
  223. document.getElementById('mainb').appendChild(studentLabel);
  224. studentLabel.id = 'ctslabel';
  225. studentLabel.innerHTML = 'Default student: <br>';
  226. if (document.getElementById('sgr_uid') !== null) {
  227. var studentsList = document.getElementById('sgr_uid');
  228. var studentSelect = studentsList.cloneNode(true);
  229. document.getElementById('mainb').appendChild(studentSelect);
  230. studentSelect.id = 'ctssel';
  231. $('#ctssel').removeAttr('name class onchange');
  232. $('#ctssel option:contains(' + defaultStudent + ')').attr('selected', true);
  233. }
  234. else {
  235. var studentl = document.createElement('select');
  236. document.getElementById('mainb').appendChild(studentl);
  237. studentl.id = 'studentl';
  238. var student = document.createElement('option');
  239. document.getElementById('studentl').appendChild(student);
  240. student.innerHTML = defaultStudent;
  241. $('select#studentl').attr('disabled', 'true');
  242. }
  243. var syntaxLabel = document.createElement('p');
  244. document.getElementById('mainb').appendChild(syntaxLabel);
  245. syntaxLabel.id = 'ctsynlabel';
  246. syntaxLabel.innerHTML = 'Syntax highlight: ';
  247. var syntaxCheckBox = document.createElement('input');
  248. document.getElementById('mainb').appendChild(syntaxCheckBox);
  249. syntaxCheckBox.id = 'ctsyncb';
  250. syntaxCheckBox.type = 'checkbox';
  251. var a;
  252. if (enableSH == 1) {
  253. a = true;
  254. }
  255. else {
  256. a = false;
  257. }
  258. syntaxCheckBox.checked = a;
  259. //----Foot Bar
  260. var savebutton = document.createElement('button');
  261. var cancelbutton = document.createElement('button');
  262. document.getElementById('footb').appendChild(savebutton);
  263. document.getElementById('footb').appendChild(cancelbutton);
  264. savebutton.id = 'save';
  265. cancelbutton.id = 'cancel';
  266. savebutton.innerHTML = 'Save';
  267. cancelbutton.innerHTML = 'Cancel';
  268. $('button#save').css({
  269. 'position': 'absolute',
  270. 'left': '7px',
  271. 'bottom': '4px'
  272. });
  273. $('button#cancel').css({
  274. 'position': 'absolute',
  275. 'right': '7px',
  276. 'bottom': '4px'
  277. });
  278. //-----Hide Block by default
  279. $('div#ctsettings').hide();
  280. return;
  281. }
  282. function initSettingsButton() { //----SETTINGS BUTTON
  283. var setB = document.createElement('div');
  284. var setlabel = document.createElement('p');
  285. document.body.appendChild(setB);
  286. setB.appendChild(setlabel);
  287. setlabel.innerHTML = 'CTools<br>Settings';
  288. setB.id = 'ctsetb';
  289. $('div#ctsetb p').css({
  290. 'cursor': 'pointer'
  291. });
  292. $('div#ctsetb').css({
  293. 'position': 'fixed',
  294. 'bottom': '0',
  295. 'right': '0',
  296. 'height': '50px',
  297. 'width': '50px',
  298. 'background': 'pink',
  299. 'border': '2px solid black',
  300. 'opacity': '0.5',
  301. 'cursor': 'pointer'
  302. });
  303. return;
  304. }
  305. function saveSettings() {
  306. defaultCompiler = document.getElementById('ctcsel').options[document.getElementById('ctcsel').selectedIndex].value;
  307. GM_setValue('ctdc', defaultCompiler);
  308. defaultSubmType = document.getElementById('ctsbsel').options[document.getElementById('ctsbsel').selectedIndex].value;
  309. GM_setValue('ctdsb', defaultSubmType);
  310. if (document.getElementById('ctsyncb').checked) {
  311. enableSH = 1;
  312. }
  313. else {
  314. enableSH = 0;
  315. }
  316. GM_setValue('ctsh', enableSH);
  317. if (document.getElementById('ctssel') !== null) {
  318. defaultStudent = document.getElementById('ctssel').options[document.getElementById('ctssel').selectedIndex].innerHTML;
  319. GM_setValue('ctdst', defaultStudent);
  320. }
  321. return;
  322. }