-
Notifications
You must be signed in to change notification settings - Fork 0
/
Surge_Sub_Info_Panel.js
156 lines (137 loc) · 4.14 KB
/
Surge_Sub_Info_Panel.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* 由@mieqq编写
* 原脚本地址:https://raw.githubusercontent.com/mieqq/mieqq/master/sub_info_panel.js
* 由@Rabbit-Spec Key 修改
* 更新日期:2023.02.20
* 版本:1.6
*/
(async () => {
let args = getArgs();
let info = await getDataInfo(args.url);
if (!info) $done();
let resetDayLeft = getRmainingDays(parseInt(args["reset_day"]));
let used = info.download + info.upload;
let total = info.total;
let expire = args.expire || info.expire;
let content = [`用量:${toPercent(used, total)}\t| 剩余:${toMultiply(total, used)}`];
if (resetDayLeft || expire) {
if (resetDayLeft && expire && expire !== "false") {
if (/^[\d.]+$/.test(expire)) expire *= 1000;
content.push(`重置:${resetDayLeft}天 \t| ${formatTime(expire)}`);
} else if (resetDayLeft && !expire) {
content.push(`重置:${resetDayLeft}天`);
} else if (!resetDayLeft && expire) {
if (/^[\d.]+$/.test(expire)) expire *= 1000;
content.push(`到期:${formatTime(expire)}`);
}
}
let now = new Date();
let hour = now.getHours();
let minutes = now.getMinutes();
hour = hour > 9 ? hour : "0" + hour;
minutes = minutes > 9 ? minutes : "0" + minutes;
$done({
title: `${args.title} | ${bytesToSize(total)} | 已用${bytesToSize(used)}`,
content: content.join("\n"),
icon: args.icon || "airplane.circle",
"icon-color": args.color || "#007afe",
});
})();
function getArgs() {
return Object.fromEntries(
$argument
.split("&")
.map((item) => item.split("="))
.map(([k, v]) => [k, decodeURIComponent(v)])
);
}
function getUserInfo(url) {
let request = { headers: { "User-Agent": "Quantumult%20X" }, url };
return new Promise((resolve, reject) =>
$httpClient.get(request, (err, resp) => {
if (err != null) {
reject(err);
return;
}
if (resp.status !== 200) {
reject(resp.status);
return;
}
let header = Object.keys(resp.headers).find((key) => key.toLowerCase() === "subscription-userinfo");
if (header) {
resolve(resp.headers[header]);
return;
}
reject("链接响应头不带有流量信息");
})
);
}
async function getDataInfo(url) {
const [err, data] = await getUserInfo(url)
.then((data) => [null, data])
.catch((err) => [err, null]);
if (err) {
console.log(err);
return;
}
return Object.fromEntries(
data
.match(/\w+=[\d.eE+]+/g)
.map((item) => item.split("="))
.map(([k, v]) => [k, Number(v)])
);
}
function getRmainingDays(resetDay) {
if (!resetDay) return;
let now = new Date();
let today = now.getDate();
let month = now.getMonth();
let year = now.getFullYear();
let daysInMonth;
if (resetDay > today) {
daysInMonth = 0;
} else {
daysInMonth = new Date(year, month + 1, 0).getDate();
}
return daysInMonth - today + resetDay;
}
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 bytesToSizeNumber(bytes) {
if (bytes === 0) return "0";
let k = 1024;
let i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)).toFixed(2);
}
function toPercent(num, total) {
return (Math.round((num / total) * 10000) / 100).toFixed(1) + "%";
}
function toMultiply(total, num) {
let totalDecimalLen, numDecimalLen, maxLen, multiple;
try {
totalDecimalLen = total.toString().split(".").length;
} catch (e) {
totalDecimalLen = 0;
}
try {
numDecimalLen = num.toString().split(".").length;
} catch (e) {
numDecimalLen = 0;
}
maxLen = Math.max(totalDecimalLen, numDecimalLen);
multiple = Math.pow(10, maxLen);
const numberSize = ((total * multiple - num * multiple) / multiple).toFixed(maxLen);
return bytesToSize(numberSize);
}
function formatTime(time) {
let dateObj = new Date(time);
let year = dateObj.getFullYear();
let month = dateObj.getMonth() + 1;
let day = dateObj.getDate();
return "到期:" + year + "." + month + "." + day + " ";
}