ACM Online Judge 评测状态自动刷新

在评测时监测,并在评测完成后自动刷新评测状态

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name ACM Online Judge 评测状态自动刷新
// @namespace https://ns.altk.org/userscript
// @description 在评测时监测,并在评测完成后自动刷新评测状态
// @include https://acm.sjtu.edu.cn/OnlineJudge/code*
// @version 0.1
// @grant none
// @license GPL-3.0
// ==/UserScript==

; {

const STATUS_SELECTOR = '#status_list tbody td:nth-child(4)'
const PENDING_REGEXP = /Running & Judging|Pending/i
const REFRESH_INTERVAL = 2000
const REFRESH_TIMEOUT = 5000

const isPending = (doc = document) => PENDING_REGEXP.test(doc.querySelector(STATUS_SELECTOR).innerText)
const delay = ms => new Promise(resolve => setTimeout(resolve, ms, true))

const monitor = async () => {
  while (true) {
    await delay(REFRESH_INTERVAL)
    const xhr = new XMLHttpRequest()
    xhr.open('GET', location.href)
    xhr.responseType = 'document'
    const response = new Promise((resolve, reject) => {
      setTimeout(reject, REFRESH_TIMEOUT, 'Timeout exceeded.')
      xhr.onload = () => resolve(xhr.responseXML)
      xhr.onerror = reject
    })
    xhr.send()
    try {
      const doc = await response
      if (!isPending(doc)) {
        location.reload()
        return
      }
    } catch (e) {
      sweetAlert('ACMOJ Helper: Network Error', `${e}`, 'error')
    }
  }
}

if (isPending()) monitor()
else console.log('[ACMOJ Helper] Not pending, not monitoring.')

}