-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfocus.js
85 lines (65 loc) · 2.79 KB
/
focus.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var timeElement = document.getElementById("time");
var dateElement = document.getElementById("date");
var bgImageElement = document.getElementById("unsplash-img");
var refreshImageIconElement = document.getElementById("refreshImageIcon");
var quoteElement = document.getElementById("quote");
var copyQuoteIconElement = document.getElementById("copyQuoteIcon");
var authorElement = document.getElementById("author");
var refreshQuoteIconElement = document.getElementById("refreshQuoteIcon");
const copyQuoteToClipboard = () => {
var range = document.createRange();
range.selectNode(quoteElement);
window.getSelection().removeAllRanges(); // clear current selection
window.getSelection().addRange(range); // to select text
document.execCommand("copy");
window.getSelection().removeAllRanges(); // to deselect
copyQuoteIconElement.innerHTML = "<i class='fa fa-check fa-lg'></i>";
};
const getBackgroundImage = async () => {
refreshImageIconElement.style.visibility = "hidden";
const apiUrl = "https://source.unsplash.com/random/1600x900";
var response = await fetch(apiUrl);
var imageURL = response.url;
bgImageElement.style.backgroundImage = `url(${imageURL})`;
bgImageElement.style.backgroundRepeat = "no-repeat";
bgImageElement.style.backgroundSize = "cover";
refreshImageIconElement.style.visibility = "visible";
refreshImageIconElement.onclick = () => getBackgroundImage();
refreshImageIconElement.title = "Get new Image";
};
const getQuote = async () => {
const apiUrl = "https://api.quotable.io/random";
copyQuoteIconElement.innerHTML = "<i class='fa fa-copy fa-lg'></i>";
copyQuoteIconElement.style.visibility = "hidden";
var response = await fetch(apiUrl);
var json = await response.json();
var quote = json.content;
var author = json.author;
quoteElement.innerText = quote;
authorElement.innerText = author;
copyQuoteIconElement.style.visibility = "visible";
copyQuoteIconElement.onclick = () => copyQuoteToClipboard();
copyQuoteIconElement.title = "Copy Quote to Clipboard";
refreshQuoteIconElement.onclick = () => getQuote();
refreshQuoteIconElement.title = "Get new Quote";
};
const getTime = () => {
var currentTime = new Date();
var showDate = currentTime.toLocaleDateString("en-US", {
day: "numeric",
month: "short",
year: "numeric",
});
var showTime = currentTime.toLocaleString("en-US", {
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: true,
});
dateElement.innerText = showDate.toString();
timeElement.innerText = showTime.toString();
setInterval(getTime, 1000);
};
getBackgroundImage();
getQuote();
getTime();