-
Notifications
You must be signed in to change notification settings - Fork 86
/
options.js
262 lines (221 loc) · 8.35 KB
/
options.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
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
/* jshint browser:true, devel:true */
/* globals chrome, codes */
'use strict';
var resetSimSettings = function() {
setSimSettings({
country: "USA",
operator: "T-Mobile",
operatorCode: "31020"
});
};
var checkSimSettings = function() {
if (!localStorage.getItem("simCountry") || !localStorage.getItem("simOperator") || !localStorage.getItem("simOperatorCode")) {
resetSimSettings();
}
};
var setSimSettings = function(sim) {
localStorage.setItem('simCountry', sim.country);
localStorage.setItem('simOperator', sim.operator);
localStorage.setItem('simOperatorCode', sim.operatorCode);
};
var initCountryOptions = function() {
var selectedCountry = localStorage.getItem('simCountry');
Object.keys(codes).sort().forEach(function(country) {
var option = new Option(/*text*/ country, /*value*/ country,
/*default_selected*/ false, /*selected*/ country == selectedCountry);
sltCountry.add(option);
});
sltCountry.onchange = function(e) {
initOperatorOptions(this.value);
};
sltCountry.onchange();
};
var initOperatorOptions = function(country) {
// Empty <select> box
sltOperator.options.length = 0;
var operators = codes[country];
if (operators) {
var selectedOperator = localStorage.getItem('simOperator');
Object.keys(operators).sort().forEach(function(operator) {
var option = new Option(/*text*/ operator, /*value*/ operator,
/*default_selected*/ false, /*selected*/ operator == selectedOperator);
sltOperator.add(option);
});
}
};
var saveAuth = function(email, token, deviceId) {
localStorage.setItem('authEmail', email);
localStorage.setItem('authToken', token);
localStorage.setItem('deviceId', deviceId.toLowerCase());
};
var clearAuth = function() {
localStorage.removeItem("authEmail");
localStorage.removeItem("authToken");
localStorage.removeItem("deviceId");
};
/**
* ClientLogin errors, taken from
* https://developers.google.com/accounts/docs/AuthForInstalledApps
*/
var clientLoginErrors = {
"BadAuthentication": "Incorrect username or password.",
"NotVerified": "The account email address has not been verified. You need to access your Google account directly to resolve the issue before logging in here.",
"TermsNotAgreed": "You have not yet agreed to Google's terms, acccess your Google account directly to resolve the issue before logging in using here.",
"CaptchaRequired": "A CAPTCHA is required. (not supported, try logging in another tab)",
"Unknown": "Unknown or unspecified error; the request contained invalid input or was malformed.",
"AccountDeleted": "The user account has been deleted.",
"AccountDisabled": "The user account has been disabled.",
"ServiceDisabled": "Your access to the specified service has been disabled. (The user account may still be valid.)",
"ServiceUnavailable": "The service is not available; try again later."
};
var login = function(email, password, deviceId) {
var ACCOUNT_TYPE_HOSTED_OR_GOOGLE = "HOSTED_OR_GOOGLE";
var URL_LOGIN = "https://www.google.com/accounts/ClientLogin";
var LOGIN_SERVICE = "androidsecure";
var params = {
"Email": email,
"Passwd": password,
"service": LOGIN_SERVICE,
"accountType": ACCOUNT_TYPE_HOSTED_OR_GOOGLE
};
var xhr = new XMLHttpRequest();
xhr.open("POST", URL_LOGIN, true);
var paramsStr = "";
for (var key in params) {
paramsStr += "&" + key + "=" + encodeURIComponent(params[key]);
}
xhr.onload = function() {
var AUTH_TOKEN = "";
var response = this.responseText;
var error = response.match(/Error=(\w+)/);
if (error) {
var msg = clientLoginErrors[error[1]] || error[1];
alert("ERROR: authentication failed.\n" + msg);
return;
}
var match = response.match(/Auth=([a-z0-9=_\-]+)/i);
if (match) {
AUTH_TOKEN = match[1];
}
if (!AUTH_TOKEN) {
// should never happen...
alert("ERROR: Authentication token not available, cannot login.");
return;
}
saveAuth(email, AUTH_TOKEN, deviceId);
refreshViews();
};
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(paramsStr);
};
var refreshViews = function() {
txtAuthEmail.textContent = inpEmail.value = localStorage.getItem("authEmail");
txtDeviceId.textContent = inpDeviceId.value = localStorage.getItem("deviceId");
if (typeof localStorage.authToken == "undefined") {
formLogin.style.display = "block";
formInfo.style.display = "none";
} else {
formInfo.style.display = "block";
formLogin.style.display = "none";
checkSimSettings();
initCountryOptions();
}
};
var formInfo = document.getElementById("info_form");
var formLogin = document.getElementById("login_form");
var txtAuthEmail = document.getElementById("auth_email");
var txtDeviceId = document.getElementById("device_id");
var inpEmail = document.getElementById("user_email");
var inpPassword = document.getElementById("user_password");
var inpDeviceId = document.getElementById("user_device_id");
var sltCountry = document.getElementById("slt_country");
var sltOperator = document.getElementById("slt_operator");
var btnDefault = document.getElementById("btn_default");
var btnsAdv = document.getElementsByClassName("btn-advanced-settings");
function toggleAdvCb(e) {
e.preventDefault();
formInfo.classList.toggle("hide-advanced");
formInfo.classList.toggle("show-advanced");
}
for (var i=0; i<btnsAdv.length; i++) {
btnsAdv[i].addEventListener("click", toggleAdvCb);
}
// whether to override sanity checks.
var chkOverride = document.getElementById('skip-dl-checks');
chkOverride.addEventListener('click', function (ev) {
var msg = 'Enabling this option makes paid apps visibile. If you have not' +
' bought the app, then you may be charged for the download.' +
' Do you want to override this safety check?';
if (this.checked && !confirm(msg)) {
ev.preventDefault();
return;
}
localStorage.setItem('assumeAvailable', this.checked);
});
chkOverride.checked = localStorage.getItem('assumeAvailable');
btnDefault.onclick = function(e) {
e.preventDefault();
if (confirm('Reset to default sim operator?')) {
resetSimSettings();
initCountryOptions();
}
};
var btnSave = document.getElementById("btn_save");
btnSave.onclick = function(e) {
e.preventDefault();
var country = sltCountry.value;
var operator = sltOperator.value;
var operatorCode = codes[country][operator];
setSimSettings({
country: country,
operator: operator,
operatorCode: operatorCode
});
alert('Save successfully!');
};
var btnLogin = document.getElementById("btn_login");
btnLogin.onclick = function(e) {
e.preventDefault();
var email = inpEmail.value;
var password = inpPassword.value;
var deviceId = inpDeviceId.value;
// append @gmail.com if no host part is available.
if (email.length > 0 && !/@/.test(email)) {
email += "@gmail.com";
inpEmail.value = email;
}
var match = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.exec(email);
if (!match) {
alert('ERROR: Please enter valid email!');
inpEmail.focus();
return;
}
if (password.length === 0) {
alert('ERROR: Please enter a password!');
inpPassword.focus();
return;
}
if (!/^[0-9a-f]{16}$/i.test(deviceId)) {
alert('ERROR: Android Device ID must be 16 characters long and only contains characters from 0-9, A-F');
inpDeviceId.focus();
return;
}
login(email, password, deviceId);
};
var btnLogout = document.getElementById("btn_logout");
btnLogout.onclick = function(e) {
e.preventDefault();
if (confirm('Change to another email?')) {
clearAuth();
refreshViews();
}
};
refreshViews();
/* test if still logged in */
if (localStorage.getItem('authToken') !== null) {
chrome.extension.getBackgroundPage().hasValidSession(function(isValid) {
if (!isValid) {
refreshViews();
}
});
}