CodeforcesRankParse

用于OJ的排名解析,传递至后台处理

  1. // ==UserScript==
  2. // @license MIT
  3. // @name CodeforcesRankParse
  4. // @namespace http://tampermonkey.net/
  5. // @version v0.1
  6. // @description 用于OJ的排名解析,传递至后台处理
  7. // @author chenyyy28
  8. // @match https://codeforces.com/*/standings/*
  9. // @match https://codeforces.com/*/standings
  10. // @match https://codeforc.es/*/standings/*
  11. // @match https://codeforc.es/*/standings
  12. // @grant GM_xmlhttpRequest
  13. // @grant GM_download
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. var button = document.createElement("button"); //创建一个按钮
  19. button.textContent = "解析排名"; //按钮内容
  20. //button.style.width = "50px"; //按钮宽度
  21. button.style.height = "17px"; //按钮高度
  22. button.style.align = "center"; //文本居中
  23. //button.style.background = "#e33e33"; //按钮底色
  24. button.style.border = "1px solid black"; //边框属性
  25. button.style.borderRadius = "4px"; //按钮四个角弧度
  26. button.style.cursor = "pointer"
  27. button.addEventListener("click", getRank) //监听按钮点击事件
  28.  
  29. let testNode = document.createElement("div")
  30. testNode.style.height = "100%"
  31. testNode.style.backgroundColor = "green"
  32. testNode.innerHTML = "<h1>测试标题</h1>"
  33. function getRank(){
  34. let standings = document.querySelector(".standings tbody")
  35. let problemNum = document.querySelector(".standings tr:nth-child(1)").children.length-4
  36. let rank = []
  37. let account = []
  38. let accept = []
  39. let accountMap = new Map()
  40. for(let i = 1;i<standings.children.length-1;i++){
  41. let row = standings.children[i]
  42. rank.push(parseInt(row.querySelector("td:nth-child(1)").innerText))
  43. account.push(row.querySelector("td:nth-child(2) a").innerText)
  44. accept.push(parseInt(row.querySelector("td:nth-child(3)").innerText))
  45. }
  46. let participantNum = 0
  47. for(let i = 0;i<rank.length;i++){
  48. if(typeof rank[i] === 'number' && !isNaN(rank[i])){
  49. participantNum++
  50. }else {
  51. break
  52. }
  53. }
  54. let standingDetails = new Map()
  55. let supplementNum = new Map()
  56. let supplement = []
  57. for(let i = 1;i <= participantNum;i++){
  58. let row = standings.children[i]
  59. accountMap.set(account[i-1],1)
  60. let nowSolve = 0;
  61. for(let j = 5;j<=4+problemNum;j++){
  62. let cssSelector = "td:nth-child("+j.toString()+")"
  63. let text =row.querySelector(cssSelector).innerText
  64. if(text.includes("+")){
  65. nowSolve = nowSolve+(1<<(j-5))
  66. }
  67. }
  68. standingDetails.set(account[i-1],nowSolve)
  69. }
  70. for(let i = participantNum+1;i < standings.children.length-1;i++){
  71. let row = standings.children[i]
  72. let weight = standingDetails.get(account[i-1])
  73. let res = 0
  74. if(weight==undefined){
  75. weight = 0
  76. }
  77. for(let j = 5;j<=4+problemNum;j++){
  78. let cssSelector = "td:nth-child("+j.toString()+")"
  79. let text =row.querySelector(cssSelector).innerText
  80. if(text.includes("+")&&((weight&(1<<(j-5)))==0)){
  81. res++
  82. }
  83. }
  84.  
  85. supplementNum.set(account[i-1],res)
  86. }
  87. for(let i = 0;i<participantNum;i++){
  88. let num = supplementNum.get(account[i])
  89. if(num==undefined){
  90. supplement.push(0)
  91. }else{
  92. supplement.push(num)
  93. }
  94. }
  95.  
  96. let msg = "本次获取结果:\n"
  97. let res = []
  98. for(let i = 0;i<participantNum;i++){
  99. let addMsg = account[i]+"\t:排名 "+rank[i].toString()+ "\t过题数 "+accept[i]+ "\t补题数 " +supplement[i]
  100. msg = msg+addMsg+"\n"
  101. let el = {
  102. account: account[i],
  103. rank: rank[i],
  104. accept: accept[i],
  105. supplement: supplement[i]
  106. }
  107. res.push(el)
  108. }
  109. let cnt = participantNum+1
  110. for(let i = participantNum;i<account.length;i++){
  111. if(accountMap.get(account[i])==undefined){
  112. let addMsg = account[i]+"\t:排名 "+cnt+ "\t过题数 "+0+ "\t补题数 " +accept[i]
  113. msg = msg+addMsg+"\n"
  114. let el = {
  115. account:account[i],
  116. rank:cnt,
  117. accept:0,
  118. supplement:accept[i]
  119. }
  120. res.push(el)
  121. cnt++;
  122. }
  123. }
  124. console.log(res)
  125. msg = msg + "请输入本次训练id\n 如果不清楚id,可以在管理系统查询"
  126. let id = prompt(msg)
  127. if(id!=null&&id!=""){
  128. let url = prompt("请输入数据目标地址")
  129. let req = {
  130. id: id,
  131. platform: "codeforces",
  132. data:res
  133. }
  134. console.log(JSON.stringify(req))
  135. GM_xmlhttpRequest({
  136. method: "POST",
  137. url: url,
  138. headers: {
  139. "Content-Type": "application/json"
  140. },
  141. data:JSON.stringify(req),
  142. onload: function(response){
  143. console.log("请求成功");
  144. console.log(response.responseText);
  145. },
  146. onerror: function(response){
  147. console.log(response);
  148. }
  149. });
  150. }
  151. }
  152. var like_comment = document.querySelector('.datatable div:nth-child(5)'); //getElementsByClassName 返回的是数组,所以要用[] 下标
  153. like_comment.appendChild(button); //把按钮加入到 x 的子节点中
  154.  
  155.  
  156. })();