-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrafficStatistics.js
113 lines (93 loc) · 2.66 KB
/
TrafficStatistics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
let params = getParams($argument)
;(async () => {
let traffic = (await httpAPI("/v1/traffic"))
let interface = traffic.interface
/* 获取所有网络界面 */
let allNet = [];
for (var key in interface){
allNet.push(key)
}
if(allNet.includes("lo0")==true){
del(allNet,"lo0")
}
let net;
let index;
if( $persistentStore.read("NETWORK")==null||allNet.includes($persistentStore.read("NETWORK"))==false){
index = 0
}else{
net = $persistentStore.read("NETWORK")
for(let i = 0;i < allNet.length; ++i) {
if(net==allNet[i]){
index=i
}
}
}
/* 手动执行时切换网络界面 */
if($trigger == "button"){
if(allNet.length>1) index += 1
if(index>=allNet.length) index = 0;
$persistentStore.write(allNet[index],"NETWORK")
};
net = allNet[index]
let network = interface[net]
let outCurrentSpeed = speedTransform(network.outCurrentSpeed) //上传速度
let outMaxSpeed = speedTransform(network.outMaxSpeed) //最大上传速度
let download = bytesToSize(network.in) //下载流量
let upload = bytesToSize(network.out) //上传流量
let inMaxSpeed = speedTransform(network.inMaxSpeed) //最大下载速度
let inCurrentSpeed = speedTransform(network.inCurrentSpeed) //下载速度
/* 判断网络类型 */
let netType;
if(net=="en0") {
netType = "WiFi"
}else{
netType = "Cellular"
}
$done({
title:"流量统计 | "+netType,
content:`流量 ➟ ${upload} | ${download}\n`+
`速度 ➟ ${outCurrentSpeed} | ${inCurrentSpeed}\n` +
`峰值 ➟ ${outMaxSpeed} | ${inMaxSpeed}`,
icon: params.icon,
"icon-color":params.color
});
})()
function bytesToSize(bytes) {
if (bytes === 0) return "0B";
let k = 1024;
sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
let i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)).toFixed(2) + " " + sizes[i];
}
function speedTransform(bytes) {
if (bytes === 0) return "0B/s";
let k = 1024;
sizes = ["B/s", "KB/s", "MB/s", "GB/s", "TB/s", "PB/s", "EB/s", "ZB/s", "YB/s"];
let i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)).toFixed(2) + " " + sizes[i];
}
function httpAPI(path = "", method = "GET", body = null) {
return new Promise((resolve) => {
$httpAPI(method, path, body, (result) => {
resolve(result);
});
});
};
function getParams(param) {
return Object.fromEntries(
$argument
.split("&")
.map((item) => item.split("="))
.map(([k, v]) => [k, decodeURIComponent(v)])
);
}
function del(arr,num) {
var l=arr.length;
for (var i = 0; i < l; i++) {
if (arr[0]!==num) {
arr.push(arr[0]);
}
arr.shift(arr[0]);
}
return arr;
}