forked from dompling/Scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VPNSubscription.js
518 lines (469 loc) · 14.1 KB
/
VPNSubscription.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// letiables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: paper-plane;
// 添加require,是为了vscode中可以正确引入包,以获得自动补全等功能
if (typeof require === 'undefined') require = importModule;
const {DmYY, Runing} = require('./DmYY');
// @组件代码开始
class Widget extends DmYY {
constructor(arg) {
super(arg);
this.name = 'VPNSubscription';
this.en = 'VPNSubscription';
this.CACHE_KEY = this.md5(`dataSouce_${this.en}`);
this.Run();
}
useBoxJS = false;
today = '';
logo = 'https://raw.githubusercontent.com/58xinian/icon/master/glados_animation.gif';
dataSource = {
restData: '0',
usedData: '0',
totalData: '0',
todayData: '0',
isCheckIn: false,
};
account = {
title: '',
url: '',
};
color1 = ['#ef0a6a', '#b6359c'];
color2 = ['#ff54fa', '#fad126'];
color3 = ['#28cfb3', '#72d7cc'];
chartConfig = (data, color, value) => {
console.log(data);
const fontColor = `#${this.widgetColor.hex}`;
const template1 = `
{
"type": "radialGauge",
"data": {
"datasets": [
{
"data": [${parseFloat(data[0])}],
"borderWidth": 0,
"backgroundColor": getGradientFillHelper('vertical', ${JSON.stringify(
color[0])}),
}
]
},
"options": {
centerPercentage: 86,
rotation: Math.PI / 2,
centerArea: {
displayText: false,
},
options:{
trackColor: '#f4f4f4',
}
}
}
`;
const template2 = `
{
"type": "radialGauge",
"data": {
"datasets": [
{
"data": [${parseFloat(data[1])}],
"borderWidth": 0,
"backgroundColor": getGradientFillHelper('vertical', ${JSON.stringify(
color[1])}),
}
]
},
"options": {
layout: {
padding: {
left: 47,
right: 47,
top: 47,
bottom: 47
}
},
options:{
trackColor: '#f4f4f4',
},
centerPercentage: 80,
rotation: Math.PI / 2,
centerArea: {
displayText: false,
}
}
}
`;
const template3 = `
{
"type": "radialGauge",
"data": {
"datasets": [
{
"data": [${parseFloat(data[2])}],
"borderWidth": 0,
"backgroundColor": getGradientFillHelper('vertical', ${JSON.stringify(
color[2])}),
}
]
},
"options": {
layout: {
padding: {
left: 94,
right: 94,
top: 94,
bottom: 94
}
},
options:{
trackColor: '#f4f4f4',
},
centerPercentage: 70,
rotation: Math.PI / 2,
centerArea: {
displayText: true,
fontColor: '${fontColor}',
fontSize: 20,
text:(value)=>{
return '${value}';
}
}
}
}
`;
console.log(template1);
console.log(template2);
console.log(template3);
return {template1, template2, template3};
};
init = async () => {
try {
const data = await this.getdata(this.account.url);
const total = data[2];
const today = data[0];
const remain = data[2] - data[0] - data[1];
const use = total - remain;
this.dataSource.restData = remain;
this.dataSource.totalData = total;
this.dataSource.usedData = use;
this.dataSource.todayData = today;
console.log(this.dataSource);
} catch (e) {
console.log(e);
}
};
async getdata(url) {
const req = new Request(url);
req.method = 'GET';
await req.load();
let resp = req.response.headers['subscription-userinfo'];
resp = [
(parseInt(resp.match(/upload=([0-9]+);?/)[1])).toFixed(2),
(parseInt(resp.match(/download=([0-9]+);?/)[1])).toFixed(2),
(parseInt(resp.match(/total=([0-9]+);?/)[1])).toFixed(2),
];
console.log(resp);
return resp;
}
gradient = (color) => {
const linear = new LinearGradient();
linear.colors = color.map(item => new Color(item));
linear.locations = [0, 0.5];
return linear;
};
formatFileSize(fileSize) {
if (fileSize < (1024 * 1024)) {
let temp = fileSize / 1024;
temp = temp.toFixed(2);
return temp + 'KB';
} else if (fileSize < (1024 * 1024 * 1024)) {
let temp = fileSize / (1024 * 1024);
temp = temp.toFixed(2);
return temp + 'MB';
} else if (fileSize < (1024 * 1024 * 1024 * 1024)) {
let temp = fileSize / (1024 * 1024 * 1024);
temp = temp.toFixed(2);
return temp + 'GB';
} else {
let temp = fileSize / (1024 * 1024 * 1024 * 1024);
temp = temp.toFixed(2);
return temp + 'TB';
}
}
createChart = async (size, chart) => {
const url = `https://quickchart.io/chart?w=${size.w}&h=${size.h}&f=png&c=${encodeURIComponent(
chart)}`;
return await this.$request.get(url, 'IMG');
};
setContent = async (w, size, viewSize) => {
const rest = this.dataSource.restData;
const use = this.dataSource.usedData;
const today = this.dataSource.todayData;
const total = this.dataSource.totalData;
const data1 = Math.floor(rest / total * 100);
const data2 = Math.floor(use / total * 100);
const data3 = Math.floor((today / total) * 100);
const data = [
data1 || 0, data2 || 0, data3 || 0,
];
const {template1, template2, template3} = this.chartConfig(
data,
[this.color1, this.color2, this.color3],
this.formatFileSize(this.dataSource.restData),
);
const stackContent = w.addStack();
stackContent.centerAlignContent();
const stackSize = viewSize;
stackContent.size = stackSize;
stackContent.backgroundImage = await this.createChart(size, template1);
const stackContent2 = stackContent.addStack();
stackContent2.size = stackSize;
stackContent2.backgroundImage = await this.createChart(size, template2);
const stackContent3 = stackContent2.addStack();
stackContent3.size = stackSize;
stackContent3.backgroundImage = await this.createChart(size, template3);
};
setLabelCell = async (stack, data) => {
stack.addSpacer();
const stackCell = stack.addStack();
stackCell.centerAlignContent();
const stackIcon = stackCell.addStack();
stackIcon.size = new Size(16, 16);
stackIcon.cornerRadius = 8;
if (data.isImg) {
try {
const icon = await this.$request.get(data.icon, 'IMG');
stackIcon.addImage(icon);
} catch (e) {
console.log(e);
}
} else {
stackIcon.backgroundGradient = data.icon;
}
stackCell.addSpacer(5);
const stackTitle = stackCell.addStack();
const title = {...this.textFormat.title};
title.color = this.widgetColor;
this.provideText(data.title, stackTitle, title);
stackCell.addSpacer(5);
const stackValue = stackCell.addStack();
const value = {...this.textFormat.defaultText};
value.color = this.widgetColor;
this.provideText(data.value, stackValue, title);
stack.addSpacer();
};
setFooterCell = (stack, data) => {
const title = {...this.textFormat.title};
title.color = this.widgetColor;
title.size = 10;
const stackCell = stack.addStack();
stackCell.layoutVertically();
const stackIcon = stackCell.addStack();
stackIcon.addSpacer();
const stackViewIcon = stackIcon.addStack();
stackViewIcon.size = new Size(10, 10);
stackViewIcon.cornerRadius = 5;
stackViewIcon.backgroundGradient = this.gradient(data.color);
stackIcon.addSpacer();
stackCell.layoutVertically();
const stackText = stackCell.addStack();
stackText.addSpacer();
this.provideText(data.value, stackText, title);
stackText.addSpacer();
const desc = {...this.textFormat.defaultText};
desc.color = this.widgetColor;
desc.size = 8;
const stackTip = stackCell.addStack();
stackTip.addSpacer();
this.provideText(data.label, stackTip, desc);
stackTip.addSpacer();
};
renderSmall = async (w) => {
const stackHeader = w.addStack();
const stackLeft = stackHeader.addStack();
stackHeader.centerAlignContent();
stackLeft.centerAlignContent();
try {
const imgIcon = await this.$request.get(
this.account.icon || this.logo, 'IMG');
const stackImgItem = stackLeft.addImage(imgIcon);
stackImgItem.imageSize = new Size(12, 12);
stackImgItem.cornerRadius = 4;
stackLeft.addSpacer(5);
} catch (e) {
console.log(e);
}
const title = {...this.textFormat.title};
title.color = this.widgetColor;
title.size = 12;
this.provideText(this.account.title, stackLeft, title);
stackHeader.addSpacer();
const stackRight = stackHeader.addStack();
stackRight.centerAlignContent();
const calendar = SFSymbol.named('waveform.path.badge.minus');
const imgCalendar = stackRight.addImage(calendar.image);
imgCalendar.imageSize = new Size(12, 12);
imgCalendar.tintColor = new Color('#00b800');
stackRight.addSpacer(5);
this.provideText(
this.formatFileSize(this.dataSource.todayData), stackRight, title);
w.addSpacer();
const stackContent = w.addStack();
stackContent.addSpacer();
await this.setContent(stackContent, {w: 360, h: 360}, new Size(80, 80));
stackContent.addSpacer();
w.addSpacer();
const stackFooter = w.addStack();
stackFooter.centerAlignContent();
const stackFooterLeft = stackFooter.addStack();
this.setFooterCell(stackFooterLeft, {
value: this.formatFileSize(this.dataSource.restData),
label: '剩余',
color: this.color1,
});
stackFooter.addSpacer();
const stackFooterRight = stackFooter.addStack();
this.setFooterCell(stackFooterRight, {
value: this.formatFileSize(this.dataSource.usedData),
label: '累计',
color: this.color2,
});
return w;
};
renderMedium = async (w) => {
const stackBody = w.addStack();
const stackLeft = stackBody.addStack();
await this.setContent(stackLeft, {w: 360, h: 360}, new Size(140, 140));
stackBody.addSpacer(10);
const stackRight = stackBody.addStack();
stackRight.layoutVertically();
await this.setLabelCell(
stackRight, {
icon: this.account.icon || this.logo,
title: this.account.title,
value: ``,
isImg: true,
});
await this.setLabelCell(
stackRight,
{
icon: this.gradient(this.color3),
title: '上传',
value: this.formatFileSize(this.dataSource.todayData),
},
);
await this.setLabelCell(
stackRight,
{
icon: this.gradient(this.color2),
title: '累计',
value: this.formatFileSize(this.dataSource.usedData),
},
);
await this.setLabelCell(
stackRight,
{
icon: this.gradient(this.color1),
title: '剩余',
value: this.formatFileSize(this.dataSource.restData),
},
);
return w;
};
renderLarge = async (w) => {
w.addText('暂不支持');
return w;
};
/**
* 渲染函数,函数名固定
* 可以根据 this.widgetFamily 来判断小组件尺寸,以返回不同大小的内容
*/
async render() {
await this.init();
const widget = new ListWidget();
await this.getWidgetBackgroundImage(widget);
if (this.widgetFamily === 'medium') {
return await this.renderMedium(widget);
} else if (this.widgetFamily === 'large') {
return await this.renderLarge(widget);
} else {
return await this.renderSmall(widget);
}
}
Run = () => {
try {
if (config.runsInApp) {
this.registerAction('默认订阅', this.actionSettings);
this.registerAction('清除订阅', this.deletedVpn);
this.registerAction('新增订阅', async () => {
const account = await this.setAlertInput(
'添加订阅', '添加订阅数据,添加完成之后请去设置默认订阅', {
title: '机场名',
icon: '图标',
url: '订阅地址',
}, false);
if (!this.settings.dataSource) this.settings.dataSource = [];
if (!account) return;
if (account.title && account.url) {
this.settings.dataSource.push(account);
}
this.settings.dataSource = this.settings.dataSource.filter(
item => item);
this.saveSettings();
});
this.registerAction('基础设置', this.setWidgetConfig);
}
this.account = this.settings.account || this.account;
this.CACHE_KEY += '_' + this.account.title;
const index = typeof args.widgetParameter === 'string' ? parseInt(
args.widgetParameter) : false;
if (this.settings.dataSource && this.settings.dataSource[index] &&
index !==
false) {
this.account = this.settings.dataSource[index];
}
} catch (e) {
console.log(e);
}
};
async actionSettings() {
try {
const table = new UITable();
const dataSource = this.settings.dataSource || [];
dataSource.map((t, index) => {
const r = new UITableRow();
r.addText(`parameter:${index} 机场名:${t.title} 订阅:${t.url}`);
r.onSelect = () => {
this.settings.account = t;
this.notify(t.title, `默认订阅设置成功\n订阅:${t.url}`);
this.saveSettings(false);
};
table.addRow(r);
});
table.present(false);
} catch (e) {
console.log(e);
}
}
async deletedVpn() {
try {
const table = new UITable();
const dataSource = this.settings.dataSource || [];
dataSource.map((t, index) => {
const r = new UITableRow();
r.addText(`❌ 机场名:${t.title} 订阅:${t.url}`);
r.onSelect = () => {
dataSource.splice(index, 1);
this.settings.dataSource = dataSource;
this.notify(t.title, `❌\n订阅:${t.url}`);
this.saveSettings(false);
};
table.addRow(r);
});
table.present(false);
} catch (e) {
console.log(e);
}
}
}
// @组件代码结束
// await Runing(Widget, "", false); // 正式环境
await Runing(Widget, '', false); //远程开发环境