/
index.html
90 lines (72 loc) · 3.18 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cotação dólar</title>
</head>
<body>
<script>
const {ipcRenderer} = require("electron");
const moment = require("moment");
class CotacaoUolWindow {
constructor() {
this.iconSize = 32;
this.wantsLatestRate = true;
this.wantsLatestTime = false;
this.wantsChart = true;
this.canvas = document.createElement("canvas");
this.canvas.width = this.iconSize;
this.canvas.height = this.iconSize;
this.context = this.canvas.getContext("2d");
ipcRenderer.on("dollar", (sender, ...args) => this.generateNewTrayIcon(...args));
}
/**
* @param {DataPoint[]} points
*/
generateNewTrayIcon(points) {
points = points.slice(-16);
const latest = points[points.length - 1];
this.context.clearRect(0, 0, this.iconSize, this.iconSize);
this.context.fillStyle = "black";
this.context.fillRect(0, 0, 32, 32);
if (this.wantsChart && points.length > 1) {
const selectedPoints = points.slice(-this.iconSize); // latest points (as many as there are pixels)
const selectedRates = selectedPoints.map(point => point.ask);
const min = selectedRates.reduce((min, val) => val < min ? val : min, Number.POSITIVE_INFINITY);
const max = selectedRates.reduce((max, val) => val > max ? val : max, Number.NEGATIVE_INFINITY);
const gap = max - min;
const scaleX = i => Math.round(this.iconSize * i / (selectedRates.length - 1));
const scaleY = val => gap > 0 ? this.iconSize - Math.round(this.iconSize * (val - min) / gap) : 0;
this.context.fillStyle = "green";
this.context.beginPath();
this.context.moveTo(scaleX(0), scaleY(selectedRates[0]));
for (let i = 1; i < selectedRates.length; i++) {
const rate = selectedRates[i];
this.context.lineTo(scaleX(i), scaleY(rate));
}
this.context.lineTo(this.iconSize, this.iconSize);
this.context.lineTo(0, this.iconSize);
this.context.closePath();
this.context.fill();
}
this.context.fillStyle = "white";
this.context.textAlign = "right";
if (this.wantsLatestRate) {
const dollarStr = latest.ask.toFixed(2);
this.context.font = "16px arial";
const top = this.wantsLatestTime ? this.iconSize / 2 : this.iconSize * .70;
this.context.fillText(dollarStr, this.iconSize, top);
}
if (this.wantsLatestTime) {
const timeStr = moment(latest.ts).format("HH:mm");
this.context.font = "12px arial";
this.context.fillText(timeStr, this.iconSize, this.iconSize);
}
const dataUrl = this.canvas.toDataURL();
ipcRenderer.send("tray-icon", dataUrl);
}
}
new CotacaoUolWindow();
</script>
</body>
</html>