-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
283 lines (221 loc) · 9.52 KB
/
script.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
const ip = args.widgetParameter || "http-statuscode.com"; // ADD IP OR DOMAIN as Widget Parameter
const url = `http://${ip}`;
const fileManager = FileManager.local();
const historyFilePath = fileManager.joinPath(fileManager.documentsDirectory(), 'ping_history.json');
// Save Ping Data
async function savePingData(pingData) {
let historyData = [];
if (fileManager.fileExists(historyFilePath)) {
const rawHistoryData = fileManager.readString(historyFilePath);
historyData = JSON.parse(rawHistoryData);
}
historyData.push(pingData);
// Delete old data
if (historyData.length > 42) {
historyData = historyData.slice(-42);
}
fileManager.writeString(historyFilePath, JSON.stringify(historyData));
}
// read old hostory entry
function readPingHistory() {
if (fileManager.fileExists(historyFilePath)) {
const rawHistoryData = fileManager.readString(historyFilePath);
return JSON.parse(rawHistoryData);
} else {
return [];
}
}
function addHistoryEntry(column, data) {
const pingColor = data.ping > 500 ? Color.red() : (data.ping > 200 ? Color.yellow() : Color.green());
const formattedDate = new Date(data.timestamp).toLocaleString(); // Date in local format
let historyText = column.addText(`${data.ping} (${formattedDate})`);
historyText.textColor = pingColor;
historyText.font = Font.systemFont(10);
}
async function pingServer() {
const start = new Date().getTime();
let statusCode = null;
let dataSize = null;
const req = new Request(url);
try {
const responseText = await req.loadString();
const end = new Date().getTime();
statusCode = req.response.statusCode; // HTTP-Statuscode
dataSize = responseText.length; // Size of the response
return { ping: end - start, statusCode, dataSize };
} catch (e) {
console.log(e);
return { ping: "Error", statusCode: "Error", dataSize: "Error" };
}
}
function formatTime(date) {
let ampm = "AM";
let hours = date.getHours();
let minutes = date.getMinutes().toString().padStart(2, '0');
let seconds = date.getSeconds().toString().padStart(2, '0');
if (hours === 0) {
hours = 12;
} else if (hours > 12) {
hours = hours - 12;
ampm = "PM";
}
return `${hours}:${minutes}:${seconds} ${ampm}`;
}
async function createWidget(result, size = "small") {
let widget = new ListWidget();
widget.backgroundColor = new Color("#1A1A1A");
if (size === "medium") {
let mainStack = widget.addStack();
mainStack.layoutHorizontally();
// Left side - Large Ping
// Ping Stack with centered content
let pingStack = mainStack.addStack();
pingStack.layoutVertically();
pingStack.centerAlignContent(); // center
pingStack.size = new Size(150, 0); // Set a fixed width for the pingStack
// Ping Value
let pingValue = pingStack.addText(`${result.ping}`);
pingValue.textColor = Color.green();
pingValue.font = Font.boldSystemFont(60);
pingValue.textOpacity = 1.0;
// Ping Unit
let pingUnit = pingStack.addText(`ms`);
pingUnit.textColor = Color.green();
pingUnit.font = Font.systemFont(16);
pingUnit.textOpacity = 0.8;
mainStack.addSpacer();
// Right side - Detail
let detailsStack = mainStack.addStack();
detailsStack.layoutVertically();
detailsStack.spacing = 4;
detailsStack.size = new Size(150, 0); // Set a fixed width for the detailsStack
let title = detailsStack.addText("Server Ping");
title.textColor = Color.white();
title.font = Font.boldSystemFont(20);
widget.addSpacer(9);
let pingTextIp = detailsStack.addText(`${url}`);
pingTextIp.textColor = Color.white();
pingTextIp.font = Font.systemFont(8);
let statusText = detailsStack.addText(`Status: ${result.statusCode}`);
statusText.textColor = Color.white();
statusText.font = Font.systemFont(12);
let sizeText = detailsStack.addText(`Size: ${result.dataSize} Zeichen`);
sizeText.textColor = Color.white();
sizeText.font = Font.systemFont(12);
const updateTime = new Date();
let updateText = detailsStack.addText(`Updated: ${formatTime(updateTime)}`);
updateText.textColor = Color.gray();
updateText.font = Font.systemFont(12);
} else if (size === "large") {
let mainStack = widget.addStack();
mainStack.layoutHorizontally();
// Left side - Large Ping
// Ping Stack with centered content
let pingStack = mainStack.addStack();
pingStack.layoutVertically();
pingStack.centerAlignContent();
pingStack.size = new Size(150, 0); // Set a fixed width for the pingStack
// Ping Value
let pingValue = pingStack.addText(`${result.ping}`);
pingValue.textColor = Color.green();
pingValue.font = Font.boldSystemFont(60);
pingValue.textOpacity = 1.0;
// Ping Unit
let pingUnit = pingStack.addText(`ms`);
pingUnit.textColor = Color.green();
pingUnit.font = Font.systemFont(16);
pingUnit.textOpacity = 0.8;
mainStack.addSpacer();
// Right side - Detail
let detailsStack = mainStack.addStack();
detailsStack.layoutVertically();
detailsStack.spacing = 4;
detailsStack.size = new Size(150, 0); // Set a fixed width for the detailsStack
let title = detailsStack.addText("Server Ping");
title.textColor = Color.white();
title.font = Font.boldSystemFont(20);
widget.addSpacer(9);
let pingTextIp = detailsStack.addText(`${url}`);
pingTextIp.textColor = Color.white();
pingTextIp.font = Font.systemFont(8);
let statusText = detailsStack.addText(`Status: ${result.statusCode}`);
statusText.textColor = Color.white();
statusText.font = Font.systemFont(12);
let sizeText = detailsStack.addText(`Size: ${result.dataSize} Zeichen`);
sizeText.textColor = Color.white();
sizeText.font = Font.systemFont(12);
const updateTime = new Date();
let updateText = detailsStack.addText(`Updated: ${formatTime(updateTime)}`);
updateText.textColor = Color.gray();
updateText.font = Font.systemFont(12);
// Trennlinie oder Spacer
widget.addSpacer();
// Historie-Stack
let historyStack = widget.addStack();
historyStack.layoutHorizontally();
// Berechnen der Breite für jede Spalte
let columnWidth = 150; // Beispielbreite, passen Sie sie nach Bedarf an
let leftColumn = historyStack.addStack();
leftColumn.layoutVertically();
leftColumn.size = new Size(columnWidth, 0);
historyStack.addSpacer(); // Fügt einen Spacer zwischen den Spalten hinzu
let rightColumn = historyStack.addStack();
rightColumn.layoutVertically();
rightColumn.size = new Size(columnWidth, 0);
// Verteilung der Historiendaten auf die Spalten
const historyData = readPingHistory();
const halfLength = Math.ceil(historyData.length / 2);
for (let i = 0; i < halfLength; i++) {
addHistoryEntry(leftColumn, historyData[i]);
}
for (let i = halfLength; i < historyData.length; i++) {
addHistoryEntry(rightColumn, historyData[i]);
}
} else {
// Title
let title = widget.addText("Server Ping");
title.textColor = Color.white();
title.font = Font.boldSystemFont(16);
widget.addSpacer(5);
// URL
let pingTextIp = widget.addText(`${url}`);
pingTextIp.textColor = Color.white();
pingTextIp.font = Font.systemFont(8);
widget.addSpacer(5);
// Ping
let pingText = widget.addText(`${result.ping} ms`);
pingText.textColor = Color.green();
pingText.font = Font.systemFont(32);
widget.addSpacer(5);
// Status and Size
let statusText = widget.addText(`Status: ${result.statusCode} \nSize: ${result.dataSize} Zeichen`);
statusText.textColor = Color.white();
statusText.font = Font.systemFont(12);
widget.addSpacer(5);
// Updated Time
const updateTime = new Date();
let updateText = widget.addText(`Updated: ${formatTime(updateTime)}`);
updateText.textColor = Color.gray();
updateText.font = Font.systemFont(12);
}
return widget;
}
async function main() {
const result = await pingServer();
await savePingData({ ping: result.ping, timestamp: new Date().toISOString() }); // Speichern mit Zeitstempel
let widget;
if (config.widgetFamily === "medium") {
widget = await createWidget(result, "medium");
} else if (config.widgetFamily === "large") {
widget = await createWidget(result, "large");
} else {
widget = await createWidget(result, "small");
}
if (config.runsInWidget) {
Script.setWidget(widget);
} else {
config.widgetFamily === "medium" ? widget.presentMedium() : widget.presentSmall();
}
Script.complete();
}
main();