-
Notifications
You must be signed in to change notification settings - Fork 3
/
foodSelect.js
74 lines (66 loc) · 2.83 KB
/
foodSelect.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
/*
* Copyright (c) 2022. Gwen Tripet-Costet
* This file is part of Better Equideow (Better Howrse).
* Better Equideow (Better Howrse) is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Better Equideow (Better Howrse) is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
class FoodSelect {
constructor() {
this.feedingBtn = document.getElementById("boutonNourrir")
this.haySlider = document.getElementById("haySlider") || null
this.haySelectors = this.haySlider?.getElementsByTagName("span")
this.oatsSlider = document.getElementById("oatsSlider") || null
this.oatsSelectors = this.oatsSlider?.getElementsByTagName("span")
this.careTabFeed = document.getElementById('care-tab-feed')
this.messageBox = this.careTabFeed.querySelector("#messageBoxInline")?.textContent
this.fourrageNode = document.getElementsByClassName("section-fourrage section-fourrage-target")[0]
this.avoineNode = document.getElementsByClassName("section-avoine section-avoine-target")[0]
}
getFoodIndex(foodNode) {
if (this.messageBox && foodNode === this.fourrageNode) {
return this.messageBox.includes('20') ? 20 : 0
}
else {
const foodValue = foodNode.textContent
const foodIndex = parseInt(foodValue)
return foodIndex
}
}
async run() {
chrome.storage.sync.get({ 'foodSelect': true }, (data) => {
if (data.foodSelect) {
if (this.fourrageNode) {
const fourrageIndex = this.getFoodIndex(this.fourrageNode)
this.haySelectors[fourrageIndex].click()
}
if (this.avoineNode) {
const avoineIndex = this.getFoodIndex(this.avoineNode)
this.oatsSelectors[avoineIndex].click()
}
}
})
}
}
const horseFeedingPageRegex = /\/elevage\/chevaux\/cheval\?/
if (window.location.href.match(horseFeedingPageRegex)) {
let foodSelect = new FoodSelect()
if (foodSelect.feedingBtn !== null) {
foodSelect.feedingBtn.addEventListener("click", () => {
foodSelect.run()
})
}
/**
* @description generate a new FoodSelect() because after #loading style change,
* it seems like the different this.elements from foodSelect are erased ..
* @todo fix it...
*/
const startObserver = () => {
observer.start().then(() => {
let newFoodSelect = new FoodSelect()
newFoodSelect.run()
startObserver()
})
}
startObserver()
}