-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.js
141 lines (121 loc) · 3.92 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
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
/* extension.js
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
"use strict";
const { St, Gio, Clutter, Soup, GLib } = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
let panelButton;
let panelButtonText;
let session;
let sourceId = null;
// Start application
function init() {
// Initialization if needed
}
// Add the button to the panel
function enable() {
panelButton = new St.Bin({
style_class: "panel-button",
});
handle_request_dollar_api();
Main.panel._centerBox.insert_child_at_index(panelButton, 0);
sourceId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 30, () => {
handle_request_dollar_api();
return GLib.SOURCE_CONTINUE;
});
}
// Remove the added button from panel
function disable() {
panelButtonText = null;
session = null;
Main.panel._centerBox.remove_child(panelButton);
if (panelButton) {
panelButton.destroy();
panelButton = null;
}
if (sourceId) {
GLib.Source.remove(sourceId);
sourceId = null;
}
}
// Handle Requests API Dollar
async function handle_request_dollar_api() {
let dollarQuotation = null;
let upDown = null;
let upDownIcon = null;
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/USD-BRL",
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 Dollar Quotation
upDown = body_response["USDBRL"]["varBid"];
dollarQuotation = body_response["USDBRL"]["bid"];
dollarQuotation = dollarQuotation.split(".");
dollarQuotation =
dollarQuotation[0] + "." + dollarQuotation[1].substring(0, 2);
parseFloat(upDown) > 0 ? (upDownIcon = "⬆") : (upDownIcon = "⬇");
let rateDisplay = "BRL: $" + dollarQuotation + " ";
let iconDisplay = upDownIcon;
let iconColor = upDown > 0 ? "darkgreen" : "darkred";
let label1 = new St.Label({
text: rateDisplay,
y_align: Clutter.ActorAlign.CENTER,
});
let label2 = new St.Label({
text: iconDisplay,
y_align: Clutter.ActorAlign.CENTER,
style: `color: ${iconColor};`,
});
panelButtonText = new St.BoxLayout();
panelButtonText.add_child(label1);
panelButtonText.add_child(label2);
panelButton.set_child(panelButtonText);
// Finish Soup Session
session.abort();
text = undefined;
response = undefined;
}
);
} catch (error) {
log(`Traceback Error in [handle_request_dollar_api]: ${error}`);
panelButtonText = new St.Label({
text: "BRL: $" + _dollarQuotation + " * ",
y_align: Clutter.ActorAlign.CENTER,
});
panelButton.set_child(panelButtonText);
session.abort();
}
}