forked from Josholith/gnome-extension-lan-ip-address
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
109 lines (91 loc) · 3.11 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const St = imports.gi.St;
const Lang = imports.lang;
const PanelMenu = imports.ui.panelMenu;
const Clutter = imports.gi.Clutter;
const GLib = imports.gi.GLib;
const ShellToolkit = imports.gi.St;
const Clipboard = St.Clipboard.get_default();
function _get_lan_ip() {
// Ask the IP stack what route would be used to reach 1.1.1.1 (Cloudflare DNS)
// Specifically, what src would be used for the 1st hop?
var command_output_bytes = GLib.spawn_command_line_sync('ip route get 1.1.1.1')[1];
var command_output_string = '';
for (var current_character_index = 0;
current_character_index < command_output_bytes.length;
++current_character_index)
{
var current_character = String.fromCharCode(command_output_bytes[current_character_index]);
command_output_string += current_character;
}
// Output of the "ip route" command will be a string
// " ... src 1.2.3.4 ..."
// So basically we want the next token (word) immediately after the "src"
// word, and nothing else. This is considerd our LAN IP address.
var Re = new RegExp(/src [^ ]+/g);
var matches = command_output_string.match(Re);
var lanIpAddress;
if (matches) {
lanIpAddress = matches[0].split(' ')[1];
} else {
lanIpAddress = '';
}
return lanIpAddress;
}
const LanIpAddressIndicator = new Lang.Class({
Name: 'LanIpAddress.indicator',
Extends: PanelMenu.Button,
_init: function () {
this.parent(0.0, "LAN IP Address Indicator", false);
this.buttonText = new St.Label({
text: 'Loading...',
y_align: Clutter.ActorAlign.CENTER
});
this.add_child(this.buttonText);
this.actor.connect(
"button-press-event",
Lang.bind(this, this._copyIP)
);
this._updateLabel();
},
_updateLabel : function(){
const refreshTime = 5 // in seconds
if (this._timeout) {
Mainloop.source_remove(this._timeout);
this._timeout = null;
}
this._timeout = Mainloop.timeout_add_seconds(refreshTime, Lang.bind(this, this._updateLabel));
this.buttonText.set_text(_get_lan_ip());
},
_removeTimeout: function () {
if (this._timeout) {
this._timeout = null;
}
},
stop: function () {
if (this._timeout) {
Mainloop.source_remove(this._timeout);
}
this._timeout = undefined;
this.menu.removeAll();
},
_copyIP: function(){
Clipboard.set_text(St.ClipboardType.CLIPBOARD, this.buttonText.get_text());
Main.notify("IP copied to clipboard.", this.buttonText.get_text());
}
});
let _indicator;
function init() {
log('LAN IP Address extension initialized');
}
function enable() {
log('LAN IP Address extension enabled');
_indicator = new LanIpAddressIndicator();
Main.panel.addToStatusArea('lan-ip-address-indicator', _indicator);
}
function disable() {
log('LAN IP Address extension disabled');
_indicator.stop();
_indicator.destroy();
}