-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMMM-XMLViewYbbet.js
More file actions
97 lines (90 loc) · 2.73 KB
/
MMM-XMLViewYbbet.js
File metadata and controls
97 lines (90 loc) · 2.73 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
Module.register("MMM-XMLViewYbbet", {
defaults: {
xmlURL: "./modules/MMM-XMLViewYbbet/example.xml",
itemLife: 1000 * 5,
scanInterval: 1000 * 60 * 60,
},
start: function () {
this.scanTimer = null
this.drawTimer = null
this.items = []
this.currentItem = {}
this.index = 0
},
getDom: function () {
let dom = document.createElement('div')
if (this.currentItem) {
if (this.currentItem?.title) {
let t = document.createElement('div')
t.classList.add('title')
t.innerHTML = this.currentItem.title
dom.appendChild(t)
}
if (this.currentItem?.image) {
let t = document.createElement('img')
t.classList.add('image')
t.src = this.currentItem.image
dom.appendChild(t)
}
if (this.currentItem?.description) {
let t = document.createElement('div')
t.classList.add('description')
t.innerHTML = this.currentItem.description
dom.appendChild(t)
}
}
return dom
},
notificationReceived: function (notification, payload, sender) {
if (notification === 'DOM_OBJECTS_CREATED') this.scan();
},
scan: function () {
if (this.scanTimer) {
clearTimeout(this.scanTimer)
this.scanTimer = null
}
let xhttp = new XMLHttpRequest()
let self = this
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
let data = []
xmlDoc = xhttp.responseXML
console.log(xmlDoc)
const items = xmlDoc.getElementsByTagName('item')
for (i = 0; i < items.length; i++) {
let image = items[ i ].getElementsByTagName('image')?.[ 0 ].textContent ?? null
let title = items[ i ].getElementsByTagName('title')?.[ 0 ].textContent ?? null
let description = items[ i ].getElementsByTagName('description')?.[ 0 ].textContent ?? null
if (image || title || description) data.push({ title, image, description })
}
if (data.length > 0) {
Log.log(`Scanned ${data.length} items`)
self.items = data
self.index = 0
self.draw()
}
}
}
xhttp.open("GET", this.config.xmlURL, true)
xhttp.send()
this.scanTimer = setTimeout(() => {
this.scan()
}, this.config.scanInterval)
},
draw: function () {
if (this.items.length < 1) {
Log.warn('There is no item to draw')
return
}
if (this.drawTimer) {
clearTimeout(this.drawTimer)
this.drawTimer = null
}
this.currentItem = this.items[ this.index++ ]
if (this.index >= this.items.length) this.index = 0
this.updateDom()
this.drawTimer = setTimeout(() => {
this.draw()
}, this.config.itemLife)
}
})