-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrqg.js
122 lines (89 loc) · 3.01 KB
/
rqg.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
var blockquoteText = document.getElementsByTagName("Blockquote")[0];
var buttonClick = document.getElementsByClassName("button")[0];
var quoteAuthor = document.getElementsByTagName("p")[0];
var twitterAnchor = document.getElementsByClassName("twitter-share-button")[0];
// Callback index.
var count = 0;
var jsonp = function(url, options) {
options = options || {};
var prefix = options.prefix || '__jp';
var param = options.param || '_jsonp';
var timeout = options.timeout ? options.timeout : 15000;
var target = document.getElementsByTagName('script')[0] || document.head;
var script;
var timer;
var cleanup;
var cancel;
var promise;
var noop = function() {};
// Generate a unique id for the request.
var id = prefix + (count++);
cleanup = function() {
// Remove the script tag.
if (script && script.parentNode) {
script.parentNode.removeChild(script);
}
window[id] = noop;
if (timer) {
clearTimeout(timer);
}
};
promise = new Promise(function(resolve, reject) {
if (timeout) {
timer = setTimeout(function() {
cleanup();
reject(new Error('Timeout'));
}, timeout);
}
window[id] = function(data) {
cleanup();
resolve(data);
};
// Add querystring component
console.log(url);
url += (~url.indexOf('?') ? '&' : '?') + param + '=' + encodeURIComponent(id);
url = url.replace('?&', '?');
// Create script.
script = document.createElement('script');
script.src = url;
target.parentNode.insertBefore(script, target);
cancel = function() {
if (window[id]) {
cleanup();
reject(new Error('Canceled'));
}
};
});
return {
promise: promise.then(function(a){
var filtered = a.filter(function(i){
if (i.content.length + i.title.length < 152){
return i;
}
})
arr.push(filtered);
return filtered;
}).then(function(a){
randomQuote();
buttonClick.addEventListener("click", randomQuote);
})
}
}
var arr = [];
function randomQuote(){
//update text
var randNum =Math.floor(Math.random() * arr[0].length);
var temp = document.createElement("div");
temp.innerHTML = arr[0][randNum].content;
var sanitized = temp.textContent || temp.innerText;
blockquoteText.textContent = sanitized;
document.getElementsByClassName("twitter-share-button")[0].setAttribute("href", "https://twitter.com/intent/tweet?text=" + sanitized);
//update author
//convert name to camel case
let titleCase = arr[0][randNum].title
.split(' ')
.map(i => i.charAt(0).toUpperCase() + i.slice(1).toLowerCase())
.join(' ');
document.getElementById('author').innerHTML = titleCase;
}
window.onload = jsonp("https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=30&");