users.js
3.02 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
var USER = {
xhr: new XMLHttpRequest(),
devid: getUrlArg("devid"),
user: getUrlArg("user"),
userb64: null,
info: null,
save : function()
{
setCookie("user", this.userb64, 365);
},
load : function(callback)
{
this.xhr.onreadystatechange = function() {
if (this.readyState != 4) return;
if (this.status == 404) {
if (!USER.devid) {
USER.devid = window.prompt("Please enter your device ID");
}
self.setTimeout(callback, 0);
return;
} else if (this.status != 200) {
if (this.status) {
alert("Server under maintenance (status: " + this.status + ")");
}
return;
}
if (!USER.devid) USER.devid = getCookie("devid");
USER.info = JSON.parse(this.responseText);
if (USER.info && USER.info.devid) {
if (USER.devid) {
var found = false;
for (var i = 0; i < USER.info.devid.length; i++) {
if (USER.info.devid[i] == USER.devid) {
found = true;
break;
}
}
//if (!found) USER.devid = USER.info.devid[0];
} else {
USER.devid = USER.info.devid[0];
}
self.setTimeout(callback, 0);
} else {
alert("No activated device for the specified user");
self.setTimeout(callback, 0);
return;
}
}
this.userb64 = this.user ? btoa(this.user) : getCookie("user");
if (!this.devid) this.devid = getCookie("devid");
if (!this.devid) {
var tosave = false;
if (!this.userb64) {
var input = window.prompt("Please enter your email or device ID");
if (!input) return;
if (input.indexOf("@") <= 0 && input.indexOf("#") <= 0) {
this.devid = input;
} else {
this.userb64 = btoa(input);
tosave = true;
}
}
if (this.userb64 && tosave) {
this.save();
}
}
if (!this.userb64) {
if (this.devid) {
self.setTimeout(callback, 0);
} else {
alert("Please specify a device ID");
}
} else {
var url = serverURL + "query?user=" + this.userb64;
this.xhr.open('GET', url, true);
this.xhr.send(null);
}
},
check : function(callback)
{
this.xhr.onreadystatechange = function() {
if (this.readyState != 4) return;
if (this.status != 200) {
if (this.status) {
alert("Server under maintenance (status: " + this.status + ")");
}
return;
}
USER.info = JSON.parse(this.responseText);
if (callback) self.setTimeout(callback, 0);
}
this.userb64 = this.user ? btoa(this.user) : getCookie("user");
if (this.userb64) {
var url = serverURL + "query?user=" + this.userb64;
this.xhr.open('GET', url, true);
this.xhr.send(null);
} else {
if (callback) self.setTimeout(callback, 0);
}
},
goDash : function(devid)
{
setCookie("devid", devid, 7);
var s = "?";
if (this.user) s += "user=" + this.user + "&";
s += "devid=" + devid;
location.href = s;
},
goNextDash: function(devid)
{
if (this.info.devid) {
for (var i = 0; i < this.info.devid.length; i++) {
if (this.info.devid[i] == devid) {
this.goDash(this.info.devid[(i + 1) % this.info.devid.length]);
return;
}
}
}
}
};