-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgold.js
More file actions
162 lines (126 loc) · 6.05 KB
/
gold.js
File metadata and controls
162 lines (126 loc) · 6.05 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import St from 'gi://St';
import GObject from 'gi://GObject';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import Soup from 'gi://Soup';
import Clutter from 'gi://Clutter';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
const SCHEMA_ID = 'org.gnome.shell.extensions.gold-silver-price-tracker';
const API_URL = 'https://api.edelmetalle.de/public.json';
const _httpSession = new Soup.Session();
export default class PriceTrackerExtension {
constructor(extension) {
this._settings = extension.getSettings(SCHEMA_ID);
this._panelButton = null;
this._goldLabel = null;
this._silverLabel = null;
this._timeout = null;
this._settingsConnectionInterval = null;
// KORREKTUR: 'console.log' ohne Backticks
console.log('[' + extension.uuid + '] RealExtension: Constructor called.');
}
enable() {
// KORREKTUR: 'console.log' ohne Backticks
console.log('[' + this.constructor.name + '] RealExtension: enable() called.');
this._panelButton = new PanelMenu.Button(0.5, 'Gold Silver Tracker', false);
this._panelButton.add_style_class_name('panel-button');
let box = new St.BoxLayout({ y_align: Clutter.ActorAlign.CENTER });
this._goldLabel = new St.Label({ text: 'Gold: Lädt...' });
this._silverLabel = new St.Label({ text: 'Silber: Lädt...' });
box.add_child(this._goldLabel);
box.add_child(new St.Label({ text: ' | ' }));
box.add_child(this._silverLabel);
this._panelButton.add_child(box);
Main.panel.addToStatusArea('gold-silver-tracker', this._panelButton, 0, 'center');
this._settingsConnectionCurrency = this._settings.connect('changed::currency', this._updatePrices.bind(this));
this._settingsConnectionInterval = this._settings.connect('changed::update-interval', this._setupInterval.bind(this));
this._setupInterval();
}
disable() {
// KORREKTUR: 'console.log' ohne Backticks
console.log('[' + this.constructor.name + '] RealExtension: disable() called.');
if (this._timeout) {
GLib.source_remove(this._timeout);
this._timeout = null;
}
if (this._panelButton) {
this._panelButton.destroy();
this._panelButton = null;
}
if (this._settingsConnectionInterval) {
this._settings.disconnect(this._settingsConnectionInterval);
this._settingsConnectionInterval = null;
}
if (this._settingsConnectionCurrency) {
this._settings.disconnect(this._settingsConnectionCurrency);
this._settingsConnectionCurrency = null;
}
this._settings = null;
}
_setupInterval() {
if (this._timeout) {
GLib.source_remove(this._timeout);
this._timeout = null;
}
const intervalMinutes = this._settings.get_int('update-interval');
const intervalSeconds = intervalMinutes * 60;
// KORREKTUR: 'console.log' ohne Backticks
console.log('[' + this.constructor.name + '] Setting update interval to ' + intervalSeconds + ' seconds.');
this._updatePrices();
this._timeout = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, intervalSeconds, () => {
this._updatePrices();
return GLib.SOURCE_CONTINUE;
});
}
_updatePrices() {
const currency = this._settings.get_string('currency');
// KORREKTUR: '.set_text' ohne Backticks
this._goldLabel.set_text('Gold (' + currency + '): Lädt...');
this._silverLabel.set_text('Silber (' + currency + '): Lädt...');
const message = Soup.Message.new('GET', API_URL);
_httpSession.send_and_read_async(message, GLib.PRIORITY_DEFAULT, null, (session, res) => {
if (message.status_code !== Soup.Status.OK) {
// KORREKTUR: 'console.error' ohne Backticks
console.error('API-Fehler: Status ' + message.status_code);
// KORREKTUR: '.set_text' ohne Backticks
this._goldLabel.set_text('Gold (' + currency + '): X');
this._silverLabel.set_text('Silber (' + currency + '): X');
return;
}
try {
const bytes = session.send_and_read_finish(res);
const jsonString = new TextDecoder('utf-8').decode(bytes.get_data());
const data = JSON.parse(jsonString);
const goldPricePerGram = data.gold_eur;
const silverPricePerGram = data.silber_eur;
let exchangeRate = 1.0;
let symbol = '€';
switch(currency) {
case 'USD':
exchangeRate = 1.08;
symbol = '$';
break;
case 'GBP':
exchangeRate = 0.85;
symbol = '£';
break;
case 'EUR':
default:
symbol = '€';
}
const goldPrice = (parseFloat(goldPricePerGram) * exchangeRate).toFixed(2);
const silverPrice = (parseFloat(silverPricePerGram) * exchangeRate).toFixed(2);
// KORREKTUR: '.set_text' ohne Backticks
this._goldLabel.set_text('Gold: ' + symbol + goldPrice + '/g');
this._silverLabel.set_text('Silber: ' + symbol + silverPrice + '/g');
} catch (e) {
// KORREKTUR: 'console.error' ohne Backticks
console.error('Fehler beim Verarbeiten der API-Antwort: ' + e.message);
// KORREKTUR: '.set_text' ohne Backticks
this._goldLabel.set_text('Gold (' + currency + '): X');
this._silverLabel.set_text('Silber (' + currency + '): X');
}
});
}
}