// ==UserScript== // @name 北化网课雨 // @namespace http://tampermonkey.net/ // @version 0.4.2 // @description never try to take over the world! // @author Snowman // @match https://buct.yuketang.cn/pro/* // @icon https://www.google.com/s2/favicons?sz=64&domain=yuketang.cn // @grant none // @downloadURL https://update.greasyfork.icu/scripts/461829/%E5%8C%97%E5%8C%96%E7%BD%91%E8%AF%BE%E9%9B%A8.user.js // @updateURL https://update.greasyfork.icu/scripts/461829/%E5%8C%97%E5%8C%96%E7%BD%91%E8%AF%BE%E9%9B%A8.meta.js // ==/UserScript== (function () { 'use strict'; class Utils { /** 页面环境 */ static Environment = { UNKNOWN: 0, VIDEO: 1, SCORE: 2, } /** 获取当前环境,以决定启动哪些模块 */ static GetEnvironment() { let path = location.pathname.split('/'); if (path[path.length - 2] == 'video') { return Utils.Environment.VIDEO; } if (path[path.length - 1] == 'score') { return Utils.Environment.SCORE; } return Utils.Environment.UNKNOWN; } /** 关闭浏览器当前标签页 */ static CloseWindow() { var userAgent = navigator.userAgent; if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Chrome") != -1) { window.location.href = "about:blank"; window.close(); } else { window.opener = null; window.open("", "_self"); window.close(); } } /** * 向目标元素注入 CSS * @param {Element} target 注入目标 * @param {string} css CSS 内容 */ static InsertCSS(target, css) { if (!target) return false; let style = document.createElement("style"); let text = document.createTextNode(css); style.appendChild(text); target.appendChild(style); return true; } /** * 选择单个元素 * @param {string} selector JS Selector 语句 * @param {boolean} mustExist 元素是否必须存在 * @returns {Function} */ static SelectOne = (selector, mustExist = false) => { let _selector = selector; let _element = null; /** @returns {Element | null} */ let f = function () { if (_element == null) _element = document.querySelector(_selector); if (mustExist && _element == null) throw { why: "错误:找不到元素", _selector }; else return _element; } return f; } /** * 选择所有元素 * @param {string} selector JS Selector 语句 * @param {boolean} mustExist 元素是否必须存在 * @returns {Function} */ static SelectAll = (selector, mustExist = true) => { let _selector = selector; let _elements = null; /** @returns {Element | null} */ let f = function () { if (_elements == null) _elements = document.querySelectorAll(_selector); if (mustExist && _elements == null) throw { why: "错误:找不到元素", _selector }; else return _elements; } return f; } /** * 坚持不懈地尝试做一件事 * @param {Function} task 要做的事,必须返回布尔值表示是否已成功完成 * @param {number} maxTries 最大尝试次数 * @param {number} interval 尝试间隔时间 */ static TryTryTry(task, maxTries = 5, interval = 1000) { let counter = 0; let result; let clock = setInterval(() => { // counter++,并检测是否超出尝试次数 if (++counter > maxTries) { clearInterval(clock); console.warn("任务失败次数过多,超出尝试上限!", { maxTries, interval, task }); return; } try { result = task(); } catch (error) { throw { why: "错误:任务执行时自身出错!", error }; } if (typeof result !== 'boolean') throw { why: "错误:任务返回值必须为布尔类型!", result }; if (result) { clearInterval(clock); console.log("[Debug] 任务完成,尝试次数为 " + counter); return; } }, interval); } } /** 若干功能模块 */ class Modules { /** 被迫展示自己被黑化的事实 */ static ShowBlacklized() { setTimeout(() => { ElementPool.title().innerHTML += `