forked from arifesat/USD-TRY-GShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
102 lines (86 loc) · 3.02 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
// This extensions shows EUR to USD convertion on Gnome panel.
// Copyright (C) 2024 vezza
// See LICENSE file
'use strict';
import St from 'gi://St'
import Gio from 'gi://Gio'
import Clutter from 'gi://Clutter'
import Soup from 'gi://Soup'
import GLib from 'gi://GLib'
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
let panelButton;
let panelButtonText;
let session;
let euroQuotation;
let sourceId = null;
// Handle Requests API Dollar
async function handle_request_euro_api() {
try {
// Create a new Soup Session
if (!session) {
session = new Soup.Session({ timeout: 10 });
}
// Create body of Soup request
let message = Soup.Message.new_from_encoded_form(
"GET", "https://economia.awesomeapi.com.br/last/EUR-USD", Soup.form_encode_hash({}));
// Send Soup request to API Server
await session.send_and_read_async(message, GLib.PRIORITY_DEFAULT, null, (_, r0) => {
let text = session.send_and_read_finish(r0);
let response = new TextDecoder().decode(text.get_data());
const body_response = JSON.parse(response);
// Get the value of Euro Quotation
euroQuotation = body_response["EURUSD"]["bid"];
euroQuotation = euroQuotation.split(".");
euroQuotation = euroQuotation[0] + "," + euroQuotation[1].substring(0, 4);
// Sext text in Widget
panelButtonText = new St.Label({
style_class : "cPanelText",
text: "(1 EUR = " + euroQuotation + " USD)",
y_align: Clutter.ActorAlign.CENTER,
});
panelButton.set_child(panelButtonText);
// Finish Soup Session
session.abort();
text = undefined;
response = undefined;
});
} catch (error) {
console.error(`Traceback Error in [handle_request_euro_api]: ${error}`);
panelButtonText = new St.Label({
text: "(1 EUR = " + _euroQuotation + ")" + " * ",
y_align: Clutter.ActorAlign.CENTER,
});
panelButton.set_child(panelButtonText);
session.abort();
}
}
export default class Eurusd {
enable() {
panelButton = new St.Bin({
style_class: "panel-button",
});
handle_request_euro_api();
Main.panel._centerBox.insert_child_at_index(panelButton, 0);
sourceId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 30, () => {
handle_request_euro_api();
return GLib.SOURCE_CONTINUE;
});
}
disable() {
Main.panel._centerBox.remove_child(panelButton);
if (panelButton) {
panelButton.destroy();
panelButton = null;
}
panelButtonText = null;
euroQuotation = null;
if (sourceId) {
GLib.Source.remove(sourceId);
sourceId = null;
}
if (session) {
session.abort(session);
session = null;
}
}
}