-
Notifications
You must be signed in to change notification settings - Fork 0
/
Surge_Sub_Info.js
126 lines (110 loc) · 3.1 KB
/
Surge_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
/*
* 由@mieqq编写
* 原脚本地址:https://raw.githubusercontent.com/mieqq/mieqq/master/sub_info_panel.js
* 由@Rabbit-Spec修改
* 更新日期:2022.08.24
* 版本:1.5
*/
let args = getArgs();
(async () => {
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 = [`用量:${bytesToSize(used)} | ${bytesToSize(total)}`];
if (resetDayLeft) {
content.push(`重置:剩余${resetDayLeft}天`);
}
if (expire && expire !== "false") {
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} | ${hour}:${minutes}`,
content: content.join("\n"),
icon: args.icon || "airplane.circle",
"icon-color": args.color || "#007AFF",
});
})();
function getArgs() {
return Object.fromEntries(
$argument
.split("&")
.map((item) => item.split("="))
.map(([k, v]) => [k, decodeURIComponent(v)])
);
}
function getUserInfo(url) {
let method = args.method || "head";
let request = { headers: { "User-Agent": "Quantumult%20X" }, url };
return new Promise((resolve, reject) =>
$httpClient[method](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 formatTime(time) {
let dateObj = new Date(time);
let year = dateObj.getFullYear();
let month = dateObj.getMonth() + 1;
let day = dateObj.getDate();
return year + "年" + month + "月" + day + "日";
}