-
Notifications
You must be signed in to change notification settings - Fork 13
/
MMM-Dad-Jokes.js
executable file
·63 lines (51 loc) · 1.23 KB
/
MMM-Dad-Jokes.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
/* global Module */
/* Magic Mirror
* Module: MMM-Dad-Jokes
*
* By Eric Chang
* MIT Licensed.
*/
"use strict";
Module.register("MMM-Dad-Jokes", {
result: {joke: "loading dad joke..."},
defaults: {
title: "Dad Jokes",
updateInterval: 60*1000, // every 60 seconds
fadeSpeed: 4*1000, // four seconds
filters: [],
},
start: function() {
this.getJoke();
this.scheduleUpdate();
},
getDom: function() {
var wrapper = document.createElement("div");
var joke = document.createElement("div");
joke.className = "bright light medium";
joke.style.textAlign = "center";
joke.style.margin = "0 auto";
joke.innerHTML = this.result["joke"];
wrapper.appendChild(joke);
return wrapper;
},
getJoke: function () {
this.sendSocketNotification("GET_JOKE");
},
scheduleUpdate: function() {
setInterval(() => {
this.getJoke();
}, this.config.updateInterval);
},
socketNotificationReceived: function(notification, payload) {
if (notification == "JOKE_RESULT") {
if( this.config.filters.some( term => payload.joke.toLowerCase().indexOf(term) > -1 ) ) {
// Filter matched, skip this joke
this.getJoke();
}
else {
this.result = payload;
this.updateDom(this.config.fadeSpeed);
}
}
},
});