-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetReview.js
More file actions
302 lines (243 loc) · 11.3 KB
/
GetReview.js
File metadata and controls
302 lines (243 loc) · 11.3 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
* Created by Tia on 25/05/17.
*/
//listens and waits for the DOM content to be loaded before calling the bindButtons function.
document.addEventListener('DOMContentLoaded', bindButtons2);
function bindButtons2() {
//adds a listener to the SeeReviewSubmit button for when it is clicked.
// When it is clicked, the function(input) is called.
document.getElementById('SeeReview').addEventListener('click', function(input){
//notes the url that we want to get the data from
var url = "https://www.goodreads.com/review/show.xml";
//creates a new XMlHTTPRequest
var Request = new XMLHttpRequest();
//sets the API Key and its framework through which our interaction with GoodReads is allowed and monitored
var APIKey = "&key=" + "PsiDYifchcrF6dDwoZUDw";
//gets the user whose shelf you want to find
var QueryString = document.getElementById("ReviewID").value;
var QueryStringComplete= "?id=" + QueryString;
//creates the url to request the information from GoodReads
var URLToRequestInfo = url + QueryStringComplete + APIKey;
//open the request to get the information from the url
//send "true" as the third argument, making this an asynchronous request.
//Note: an asynchronous request is one where our program can continue,
// and the browser will take care of the sending and receiving of data
// in the background.
Request.open("GET", URLToRequestInfo, true);
//since this is an asynchronous request, we need to create a listener
//that lets us know when the request returns (or has loaded).
Request.addEventListener('load', function() {
//if the request was successful, then change the xml to json using the
// xmlToJSON function from below. After this, put it into
//the ReturnedInfo function.
//Note that a request is considered successful if the status returned
//is between 200 and less than 400.
if (Request.status >= 200 && Request.status < 400) {
var Response = xmlToJSON2.parseString(Request.responseText);
ReturnedInfo2(Response);
} else { //else, there was an error. Let the user know by creating an
//alert that tells them the request to open GoodReads was unsuccesful.
var ErrorText = "Error in network request: " + Response.statusText;
alert(ErrorText);
}
});
//sends the request to GoodReads
Request.send();
// We call preventDefault on the event (called input) so we refresh the page
// and cause the browser to lose all of the information. That would
// break everything.
input.preventDefault();
});
}
//put all of the returned GoodReads info into the html elements by their id.
function ReturnedInfo2(Response) {
//note, you can go through the reviews by changing the description
document.getElementById('Review').innerHTML = Response.GoodreadsResponse[0].review[0].book[0].description[0]._text;
}
/****************************/
var xmlToJSON2 = (function () {
this.version = "1.3";
var options = { // set up the default options
mergeCDATA: true, // extract cdata and merge with text
grokAttr: true, // convert truthy attributes to boolean, etc
grokText: true, // convert truthy text/attr to boolean, etc
normalize: true, // collapse multiple spaces to single space
xmlns: true, // include namespaces as attribute in output
namespaceKey: '_ns', // tag name for namespace objects
textKey: '_text', // tag name for text nodes
valueKey: '_value', // tag name for attribute values
attrKey: '_attr', // tag for attr groups
cdataKey: '_cdata', // tag for cdata nodes (ignored if mergeCDATA is true)
attrsAsObject: true, // if false, key is used as prefix to name, set prefix to '' to merge children and attrs.
stripAttrPrefix: true, // remove namespace prefixes from attributes
stripElemPrefix: true, // for elements of same name in diff namespaces, you can enable namespaces and access the nskey property
childrenAsArray: true // force children into arrays
};
var prefixMatch = new RegExp(/(?!xmlns)^.*:/);
var trimMatch = new RegExp(/^\s+|\s+$/g);
this.grokType = function (sValue) {
if (/^\s*$/.test(sValue)) {
return null;
}
if (/^(?:true|false)$/i.test(sValue)) {
return sValue.toLowerCase() === "true";
}
if (isFinite(sValue)) {
return parseFloat(sValue);
}
return sValue;
};
this.parseString = function (xmlString, opt) {
return this.parseXML(this.stringToXML(xmlString), opt);
}
this.parseXML = function (oXMLParent, opt) {
// initialize options
for (var key in opt) {
options[key] = opt[key];
}
var vResult = {},
nLength = 0,
sCollectedTxt = "";
// parse namespace information
if (options.xmlns && oXMLParent.namespaceURI) {
vResult[options.namespaceKey] = oXMLParent.namespaceURI;
}
// parse attributes
// using attributes property instead of hasAttributes method to support older browsers
if (oXMLParent.attributes && oXMLParent.attributes.length > 0) {
var vAttribs = {};
for (nLength; nLength < oXMLParent.attributes.length; nLength++) {
var oAttrib = oXMLParent.attributes.item(nLength);
vContent = {};
var attribName = '';
if (options.stripAttrPrefix) {
attribName = oAttrib.name.replace(prefixMatch, '');
} else {
attribName = oAttrib.name;
}
if (options.grokAttr) {
vContent[options.valueKey] = this.grokType(oAttrib.value.replace(trimMatch, ''));
} else {
vContent[options.valueKey] = oAttrib.value.replace(trimMatch, '');
}
if (options.xmlns && oAttrib.namespaceURI) {
vContent[options.namespaceKey] = oAttrib.namespaceURI;
}
if (options.attrsAsObject) { // attributes with same local name must enable prefixes
vAttribs[attribName] = vContent;
} else {
vResult[options.attrKey + attribName] = vContent;
}
}
if (options.attrsAsObject) {
vResult[options.attrKey] = vAttribs;
} else {}
}
// iterate over the children
if (oXMLParent.hasChildNodes()) {
for (var oNode, sProp, vContent, nItem = 0; nItem < oXMLParent.childNodes.length; nItem++) {
oNode = oXMLParent.childNodes.item(nItem);
if (oNode.nodeType === 4) {
if (options.mergeCDATA) {
sCollectedTxt += oNode.nodeValue;
} else {
if (vResult.hasOwnProperty(options.cdataKey)) {
if (vResult[options.cdataKey].constructor !== Array) {
vResult[options.cdataKey] = [vResult[options.cdataKey]];
}
vResult[options.cdataKey].push(oNode.nodeValue);
} else {
if (options.childrenAsArray) {
vResult[options.cdataKey] = [];
vResult[options.cdataKey].push(oNode.nodeValue);
} else {
vResult[options.cdataKey] = oNode.nodeValue;
}
}
}
} /* nodeType is "CDATASection" (4) */
else if (oNode.nodeType === 3) {
sCollectedTxt += oNode.nodeValue;
} /* nodeType is "Text" (3) */
else if (oNode.nodeType === 1) { /* nodeType is "Element" (1) */
if (nLength === 0) {
vResult = {};
}
// using nodeName to support browser (IE) implementation with no 'localName' property
if (options.stripElemPrefix) {
sProp = oNode.nodeName.replace(prefixMatch, '');
} else {
sProp = oNode.nodeName;
}
vContent = xmlToJSON.parseXML(oNode);
if (vResult.hasOwnProperty(sProp)) {
if (vResult[sProp].constructor !== Array) {
vResult[sProp] = [vResult[sProp]];
}
vResult[sProp].push(vContent);
} else {
if (options.childrenAsArray) {
vResult[sProp] = [];
vResult[sProp].push(vContent);
} else {
vResult[sProp] = vContent;
}
nLength++;
}
}
}
} else if (!sCollectedTxt) { // no children and no text, return null
if (options.childrenAsArray) {
vResult[options.textKey] = [];
vResult[options.textKey].push(null);
} else {
vResult[options.textKey] = null;
}
}
if (sCollectedTxt) {
if (options.grokText) {
var value = this.grokType(sCollectedTxt.replace(trimMatch, ''));
if (value !== null && value !== undefined) {
vResult[options.textKey] = value;
}
} else if (options.normalize) {
vResult[options.textKey] = sCollectedTxt.replace(trimMatch, '').replace(/\s+/g, " ");
} else {
vResult[options.textKey] = sCollectedTxt.replace(trimMatch, '');
}
}
return vResult;
}
// Convert xmlDocument to a string
// Returns null on failure
this.xmlToString = function (xmlDoc) {
try {
var xmlString = xmlDoc.xml ? xmlDoc.xml : (new XMLSerializer()).serializeToString(xmlDoc);
return xmlString;
} catch (err) {
return null;
}
}
// Convert a string to XML Node Structure
// Returns null on failure
this.stringToXML = function (xmlString) {
try {
var xmlDoc = null;
if (window.DOMParser) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlString, "text/xml");
return xmlDoc;
} else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(xmlString);
return xmlDoc;
}
} catch (e) {
return null;
}
}
return this;
}).call({});
if (typeof module != "undefined" && module !== null && module.exports) module.exports = xmlToJSON;
else if (typeof define === "function" && define.amd) define(function() {return xmlToJSON});