-
Notifications
You must be signed in to change notification settings - Fork 0
/
jms_sub_info.js
164 lines (139 loc) · 4.76 KB
/
jms_sub_info.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
157
158
159
160
161
162
163
164
/*
* 显示 JMS 剩余流量信息以及套餐到期时间
* update: 14/8/2023
* version: 1.0
*/
let args = getArgs();
let title = args.title || "JMS_info"
let icon = args.icon || "airplane.circle";
let iconColor = args.color || "#32CD32";
const apiUrl = args.url;
JMS_getDataInfo(apiUrl, function (result) {
let now = new Date();
let hour = now.getHours();
let minutes = now.getMinutes();
hour = hour > 9 ? hour : "0" + hour;
minutes = minutes > 9 ? minutes : "0" + minutes;
if (result.error) {
$done({ title: `${title} | ${hour}:${minutes}`, content: "获取失败,请检查模块或网络", icon: icon, "icon-color": "#ff4500" });
}
let jsonData = JSON.parse(result.data);
let usage = jsonData.bw_counter_b;
let total = jsonData.monthly_bw_limit_b;
let resetDay = jsonData.bw_reset_day_of_month;
let resetDate = JMS_getExpireDate(resetDay);
let resetDayLeft = getRmainingDays(parseInt(resetDay));
let expireDate = args.expire;
let content = [`已用: ${JMS_bytesToSize(usage)} | 剩余: ${toMultiply(total, usage)}`];
if (resetDayLeft || expireDate) {
if (resetDayLeft && expireDate && expireDate !== "false") {
if (/^[\d.]+$/.test(expireDate)) expireDate *= 1000;
content.push(`重置: \t ${resetDayLeft} 天 \t| 到期: ${formatTime(expireDate)}`);
} else if (resetDayLeft && !expireDate) {
content.push(`重置: \t ${resetDayLeft} 天 \t| 到期: ${formatTime(resetDate)}`);
} else if (!resetDayLeft) {
content.push(`到期时间:${formatTime(resetDate)}`);
}
}
$done({
title: `${title} | ${toPercent(usage, total)} | ${hour}:${minutes}`,
content: content.join("\n"),
icon: `${icon}`,
"icon-color": `${iconColor}`,
});
})
function getArgs() {
return Object.fromEntries(
$argument
.split("&")
.map((item) => item.split("="))
.map(([k, v]) => [k, decodeURIComponent(v)])
);
}
function JMS_getDataInfo(url, callback) {
const headers = {
'User-Agent': 'Quantumult%20X'
};
$httpClient.get({url: url, headers: headers}, function (error, response, data) {
if ((error) || (response.status !== 200)) {
callback({ error: true, data: null });
} else {
callback({ error: false, data: data });
}
});
}
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 JMS_bytesToSize(bytes) {
// const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
// if (bytes === undefined) return "N/A";
// const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)));
// return Math.round((bytes / Math.pow(1000, i)) * 100) / 100 + " " + sizes[i];
//}
function JMS_bytesToSize(bytes) {
if (bytes === 0) return "0B";
let k = 1000;
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 JMS_bytesToSize(numberSize);
}
function JMS_getExpireDate(dayOfMonth) {
if (!dayOfMonth) return;
const today = new Date();
const currentMonth = today.getMonth();
const currentYear = today.getFullYear();
const nextMonth = (currentMonth + 1 ) % 12;
const nextYear = currentMonth === 11 ? currentYear + 1 : currentYear;
const lastDayOfMonth = new Date(nextYear, nextMonth, 0).getDate();
const resetDate = Math.min(dayOfMonth, lastDayOfMonth);
if (dayOfMonth > today) {
return new Date(nextYear, nextMonth, resetDate).toLocaleDateString();
} else {
return new Date(nextYear, currentMonth, resetDate).toLocaleDateString();
}
}
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;
}