-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.js
119 lines (97 loc) · 2.27 KB
/
net.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
//todo: indicate errors
/*
Singleton for doing stuff on the network.
*/
var Net = new function() {
this.profileNotFoundError = "The specified profile could not be found.";
this.profileNotFoundDisplay = "Profile not found";
/*
A simple wrapper for XMLHttpRequest.
Similar to GM_xmlhttprequest.
*/
this.XMLHttpRequest = function(details) {
var xhr = new XMLHttpRequest();
xhr.open(details.method, details.url, true);
if(details.headers) {
for(var i in details.headers)
xhr.setRequestHeader(i, details.headers[i]);
}
xhr.onreadystatechange = function() {
if(details.onreadystatechange) {
details.onreadystatechange(xhr);
}
if(xhr.readyState == 4) {
if(details.onload && xhr.status == 200) {
details.onload(xhr);
}
else if(details.onerror && xhr.status != 200) {
details.onerror(xhr);
}
}
}
xhr.send(details.data);
}
this.getUserModel = function(model, callback) {
if(typeof callback != "function") {
callback = function(){};
}
this.XMLHttpRequest({
method: "GET",
url: model.profileURLXML,
onload: function(xhr) {
var xml = xhr.responseXML;
var error;
if(xml) {
Parse.userModelXML(model, xml.documentElement);
error = Parse.error(xml);
}
callback(xhr, error);
},
onerror: function(xhr) {
callback(xhr);
}
});
}
this.getUserFriends = function(model, manager, callback) {
if(typeof callback != "function") {
callback = function(){};
}
this.XMLHttpRequest({
method: "GET",
url: model.friendsURLXML,
onload: function(xhr) {
var xml = xhr.responseXML;
var error;
if(xml) {
Parse.friendsXML(manager, xml.documentElement);
error = Parse.error(xml);
}
callback(xhr, error);
},
onerror: function(xhr) {
callback(xhr);
}
});
}
this.getUserGames = function(model, callback) {
if(typeof callback != "function") {
callback = function(){};
}
this.XMLHttpRequest({
method: "GET",
url: model.gamesURLXML,
onload: function(xhr) {
var xml = xhr.responseXML;
var error;
if(xml) {
Parse.gamesXML(model.games, xml.documentElement);
error = Parse.error(xml);
}
callback(xhr, error);
},
onerror: function(xhr) {
callback(xhr);
}
});
}
}