Ubik Academy - automatically extend lab time

[EN] Extend the time the lab is usable when the timer is below 10 minutes

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           Ubik Academy - automatically extend lab time
// @version        0.3
// @namespace      nil
// @author         nil
// @grant          none
// @description    [EN] Extend the time the lab is usable when the timer is below 10 minutes
// @include        https://cyberkube.app/?token=*
// @license        GPLv3
// ==/UserScript==

(function nil_greasemonkey_ubik_extendlab() {
  "use strict";
  function checkTimerAndPotentiallyExtendLab() {
    let elt = document.getElementById("timer");
    // "01h : 53m : 35s"
    if (!elt || 9 >= elt.innerText.length) return;
    let remaining_seconds = elt.innerText
				.replace(/[hms\s]/g, "")
         		        .split(':')
    				.map(Number)
    				.reverse()
    				.reduce((acc, elt, idx) => acc + elt * 60**idx, 0);
    // do nothing if more than 10 minutes left (600 seconds)
    if (600 <= remaining_seconds) return;

    let extendBtn = document.getElementById("extend-btn");
    if (!extendBtn) { console.log("#extend-btn element not found"); return; }

    console.log("clicked the extend lab button!");
    extendBtn.click();

    // wait 20 s then refresh the page
    window.setTimeout(() => location.reload(), 20000);
  }
  
  window.setInterval(checkTimerAndPotentiallyExtendLab, 60000);
})();