forked from kevinchevallier/dev-wisdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotes.js
executable file
·25 lines (20 loc) · 784 Bytes
/
quotes.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
function random(array) {
return array[Math.floor(Math.random() * array.length)];
}
var xmlhttp = new XMLHttpRequest();
var url = "./quotes.json";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
jsonLoaded(myArr);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function jsonLoaded(arr) {
var randomItem = random(arr.lines);
var isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
document.getElementById('line').innerHTML = randomItem.text ;
document.getElementById('author').innerHTML = '- ' + randomItem.author;
document.body.style.backgroundColor = random(isDarkMode ? arr.colorsDarkMode :arr.colors );
}