forked from kinduff/usd-mxn-gshell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
97 lines (81 loc) · 2.29 KB
/
extension.js
File metadata and controls
97 lines (81 loc) · 2.29 KB
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
// This extension shows USD to JPY conversion on Gnome panel.
// Copyright (C) 2025 AM Linux Team
// See LICENSE file
'use strict';
import St from 'gi://St';
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 session;
let sourceId = null;
async function fetchDollarQuotation() {
if (!session) {
session = new Soup.Session({ timeout: 10 });
}
const message = Soup.Message.new_from_encoded_form(
"GET", "https://economia.awesomeapi.com.br/last/USD-JPY", Soup.form_encode_hash({})
);
try {
const text = await session.send_and_read_async(message, GLib.PRIORITY_DEFAULT, null);
return new TextDecoder().decode(text.get_data());
} catch (error) {
console.error(`Error fetching dollar quotation: ${error}`);
} finally {
session.abort();
}
return null;
}
function updatePanelText(quotation) {
let displayText = quotation
? `1 USD = ${quotation} JPY`
: "";
const panelButtonText = new St.Label({
text: displayText,
y_align: Clutter.ActorAlign.CENTER,
});
panelButton.set_child(panelButtonText);
}
async function handleRequestApi() {
const response = await fetchDollarQuotation();
if (response) {
try {
const bodyResponse = JSON.parse(response);
let dollarQuotation = bodyResponse["USDJPY"]["bid"];
dollarQuotation = dollarQuotation.substring(0, 5);
updatePanelText(dollarQuotation);
} catch (error) {
console.error(`Error parsing dollar quotation: ${error}`);
updatePanelText(null);
}
} else {
updatePanelText(null);
}
}
export default class UsdJpy {
enable() {
panelButton = new St.Bin({ style_class: "panel-button" });
handleRequestApi();
Main.panel._centerBox.insert_child_at_index(panelButton, 0);
sourceId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 30, () => {
handleRequestApi();
return GLib.SOURCE_CONTINUE;
});
}
disable() {
if (panelButton) {
Main.panel._centerBox.remove_child(panelButton);
panelButton.destroy();
panelButton = null;
}
if (sourceId) {
GLib.Source.remove(sourceId);
sourceId = null;
}
if (session) {
session.abort();
session = null;
}
}
}