Skip to content

Commit

Permalink
feat: BIT-校园网-上网明细
Browse files Browse the repository at this point in the history
  • Loading branch information
YDX-2147483647 committed Sep 14, 2023
1 parent 0b90c4f commit 0a423e6
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
7 changes: 7 additions & 0 deletions TamperMonkey/BIT-校园网-上网明细.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# BIT-校园网-上网明细

给“上网明细”中的“总流量”按大小标注颜色,类似[`lsd --long`](https://github.com/lsd-rs/lsd)

![BIT-校园网-上网明细](https://s2.loli.net/2023/09/14/EgByaVoCX8MKN32.jpg)

可帮助寻找异常流量。
72 changes: 72 additions & 0 deletions TamperMonkey/BIT-校园网-上网明细.user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// ==UserScript==
// @name BIT-校园网-上网明细
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 给“上网明细”中的“总流量”标注颜色,类似 lsd --long
// @license GPL-3.0-or-later
// @supportURL https://github.com/YDX-2147483647/BIT-enhanced/issues
// @author Y.D.X.
// @match http://10.0.0.54:8800/log/detail*
// @grant none
// ==/UserScript==

(function () {
'use strict'

const table = document.querySelector('#w0-container > table')

/** “总流量”(人类可读)列号 */
const TRAFFIC_HUMAN_COL = 5
/** “总流量”(机器数据)列号 */
const TRAFFIC_DATA_COL = 4
/** 划分大小的基数 */
const BASE = Math.pow(1024, 0.6)
/** 颜色系列,每种颜色对于一种流量大小,小流量在前 */
const PALETTE = ['gray', 'darkmagenta', 'darkblue', 'green', 'orange', 'red', 'fuchsia']

/**
* 检查网站内容是否符合预期
* @returns {boolean} 是否符合预期
*/
function content_as_expected () {
const traffic_human_thead = table.querySelector(`thead th:nth-child(${TRAFFIC_HUMAN_COL + 1})`)
const traffic_data_thead = table.querySelector(`thead th:nth-child(${TRAFFIC_DATA_COL + 1})`)

let as_expected = traffic_human_thead.textContent === '总流量' && traffic_human_thead.style.display !== 'none'
as_expected &= traffic_data_thead.textContent === '总流量' && traffic_data_thead.style.display === 'none'

return as_expected
}

/**
* 上色
* @param {HTMLElement} element
* @param {Number} data
*/
function paint (element, data) {
for (const color of PALETTE.slice(0, -1)) {
if (data < BASE) {
element.style.color = color
return
} else {
data /= BASE
}
}

// Fallback to the last
element.style.color = PALETTE[PALETTE.length - 1]
}

if (!content_as_expected()) {
console.error('网站有变,此脚本可能不再适用。已停用。')
return
}

table.querySelectorAll('tbody > tr').forEach(row => {
/** @type {HTMLTableCellElement} */
const cell = row.children[TRAFFIC_HUMAN_COL]
const traffic = Number(row.children[TRAFFIC_DATA_COL].textContent)

paint(cell, traffic)
})
})()

0 comments on commit 0a423e6

Please sign in to comment.