-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
194 lines (138 loc) · 6.2 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// =============== Page Loader ===============
document.addEventListener("DOMContentLoaded", function () {
document.body.classList.add("stop-scrolling");
setTimeout(() => {
const pageLoaderContainer = document.querySelector(".pageLoader-container");
pageLoaderContainer.classList.add("closeLoader");
document.body.classList.remove("stop-scrolling");
}, 3000);
});
const quoteText = document.querySelector(".quote"),
authorName = document.querySelector(".author .name"),
dateAdded = document.querySelector(".date .dateAdded"),
quoteBtn = document.querySelector("#newQuote"),
tag = document.getElementById('tag'),
// translateBtn = document.querySelector(".translate"),
soundBtn = document.querySelector(".sound"),
copyBtn = document.querySelector(".copy"),
twitterBtn = document.querySelector(".twitter"),
translateBtn = document.querySelector(".translate"),
htmlEle = document.getElementById("google_translate_element");
const url = "https://api.quotable.io/random?tags=technology|love|happiness|history|science&minLength=50&maxLength=100";
function randomQuote() {
tag.innerHTML = "";
quoteBtn.classList.add("loading");
quoteBtn.innerText = "Loading Quote...";
// Fetching random quotes from the API and parsing them into a javascript object
fetch(url)
.then(res => res.json())
.then(result => {
// console.log(result);
let mydate = new Date(result.dateAdded);
quoteText.innerHTML = result.content;
authorName.innerHTML = result.author;
dateAdded.innerHTML = mydate.toDateString();
result.tags.forEach(t => {
tag.innerHTML += "<span>#" + t + " </span>";
});
quoteBtn.innerText = "New Quote";
quoteBtn.classList.remove("loading");
});
}
//======= Sound Feature Implementation ===============================
soundBtn.addEventListener("click", () => {
document.getElementById("alert").innerText = "Speaking...";
document.getElementById("alert").style.display = "block";
setTimeout(() => {
document.getElementById("alert").innerText = "";
document.getElementById("alert").style.display = "none";
}, 2500);
// SpeechSynthesisUtterance is a web speech api that represents a speech request
let msg = new SpeechSynthesisUtterance(`${quoteText.innerText}`);
soundBtn.classList.add("loading");
speechSynthesis.speak(msg); // Speak mehtod of speechSynthesis that speak the msg
msg.addEventListener('end', (evt) => {
const { charIndex, utterance } = evt;
if (charIndex + 1 === utterance.text.length) {
// End fired when utterance finished
console.log("Text not Finished");
} else {
// console.log("Text is Finished");
soundBtn.classList.remove("loading");
}
});
});
//====== Copy Feature Implementation ==================================
copyBtn.addEventListener("click", () => {
document.getElementById("alert").innerText = "Copied to clipboard!";
document.getElementById("alert").style.display = "block";
setTimeout(() => {
document.getElementById("alert").innerText = "";
document.getElementById("alert").style.display = "none";
}, 2500);
// Copy the quote's text to clipboard
// writeText() property writes the specified text string to the system clipboard
navigator.clipboard.writeText(quoteText.innerText);
});
//======== Twitter Feature Implementation =================================
twitterBtn.addEventListener("click", () => {
let twitterUrl = `https://twitter.com/intent/tweet?url=${quoteText.innerText},${authorName.innerText}`;
window.open(twitterUrl, '_blank'); // open a new twitter window with passing quote in the url
});
// ======= Translations Implementation ===================================
function googleTranslateElementInit() {
new google.translate.TranslateElement({ pageLanguage: 'en', autoDisplay: false }, 'google_translate_element');
}
// ======== ScreenShot Capture Implementation =================================
// Define the function
// to screenshot the div
function takeshot() {
let capturedDiv = document.getElementById('photo');
document.getElementById('outputImage').innerHTML = "";
// Use the html2canvas
// function to take a screenshot
// and append it
// to the output div
html2canvas(capturedDiv).then(
function (canvas) {
var canvas_data = canvas.toDataURL();
var img_element = document.createElement("img")
img_element.src = canvas_data;
document.getElementById('outputImage').appendChild(img_element);
document.getElementById('outputImgContainer').style.display = "flex";
document.body.classList.add("stop-scrolling");
});
}
document.getElementById("capture").addEventListener("click", takeshot);
document.getElementById("closeCaptureImg").addEventListener("click", () => {
document.getElementById('outputImgContainer').style.display = "none";
document.body.classList.remove("stop-scrolling");
});
window.addEventListener("load", randomQuote);
quoteBtn.addEventListener("click", randomQuote);
// =================================================== Notes for coming updates =================================
// Facebook Feature Implementation
// facebookBtn.addEventListener("click", () => {
// FB.ui(
// {
// method: 'feed',
// caption: 'Caption like which appear as title of the dialog box',
// description: 'Small description of the post',
// message: 'Hello World!'
// }
// );
// // window.open(twitterUrl, '_blank'); // open a new twitter window with passing quote in the url
// });
// (function () {
// // if (htmlEle.getAttribute("lang") === 'ar') {
// // container.style.cssText = "direction: rtl;";
// // tag.style.cssText = "text-align: right;";
// // date.style.cssText = "text-align: right;";
// // }
// })();
// myTimeout = setTimeout(function () {
// language = htmlEle.lang;
// }, 100);
// setInterval(function () {
// language = htmlEle.lang;
// }, 1000);