-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbGrab.js
179 lines (139 loc) · 8.6 KB
/
dbGrab.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
//CyberduckCheck
//check length of URL to see if query is present
if(document.location.search.length) {
//query has been found
//request the DB
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
//if db state is ready
if (this.readyState == 4 && this.status == 200) {
console.log("Got DB, Now to get the query");
// Grab the quoteId out of the URL by it's name
function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var quoteId = getParameterByName('quoteid');
console.log(quoteId);
// get the file contents
var fileContent = this.responseText;
// split into lines
var fileContentLines = fileContent.split( '\n' );
// extract the value
var setLine = fileContentLines[ quoteId ];
console.log (setLine);
console.log (quoteId);
// put the content into the divs and hide the loader
setTimeout(function(){
// get total count
$("#quoteTotal").html(fileContentLines.length - 1);
// add in the quoteId to the content
$("#quoteNumber").html(quoteId);
// split line and set to divs
var res = setLine.split("^");
$("#nameDiv").html(res[0]);
$("#contextDiv").html(res[1]);
$("#quoteDiv").html(res[2]);
$("#linkDiv").html(res[3]);
//generate URL
var url=window.location.href,
separator = (url.indexOf("?")===-1)?"?":"&",
newParam=separator + "quoteid=" + (quoteId);
newUrl=url.replace(newParam,"");
newUrl+=newParam;
//window.location.href =newUrl;
console.log(newUrl);
//trim quote to fit for twitter
var fullString = res[2];
var trimmedString = res[2].substring(0, 55) + "...";
console.log (trimmedString);
if (fullString.length > 55) {
$("#twitterLink").attr("href","https://twitter.com/intent/tweet?text=" + trimmedString + " via " + newUrl + " %23UX %23design %23quote #wordsireadsomewhere ");
}
else{
$("#twitterLink").attr("href","https://twitter.com/intent/tweet?text=" + fullString + " via " + newUrl + " %23UX %23design %23quote #wordsireadsomewhere ");
}
$("#directURL").attr("href", newUrl );
$("#directURL").html( newUrl );
//hide loader
var status = ("ready");
console.log (status);
if (status == "ready"){
$(".se-pre-con").fadeOut("slow");
}
}, 2000);
}
}
} else {
//when no query is found generate random quote
console.log("No query. Grabbing a random quote!");
console.log("Just trying to track down the DB, give me a second.");
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log("I have the database! Woo!");
// get the file contents
var fileContent = this.responseText;
// split into lines
var fileContentLines = fileContent.split( '\n' );
// get a random index (line number)
var randomLineIndex = Math.floor( Math.random() * fileContentLines.length);
// extract the value
var randomLine = fileContentLines[ randomLineIndex ];
var displayCount = randomLineIndex;
//wait to make sure it's loaded the quotes properly.
setTimeout(function(){
// get total count
var lineCount = fileContentLines.length;
$("#quoteTotal").html(lineCount);
// add the random line in a div
// document.getElementById( 'random-phrase' ).innerHTML = randomLine;
$("#random-phrase").html(randomLine);
// document.getElementById( 'quoteNumber' ).innerHTML = randomLineIndex;
$("#quoteNumber").html(displayCount);
console.log("Ok, I Did some technical math things and have the lines all counted.");
// split randomLine and set to divs
var res = randomLine.split("^");
$("#nameDiv").html(res[0]);
$("#contextDiv").html(res[1]);
$("#quoteDiv").html(res[2]);
$("#linkDiv").html(res[3]);
var url=window.location.href,
separator = (url.indexOf("?")===-1)?"?":"&",
newParam=separator + "quoteid=" + (randomLineIndex);
newUrl=url.replace(newParam,"");
newUrl+=newParam;
//window.location.href =newUrl;
console.log(newUrl);
//trim quote to fit for twitter
var fullString = res[2];
var trimmedString = res[2].substring(0, 55) + "...";
console.log (trimmedString);
if (fullString.length > 55) {
$("#twitterLink").attr("href","https://twitter.com/intent/tweet?text=" + trimmedString + " via %23wordsireadsomewhere " + newUrl + " %23UX %23design %23quote ");
}
else{
$("#twitterLink").attr("href","https://twitter.com/intent/tweet?text=" + fullString + " via %23wordsireadsomewhere " + newUrl + " %23UX %23design %23quote ");
}
$("#directURL").attr("href", newUrl );
$("#directURL").html( newUrl );
//hide loader
var status = ("ready");
console.log (status);
if (status == "ready"){
$(".se-pre-con").fadeOut("slow");
}
}, 2000);
}
};
}
//get DB file
request.open( 'GET', 'http://www.wordsireadsomewhere.com/uxQuotesDB.txt', true );
request.send();