-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
115 lines (102 loc) · 2.88 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
$(document).ready(function () {
let listQuotes = [];
let currentQuote = 0;
let progressWidth = 0;
let progress;
function loadQuotesFromGitHub(rawURL) {
return fetch(rawURL)
.then(response => response.text())
.then(text => {
return text.split('\n').map(line => {
if (line.includes(' - ')) {
const [quote, author] = line.split(' - ');
return {
quote: quote.trim(),
author: author ? author.trim() : " ",
};
} else {
return {
quote: line.trim(),
author: " ",
};
}
}).filter(quoteObj => quoteObj.quote !== "");
});
}
function setQuote() {
if (listQuotes.length > 0) {
$(".quote").html(listQuotes[currentQuote].quote);
$(".author-name").html(listQuotes[currentQuote].author);
tweetQuote();
} else {
console.error('No quotes available to display');
}
}
function changeQuote() {
if (listQuotes.length > 0) {
currentQuote = (currentQuote + 1) % listQuotes.length;
setQuote();
}
}
function timerProgress() {
$(".quote-progress").width(progressWidth + "%");
if (progressWidth < 100) {
progressWidth += 0.1;
} else {
changeQuote();
progressWidth = 0;
}
}
function startProgress() {
if (progress) clearInterval(progress);
progress = setInterval(timerProgress, 10);
}
loadQuotesFromGitHub('./quotes.txt')
.then(quotes => {
listQuotes = quotes;
if (listQuotes.length > 0) {
setQuote();
startProgress();
} else {
console.error('No quotes loaded');
}
})
.catch(error => console.error('Error loading quotes:', error));
$(".previous").click(function () {
if (listQuotes.length > 0) {
currentQuote = (currentQuote - 1 + listQuotes.length) % listQuotes.length;
setQuote();
progressWidth = 0;
}
});
$(".next").click(function () {
changeQuote();
progressWidth = 0;
});
$(".random").click(function () {
if (listQuotes.length > 0) {
currentQuote = Math.floor(Math.random() * listQuotes.length);
setQuote();
progressWidth = 0;
}
});
window.twttr = (function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0],
t = window.twttr || {};
if (d.getElementById(id)) return t;
js = d.createElement(s);
js.id = id;
js.src = "https://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
t._e = [];
t.ready = function (f) {
t._e.push(f);
};
return t;
}(document, "script", "twitter-wjs"));
function tweetQuote() {
if (listQuotes.length > 0) {
$('#quote-tweet').attr('href', 'https://twitter.com/intent/tweet?&text=' + encodeURIComponent('"' + listQuotes[currentQuote].quote + '" ' + listQuotes[currentQuote].author));
}
}
});