BIT-Programming-Detect

通过提交C语言程序,逐字符确定测试用例。为符合编程人员的习惯,字符默认从第0个开始。文本框中输入Enter键可以直接开始探测。在结束文本框输入非数字时,会持续探测。探测到所有的测试用例都OF时探测会结束。支持的测试用例字符:ASCII -1, 8-13, 32-126。

安裝腳本?
作者推薦腳本

您可能也會喜歡 BIT-Programming-Refresh result

安裝腳本
  1. // ==UserScript==
  2. // @name BIT-Programming-Detect
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.2
  5. // @description 通过提交C语言程序,逐字符确定测试用例。为符合编程人员的习惯,字符默认从第0个开始。文本框中输入Enter键可以直接开始探测。在结束文本框输入非数字时,会持续探测。探测到所有的测试用例都OF时探测会结束。支持的测试用例字符:ASCII -1, 8-13, 32-126。
  6. // @license GPL-3.0-or-later
  7. // @supportURL https://github.com/YDX-2147483647/BIT-enhanced/issues
  8. // @author CJJ (https://github.com/CJJ-amateur-programmer/Detect_BIT_OJ_getchar)
  9. // @author JWJ, python “getchar” version
  10. // @author Y.D.X., original python version
  11. // @match https://lexue.bit.edu.cn/mod/programming/*.php?id=*
  12. // @grant GM_registerMenuCommand
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. 'use strict'
  17. /* global GM_registerMenuCommand */
  18.  
  19. /** 提示“正在探测”的顶部提示栏 */
  20. const reminder = document.createElement('b')
  21. /** 弹出窗口的灰色背景 */
  22. const popup_cover = document.createElement('div')
  23. /** 弹出窗口 */
  24. const popup = document.createElement('div')
  25. /** 鼠标点击到弹窗上边沿时的X位置 */
  26. let mousestartX
  27. /** 鼠标点击到弹窗上边沿时的Y位置 */
  28. let mousestartY
  29. /** 提交的表单数据,包括页面id、代码内容等 */
  30. let fm
  31. /** 弹窗跟随鼠标移动 */
  32. const movePopup = (e) => {
  33. popup.style.left = (e.clientX - mousestartX) + 'px'
  34. popup.style.top = (e.clientY - mousestartY) + 'px'
  35. }
  36. /** 停止弹窗随鼠标移动 */
  37. const stopMove = () => {
  38. document.removeEventListener('mousemove', movePopup)
  39. document.removeEventListener('mouseup', stopMove)
  40. }
  41. /** 防止在探测时页面刷新 */
  42. const preventClosing = (e) => {
  43. e.preventDefault()
  44. }
  45. /** 关闭弹窗 */
  46. function closePopup () {
  47. document.body.removeChild(popup_cover)
  48. document.body.removeChild(popup)
  49. }
  50. /** 显示消息,然后隐藏顶部提示 */
  51. async function hideReminder (msg) {
  52. if (msg) {
  53. reminder.innerHTML = msg
  54. }
  55. await new Promise(resolve => setTimeout(resolve, 2000))
  56. reminder.style.opacity = '0'
  57. await new Promise(resolve => setTimeout(resolve, 300))
  58. document.body.removeChild(reminder)
  59. window.removeEventListener('beforeunload', preventClosing)
  60. }
  61. function downloadTXT (content, filename) {
  62. const downloadLink = document.createElement('a')
  63. // 创建txt
  64. downloadLink.href = URL.createObjectURL(new Blob([content], { type: 'text/plain' }))
  65. // 命名txt
  66. downloadLink.download = filename
  67. downloadLink.click()
  68. }
  69. /**
  70. * 将测试输入打印成人类易读的形式
  71. * @param arguments_ 记录了所有测试输入的词典,格式为 {<编号>: [<测试输入每一位的ASCII>]}
  72. */
  73. function print_arguments (arguments_) {
  74. let result = ' No. |测试输入\n'
  75. for (const key in arguments_) {
  76. result += (' ' + key).slice(-4) + ' |' + String.fromCharCode(...arguments_[key])
  77. .replaceAll('\b', '\\b')
  78. .replaceAll('\t', '\\t')
  79. .replaceAll('\n', '\\n')
  80. .replaceAll('\v', '\\v')
  81. .replaceAll('\f', '\\f')
  82. .replaceAll('\r', '\\r') +
  83. '\n'
  84. }
  85. return result
  86. }
  87. function show_results (results) {
  88. popup_cover.style = 'width:100%;height:100%;background-color:rgba(0,0,0,0.6);position:fixed;inset:0px;z-index:2000'
  89. // 双击弹窗灰色背景时关闭它
  90. popup_cover.setAttribute('ondblclick', 'this.style.display="none";')
  91.  
  92. popup.style = 'background-color:white;color:black;box-shadow:rgb(153,153,153) 0px 0px 2px;transform:translate(-50%,-50%);position:fixed;border:3px solid rgba(0,0,0,0.6);font-size:16px;overflow:hidden;z-index:3000;left:50%;top:50%;width:40%;text-align:center;'
  93. popup.innerHTML = `<style>
  94. #popup_title{
  95. width:100%;
  96. height:40px;
  97. line-height:40px;
  98. box-sizing:border-box;
  99. background-color:rgb(255,77,64);
  100. color:rgb(255,255,255);
  101. font-weight:700;
  102. font-size:20px;
  103. cursor:move;
  104. -webkit-touch-callout:none;
  105. -webkit-user-select:none;
  106. -khtml-user-select:none;
  107. -moz-user-select:none;
  108. -ms-user-select:none;
  109. user-select:none;
  110. }
  111. #close_popup{
  112. text-decoration:none;
  113. color:rgb(255,255,255);
  114. position:absolute;
  115. right:10px;
  116. top:0px;
  117. font-size:25px;
  118. display:inline-block;
  119. cursor:pointer;
  120. }
  121. </style><div id="popup_title">探测结果
  122. <div id="close_popup">×</div>
  123. </div>
  124. <pre style="overflow:auto;text-align:left;max-height:80vh;">${results}</pre></div>`
  125. popup.querySelector('#popup_title').addEventListener('mousedown', e => {
  126. // 记录鼠标在弹出窗口的相对位置
  127. mousestartX = e.clientX - parseInt(window.getComputedStyle(popup).getPropertyValue('left'))
  128. mousestartY = e.clientY - parseInt(window.getComputedStyle(popup).getPropertyValue('top'))
  129. // 鼠标移动时移动弹窗
  130. document.addEventListener('mousemove', movePopup)
  131. // 鼠标松开时结束移动
  132. document.addEventListener('mouseup', stopMove)
  133. })
  134. // 点击关闭按钮,关闭窗口
  135. popup.querySelector('#close_popup').onclick = () => closePopup()
  136.  
  137. document.body.append(popup_cover)
  138. document.body.append(popup)
  139. }
  140. /**
  141. * 探测保密测试用例
  142. * @param start 从第几个字符开始探测。如果不是数字,比如文本框留空,则当成0
  143. * @param end 探测到第几个字符为止。如果不是数字,比如文本框留空,则不断探测,直到全部都OF
  144. */
  145. async function detect (start, end) {
  146. start = start || 0 // 当start不是数字时重新赋值为0
  147. if (start > end || start < 0 || end < 0) { // 当end不是数字时表达式也是false
  148. alert('输入无效!')
  149. return
  150. }
  151. closePopup()
  152. window.addEventListener('beforeunload', preventClosing)// 防止探测时页面刷新
  153. document.body.append(reminder)
  154. reminder.style.opacity = '1'
  155. fm = await new Promise((resolve, reject) => { // 等待从submit.php获取表单数据
  156. const submit_xhr = new XMLHttpRequest()
  157. submit_xhr.open('GET', document.querySelector("[title='提交']").href, true)
  158. submit_xhr.onreadystatechange = async function () {
  159. if (submit_xhr.readyState === 4) { // 请求完成
  160. if (submit_xhr.status === 200) { // 请求正常,并且还能提交
  161. try {
  162. resolve(new FormData(new DOMParser().parseFromString(submit_xhr.responseText, 'text/html').querySelector('form[action="https://lexue.bit.edu.cn/mod/programming/submit.php"]')))
  163. } catch (error) { // 迟交的程序会找不到表单数据
  164. hideReminder('时间已到,您不能再提交程序了!')
  165. reject(Error('FormData Error:' + error.message))
  166. }
  167. } else {
  168. hideReminder('网络错误!')
  169. reject(Error('Network Error'))
  170. }
  171. }
  172. }
  173. submit_xhr.send()
  174. })
  175. /**
  176. * ```
  177. * arguments_ = {
  178. * '3': [65, 12],
  179. * '4': [5, 7, 534, 1, 543, 3, 2, 4, 6, 12, 3, 45, 3, 2, 13, 22, 1, 33, 56],
  180. * '5': [4, 5, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 2, 3, 4, 5, 1, 3, 4]
  181. * }
  182. * ```
  183. * 变量arguments_将会变成类似的格式,冒号前面的代表题号,后面的数组代表每一位字符的ASCII数值。
  184. */
  185. const arguments_ = {}
  186. let not_all_OF = true
  187. let i = start
  188. for (; (isNaN(end) || i <= end) && not_all_OF; i++) {
  189. reminder.innerHTML = `正在探测第${i}个字符<button style="float:right;" onclick="this.parentNode.innerHTML='正在停止'">停止</button>`
  190. // 以下生成C语言代码:
  191. // delay函数用于拖延时间。程序的正常输入输出耗时误差只出现在运行时间的最后一位,因此最后一位舍弃,只拖延前两位的时间;
  192. // ASCII -1意味着OF,返回1/0报“FPE”来读取;
  193. // ASCII 32-126能够正常显示,统一减去32以显示在耗时的前两位上,对应0-94;
  194. // ASCII 8-13是特殊符号\b\t\n\v\f\r,保险起见也放进来,对应95-100,其中100一般会报“TLE”;如果无时间限制则会显示“1.001”左右的数字,同样可以读取。
  195. // 这样,常用的字符能够全部表示在运行结果页面上,类似于加密。
  196. fm.set('code', '#include<stdio.h>\n#include<time.h>\nvoid delay(int seconds){clock_t start = clock();clock_t lay=(clock_t)seconds*CLOCKS_PER_SEC/1000;while((clock()-start)<lay);}int main(){int x;long long i;for(i=0;i<' +
  197. i + ';i++)getchar();x=getchar()-32;if(x==-33)return 1/0;else if(x<-18&&x>-25)x+=119;delay(x*10);return 0;}')
  198. const xhr = new XMLHttpRequest()
  199. xhr.open('POST', 'https://lexue.bit.edu.cn/mod/programming/submit.php', true)
  200. xhr.send(fm)
  201. let result = await new Promise(resolve => {
  202. // 不断查看运行结果页面,直到“程序处理完毕”
  203. const int = setInterval(() => {
  204. const x = new XMLHttpRequest()
  205. x.open('GET', 'https://lexue.bit.edu.cn/mod/programming/result.php?id=' + fm.get('id'), true)
  206. x.onreadystatechange = function () {
  207. if (x.readyState === 4) {
  208. if (x.status === 200) {
  209. if (x.responseText.match('当前状态:程序已处理完毕。')) {
  210. clearInterval(int)
  211. return resolve(x.responseText)
  212. } else if (!x.responseText.match('当前状态:程序已提交,正等待编译。')) {
  213. clearInterval(int)
  214. reminder.innerHTML = '编译错误!'
  215. return resolve(false)
  216. }
  217. } else {
  218. clearInterval(int)
  219. reminder.innerHTML = '网络错误!'
  220. return resolve(false)
  221. }
  222. }
  223. }
  224. x.send()
  225. },
  226. // 1000意味着每1秒查看一次运行结果页面
  227. 1000)
  228. })
  229. if (!result) { // 出错
  230. const partial_content = document.querySelector('title').innerText + ' 保密测试用例(字符:' + start + '~' + (i - 1) + ')\n探测于' + new Date().toLocaleString() + '\n' + print_arguments(arguments_)// 写入txt的探测内容
  231. downloadTXT(partial_content, document.querySelector('title').innerText + ' 保密测试用例(字符:' + start + '~' + (i - 1) + ').txt')
  232. hideReminder()
  233. show_results(partial_content)
  234. return
  235. }
  236. const rows = new DOMParser().parseFromString(result, 'text/html').querySelectorAll('#test-result-detail tbody > tr'); let // 结果表格的所有行
  237. OF_count = 0 // OF的样例个数
  238. for (let r = 0; r < rows.length; r++) {
  239. const row_number = rows[r].querySelector("[class~='c0']").innerText; const // 测试用例编号
  240. error = rows[r].querySelector("[class~='c12']").innerText.split(':')[0]// 错误类型
  241. if (error === 'FPE') { // 1/0的情况,意味着已经OF了
  242. ++OF_count
  243. continue
  244. }
  245. if (rows[r].querySelector("[class~='c4']").innerText === '保密') {
  246. if (i === start) { // i是start,说明是第一次循环,需要将json初始化为空数组[]。
  247. arguments_[row_number] = []
  248. }
  249. /* 接下来对结果进行解密 */
  250. if (error === 'TLE') {
  251. // 对应的是ASCII 13 的\r
  252. arguments_[row_number][i] = 13
  253. continue
  254. }
  255. result = Math.floor(parseFloat(rows[r].querySelector("[class~='c8']").innerText) * 100 + 32)
  256. if (result > 126) {
  257. // ASCII 8-12
  258. result -= 119
  259. }
  260. arguments_[row_number][i] = result
  261. }
  262. }
  263. if (reminder.innerHTML === '正在停止') {
  264. break
  265. }
  266. if (OF_count === rows.length) { // 全部都OF了
  267. not_all_OF = false
  268. }
  269. }
  270. hideReminder('已探明全部参数。')
  271. let content
  272. if (not_all_OF) {
  273. content = document.querySelector('title').innerText + ' 保密测试用例(字符:' + start + '~' + i + ')\n探测于' + new Date().toLocaleString() + '\n' + print_arguments(arguments_)// 写入txt的探测内容
  274. downloadTXT(content, document.querySelector('title').innerText + ' 保密测试用例(字符:' + start + '~' + i + ').txt')
  275. } else {
  276. content = document.querySelector('title').innerText + ' 保密测试用例(字符:' + start + '~end)\n探测于' + new Date().toLocaleString() + '\n' + print_arguments(arguments_)// 写入txt的探测内容
  277. downloadTXT(content, document.querySelector('title').innerText + ' 保密测试用例(字符:' + start + '~end).txt')
  278. }
  279. show_results(content)
  280. }
  281.  
  282. GM_registerMenuCommand('获取保密测试用例', function () { // 脚本菜单
  283. reminder.style = 'width:100%;color:white;position:fixed;left:0;top:0;text-align:center;opacity:100%;background-color:rgb(255,127,127);font-size:2vh;line-height:150%;opacity:0;line-height:4vh;transition:opacity 0.3s;z-index:4000;'
  284. reminder.innerHTML = '准备开始探测'
  285. popup_cover.style = 'width:100%;height:100%;background-color:rgba(0,0,0,0.6);position:fixed;inset:0px;z-index:2000'
  286. popup_cover.setAttribute('ondblclick', 'this.style.display="none";')// 双击弹窗灰色背景时关闭它
  287. popup.style = 'background-color:white;color:black;box-shadow:rgb(153,153,153) 0px 0px 2px;transform:translate(-50%,-50%);position:fixed;border:3px solid rgba(0,0,0,0.6);font-size:16px;overflow:hidden;z-index:3000;left:50%;top:50%;width:40%;text-align:center;'
  288. popup.innerHTML = `<style>
  289. #popup_title{
  290. width:100%;
  291. height:40px;
  292. line-height:40px;
  293. box-sizing:border-box;
  294. background-color:rgb(255,77,64);
  295. color:rgb(255,255,255);
  296. font-weight:700;
  297. font-size:20px;
  298. cursor:move;
  299. -webkit-touch-callout:none;
  300. -webkit-user-select:none;
  301. -khtml-user-select:none;
  302. -moz-user-select:none;
  303. -ms-user-select:none;
  304. user-select:none;
  305. }
  306. #close_popup{
  307. text-decoration:none;
  308. color:rgb(255,255,255);
  309. position:absolute;
  310. right:10px;
  311. top:0px;
  312. font-size:25px;
  313. display:inline-block;
  314. cursor:pointer;
  315. }
  316. </style><div id="popup_title">编辑测试用例格式
  317. <div id="close_popup">×</div>
  318. </div>
  319. <div style="line-height:150%;"><br>从第<input type="number" id="char_start" style="width:20%;" value="0" placeholder="0"></input>个字符探测到第<input type="number" id="char_end" style="width:20%;"></input>个字符<br>
  320. </div><button id="start_detect" style="margin:5% 5% 5% 5%;">开始探测</button></div>`
  321. popup.querySelector('#popup_title').addEventListener('mousedown', e => {
  322. // 记录鼠标在弹出窗口的相对位置
  323. mousestartX = e.clientX - parseInt(window.getComputedStyle(popup).getPropertyValue('left'))
  324. mousestartY = e.clientY - parseInt(window.getComputedStyle(popup).getPropertyValue('top'))
  325. // 鼠标移动时移动弹窗
  326. document.addEventListener('mousemove', movePopup)
  327. // 鼠标松开时结束移动
  328. document.addEventListener('mouseup', stopMove)
  329. })
  330. popup.querySelector('#close_popup').onclick = () => closePopup()// 点击关闭按钮,关闭窗口
  331. popup.querySelector('#start_detect').onclick = () => detect(parseInt(popup.querySelector('#char_start').value), parseInt(popup.querySelector('#char_end').value))
  332. document.body.append(popup_cover)
  333. document.body.append(popup)
  334. popup.querySelector('#char_end').focus()
  335. popup.querySelector('#char_start').addEventListener('keydown', function (e) {
  336. if (e.keyCode === 13) { // 按下Enter键
  337. detect(parseInt(popup.querySelector('#char_start').value), parseInt(popup.querySelector('#char_end').value))
  338. }
  339. })
  340. popup.querySelector('#char_end').addEventListener('keydown', function (e) {
  341. if (e.keyCode === 13) { // 按下Enter键
  342. detect(parseInt(popup.querySelector('#char_start').value), parseInt(popup.querySelector('#char_end').value))
  343. }
  344. })
  345. })
  346. })()