forked from imabhi2000/quote-generator-updated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (43 loc) · 1.49 KB
/
script.js
File metadata and controls
49 lines (43 loc) · 1.49 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
//get quotes from API
var btn = document.getElementById('btn');
var quote = document.getElementById('quoteplace');
var author = document.getElementById("authorplace");
var twitter = document.getElementById('tweet');
var textplace = document.getElementById('textplace');
function getQuotes() {
console.log('button pressed');
const url = 'https://goquotes-api.herokuapp.com/api/v1/random?count=1';
const xhreq = new XMLHttpRequest();
xhreq.onload = function () {
if (xhreq.status === 200) {
console.log(xhreq.status);
var data = JSON.parse(xhreq.response);
var obj = data.quotes[0];
if (!obj.author) {
obj.author = 'unknown';
}
if (obj["text"].length > 120) {
textplace.style.fontSize = "3.2rem";
}
quote.innerHTML = obj.text;
author.innerHTML = obj.author;
console.log(obj.text);
console.log(obj.text.length);
console.log(obj.author);
} else {
console.log(xhreq.status);
console.log('error establishing the connection');
}
}
xhreq.open('get', url);
xhreq.send();
}
function newpage() {
let q1 = quote.innerHTML;
let a1 = author.innerHTML;
let turl = `https://twitter.com/intent/tweet?text=${q1}-${a1}`;
window.open(turl, "-blank");
};
btn.addEventListener('click', getQuotes);
twitter.addEventListener('click', newpage);
console.log("script working");