-
Notifications
You must be signed in to change notification settings - Fork 54
/
groupPanel.js
91 lines (72 loc) · 2.04 KB
/
groupPanel.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
/*
[Script]
groupPanel = type=generic,timeout=10,script-path=https://raw.githubusercontent.com/fishingworld/something/main/groupPanel.js,argument=icon=network&color=#86abee&group=Master
对应参数:
icon:图标
color:图标颜色
group:策略组名称
[Panel]
groupPanel = script-name=groupPanel,update-interval=5
*/
;(async () => {
let params = getParams($argument);
let group=params.group;
let proxy = await httpAPI("/v1/policy_groups");
let groupName = (await httpAPI("/v1/policy_groups/select?group_name="+encodeURIComponent(group)+"")).policy;
var proxyName= [];
let arr = proxy[""+group+""];
let allGroup = [];
for (var key in proxy){
allGroup.push(key)
}
for (let i = 0; i < arr.length; ++i) {
proxyName.push(arr[i].name);
}
let index;
for(let i = 0;i < proxyName.length; ++i) {
if(groupName==proxyName[i]){
index=i
}
};
if($trigger == "button"){
index += 1;
if(index>arr.length-1){
index = 0;
}
$surge.setSelectGroupPolicy(group, proxyName[index]);
};
let name =proxyName[index];
let secondName;
let rootName = name;
if(allGroup.includes(rootName)==true){
secondName = (await httpAPI("/v1/policy_groups/select?group_name="+encodeURIComponent(rootName)+"")).policy;
name = name + ' ➟ ' + secondName
}
while(allGroup.includes(rootName)==true){
rootName = (await httpAPI("/v1/policy_groups/select?group_name="+encodeURIComponent(rootName)+"")).policy;
}
if(arr[index].isGroup==true && secondName!= rootName){
name=name + ' ➟ ' + rootName;
}
$done({
title:group,
content:name,
icon: params.icon,
"icon-color":params.color
});
})();
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)])
);
}