-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Quotable.js
67 lines (61 loc) · 1.97 KB
/
MMM-Quotable.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
/* Magic Mirror
* Module: MMM-Quotable
*
* Made with lukePeavey's Quotable API https://github.com/lukePeavey/quotable
*/
Module.register("MMM-Quotable", {
// Define module defaults
defaults: {
updateInterval: 24 * 60 * 60 * 1000, // 1 day
fadeSpeed: 1000,
maxLength: 100,
tags: "motivational|inspirational"
},
// Define start sequence
start: function() {
var self = this;
self.quote = "Loading quote...";
self.author = "";
self.getQuote();
setInterval(function() {
self.getQuote();
}, self.config.updateInterval);
},
// Define required styles
getStyles: function() {
return ["MMM-Quotable.css"];
},
// Get a random quote from the Quotable API
getQuote: function() {
var self = this;
var maxLength = self.config.maxLength;
var tags = self.config.tags;
var url = "https://api.quotable.io/random?maxLength=" + maxLength + "&tags=" + tags;
fetch(url)
.then(response => response.json())
.then(data => {
var quoteText = data.content;
var quoteAuthor = data.author;
self.quote = quoteText;
self.author = quoteAuthor;
self.updateDom(self.config.fadeSpeed);
})
.catch(error => {
console.error(self.name + ": Could not load quote.", error);
});
},
// Override dom generator
getDom: function() {
var wrapper = document.createElement("div");
wrapper.className = "quotable";
var quote = document.createElement("div");
quote.className = "quote-text";
quote.innerHTML = this.quote;
wrapper.appendChild(quote);
var author = document.createElement("div");
author.className = "quote-author";
author.innerHTML = this.author;
wrapper.appendChild(author);
return wrapper;
}
});