-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
122 lines (105 loc) · 3.05 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
const GETTEXT_DOMAIN = "bitcoin-price-checker"
const { GObject, St, Clutter, Soup, GLib } = imports.gi
const ExtensionUtils = imports.misc.extensionUtils
const Main = imports.ui.main
const PanelMenu = imports.ui.panelMenu
const _ = ExtensionUtils.gettext
let soupSession
const Indicator = GObject.registerClass(
class Indicator extends PanelMenu.Button {
_init() {
super._init(0.0, _("Bitcoin Price Checker"))
this.label = new St.Label({ y_align: Clutter.ActorAlign.CENTER })
this.add_child(this.label)
this._updatePrice()
this._refreshTimeout = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
60,
() => {
this._updatePrice()
return GLib.SOURCE_CONTINUE
},
)
}
_updatePrice() {
this.label.set_text(_("Fetching..."))
this._fetchBitcoinPrice()
.then((price) => {
const formattedPrice = price.toLocaleString("en-US", {
style: "currency",
currency: "USD",
maximumFractionDigits: 0,
})
this.label.set_text(`BTC: ${formattedPrice}`)
})
.catch((error) => {
this.label.set_text(_("Error fetching price"))
})
}
async _fetchBitcoinPrice() {
if (!soupSession) {
soupSession = new Soup.Session()
soupSession.user_agent =
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
}
const url = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT"
return new Promise((resolve, reject) => {
let message = new Soup.Message({
method: "GET",
uri: Soup.URI.new(url),
})
soupSession.queue_message(message, (session, response) => {
if (response.status_code === 200) {
try {
let data = JSON.parse(response.response_body.data)
let price = parseFloat(data.price)
resolve(price)
} catch (e) {
reject(new Error("Failed to parse JSON data from API response."))
}
} else {
reject(
new Error(
`API request failed with status code: ${response.status_code}`,
),
)
}
})
})
}
destroy() {
if (this._refreshTimeout) {
GLib.source_remove(this._refreshTimeout)
this._refreshTimeout = null
}
if (this.label) {
this.label.destroy()
this.label = null
}
if (soupSession) {
soupSession.abort()
soupSession = null
}
super.destroy()
}
},
)
class Extension {
constructor(uuid) {
this._uuid = uuid
ExtensionUtils.initTranslations(GETTEXT_DOMAIN)
}
enable() {
this._indicator = new Indicator()
Main.panel.addToStatusArea(this._uuid, this._indicator, 0, "right")
}
disable() {
if (this._indicator) {
this._indicator.destroy()
this._indicator = null
}
}
}
function init(meta) {
return new Extension(meta.uuid)
}