-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
57 lines (46 loc) · 2.04 KB
/
script.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
var newQuoteBtn = document.getElementById("button2");
var tweet = document.getElementById("button1");
var quote; // holds the quote recieved from the api call
var author; // holds the name of the author recieved from the api call
function getNewQuote() {
var xmlHTTP = new XMLHttpRequest();
var url = "https://cors-anywhere.herokuapp.com/https://api.forismatic.com/api/1.0/?method=getQuote&key=457653&format=json&lang=en";
xmlHTTP.open('GET', url, true);
xmlHTTP.send();
xmlHTTP.onload = function() {
if (xmlHTTP.status === 200) {
var recieved = xmlHTTP.response;
recieved = JSON.parse(recieved);
quote = recieved.quoteText;
author = recieved.quoteAuthor;
// set the value of the quote along with formatting
var quoteString = '<i class="fa fa-quote-left"></i> ' + quote + ' <i class="fa fa-quote-right"></i>';
// display the element
document.getElementById("a-quote").innerHTML = quoteString;
// set the value of author to quoteAuthor if an empty value is not recieved
// and display the author name
author ? document.getElementById("author-id").innerHTML = " - " + author : document.getElementById("author-id").innerHTML = " - unknown";
} else {
console.log(xmlHTTP.status);
}
};
}
getNewQuote(); // invoke the function for the first time
function randomColor() {
var x = Math.floor(Math.random() * 256);
var y = Math.floor(Math.random() * 256);
var z = Math.floor(Math.random() * 256);
var bgColor = "rgb(" + x + "," + y + "," + z + ")";
document.body.style.background = bgColor;
document.getElementById("button1").style.background = bgColor;
document.getElementById("button2").style.background = bgColor;
document.getElementById("quote-id").style.color = bgColor;
document.getElementById("author-id").style.color = bgColor;
}
newQuoteBtn.addEventListener("click", function() {
getNewQuote();
randomColor();
});
tweet.addEventListener("click", function() {
window.open('http://twitter.com/intent/tweet?text=' + encodeURIComponent(quote + ' - ' + author));
});