-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.js
94 lines (87 loc) · 3.45 KB
/
extension.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
import St from 'gi://St';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import GLib from 'gi://GLib';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import GObject from 'gi://GObject';
import Clutter from 'gi://Clutter';
import Gio from 'gi://Gio';
let domainToPing = 'google.com'; // Change this to the domain or IP address you want to ping
let timeoutId = null;
const Indicator = GObject.registerClass(
class Indicator extends PanelMenu.Button {
_init() {
super._init(0.0, 'My Indicator');
this._label = new St.Label({
text: '',
y_align: Clutter.ActorAlign.CENTER
});
this.add_child(this._label);
this._entryItem = new PopupMenu.PopupMenuItem('');
this._entryItem.actor.reactive = false;
this._entryItem.actor.can_focus = false;
this._entry = new St.Entry({
hint_text: 'Enter domain or IP address',
track_hover: true,
can_focus: true
});
this._entry.clutter_text.connect('activate', () => {
domainToPing = this._entry.get_text();
this.menu.close();
});
this._entryItem.add_child(this._entry);
this.menu.addMenuItem(this._entryItem);
this.checkPingAsync();
}
async checkPingAsync() {
while (true) {
try {
let out = await new Promise((resolve, reject) => {
let proc = new Gio.Subprocess({
argv: ['ping', '-c', '1', domainToPing],
flags: Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE,
});
proc.init(null);
proc.communicate_utf8_async(null, null, (proc, res) => {
try {
let [ok, stdout, stderr] = proc.communicate_utf8_finish(res);
if (!ok) {
reject(new Error(stderr.trim()));
} else {
resolve(stdout.trim());
}
} catch (e) {
reject(e);
}
});
});
let match = /time=([\d.]+)/.exec(out);
if (match) {
let time = Math.round(parseFloat(match[1]));
this._label.set_text(`${time} ms`);
} else {
this._label.set_text('No response');
}
} catch (e) {
this._label.set_text('No response');
}
await new Promise(resolve => timeoutId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1, resolve));
}
}
}
);
let indicator;
export default class PingExtension {
enable() {
indicator = new Indicator();
Main.panel.addToStatusArea('indicator', indicator);
}
disable() {
if (timeoutId) {
GLib.Source.remove(timeoutId);
timeoutId = null;
}
indicator.destroy();
indicator = null; // Set indicator to null
}
}