This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentScript.js
95 lines (85 loc) · 2.86 KB
/
contentScript.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
// Build code as string
var actualCode = '(' + function () {
// Setup
const zoomChart = (from, to) => {
wrChart.data.datasets =
originalDatasets.map(dataset =>
({
...dataset,
data: dataset.data.filter(data =>
data.x * 1000 >= from && data.x * 1000 <= to
),
})
)
wrChart.update();
}
let fromInputElement;
let toInputElement;
let lowestValue;
let highestValue;
const validateFields = () => {
let from;
let to;
// Validation
if (fromInputElement.value) {
fromInputElement.classList.remove('border-danger');
from = new Date(fromInputElement.value).getTime();
} else {
fromInputElement.classList.add('border-danger');
from = lowestValue;
}
if (toInputElement.value) {
toInputElement.classList.remove('border-danger');
to = new Date(toInputElement.value).getTime();
} else {
toInputElement.classList.add('border-danger');
to = highestValue;
}
return [from, to + day1];
}
const day1 = 86400000 - 1;
window.onWrChartFromToChange = () => zoomChart(...validateFields());
const htmlToElement = (html) => {
var template = document.createElement('template');
template.innerHTML = html;
return template.content.firstChild;
}
const zoomDateForm = htmlToElement(
`<div>
<div class="form-group row">
<div class="col-2 col-md-1 text-right col-label-padding">
<label for="wrChart-from">From: </label>
</div>
<div>
<input id="wrChart-from" class="form-control" onchange="onWrChartFromToChange()" type="date">
</div>
</div>
<div class="form-group row">
<div class="col-2 col-md-1 text-right col-label-padding">
<label for="wrChart-to">To: </label>
</div>
<div>
<input id="wrChart-to" class="form-control" onchange="onWrChartFromToChange()" type="date">
</div>
</div>
</div>`
);
// Obtain the chart
const wrChart = window.Chart.instances[0];
const originalDatasets = wrChart.config.data.datasets;
// Create elements in page
document.getElementById('wrChart').parentNode.appendChild(zoomDateForm);
fromInputElement = document.getElementById('wrChart-from');
toInputElement = document.getElementById('wrChart-to');
const allDateNumbers = originalDatasets.flatMap(datasets => datasets.data).map(data => data.x)
lowestValue = Math.min(...allDateNumbers) * 1000;
highestValue = Math.max(...allDateNumbers) * 1000
fromInputElement.valueAsDate = new Date(lowestValue);
toInputElement.valueAsDate = new Date(highestValue);
} + ')();';
// Inject the code string into the page
// We have to do this to access Chart
const script = document.createElement('script');
script.textContent = actualCode;
(document.head || document.documentElement).appendChild(script);
script.remove();