-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
216 lines (202 loc) · 7.08 KB
/
popup.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
// converts a string value to its equivlant binary representation.
function stringToBinary(stringValue) {
return stringValue.replace(/.{1}/g, function (matchedString) {
var binString = matchedString.charCodeAt(0).toString(2);
return '00000000'.substring(0, 8 - binString.length) + binString;
});
}
// converts a binary value to its equivlant string representation.
function binaryToString(binValue) {
return binValue.replace(/[01]{8}/g, function (matchedString) {
return String.fromCharCode(parseInt(matchedString, 2));
});
}
//XOR with secret key
function XOR(binValue){
var x = "";
for(i = 0; i < binValue.length; i++){
if(binValue.charAt(i) == "0"){
x = x + "1";
}else{
x = x + "0";
}
}
return x;
}
//This function is called when the extension is clicked on (i.e. when the popup page loads)
document.addEventListener('DOMContentLoaded', function () {
document.getElementById("forgot").onclick = forgotP;
$("#wrong").hide();
if(localStorage.loggedin == "true"){
alreadyIn();
}else{
$('#username').watermark('USERNAME');
$('#password').watermark('PASSWORD');
$('#login').text("Login");
document.getElementById("login").onclick = signOn;
}
});
//Signs the user into our site, so that we can sign them into NFL.com
function signOn(){
var u = $('#username').val() + ",";
var p = $('#password').val();
//Get the login info to see if the u and p are correct
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
var theText = binaryToString(XOR(xmlhttp.responseText));
var where = theText.indexOf(u);
if(where != -1 && (p == theText.substr(where+10, 9))){
chrome.storage.sync.set({"username" : $('#username').val()});
chrome.storage.sync.set({"loggedin" : "true"});
localStorage.username = $('#username').val();
chrome.storage.sync.get("new", function (obj) {
if("new" in obj){
localStorage.loggedin = "true";
localStorage.username = $('#username').val();
alreadyIn();
$('#forgot').hide();
$("#wrong").hide();
}else{
localStorage.survey = "false";
localStorage.complete = "never";
$('#username').val("");
$('#username').watermark('New Password');
$('#password').hide();
$('#login').text("Save Password");
document.getElementById("login").onclick = changeP;
$('#forgot').hide();
$("p").text("Please change your password. Enter the new password below:");
$("#wrong").hide();
}
});
}else if(where != -1){
chrome.storage.sync.get("new", function (obj) {
chrome.storage.sync.get("username", function (other) {
if("new" in obj
&& obj["new"] == $('#password').val()
&& other["username"] == $('#username').val()){
chrome.storage.sync.set({"loggedin" : "true"});
localStorage.loggedin = "true";
localStorage.username = $('#username').val();
alreadyIn();
$('#forgot').hide();
$("#wrong").hide();
}else if(obj["new"] == $('#password').val() && other["username"] == $('#username').val()){
chrome.storage.sync.set({"loggedin" : "true"});
localStorage.survey = "false";
localStorage.complete = "never";
localStorage.username = $('#username').val();
localStorage.loggedin = "true";
$('#username').val("");
$('#username').watermark('New Password');
$('#password').hide();
$('#login').text("Save Password");
document.getElementById("login").onclick = changeP;
$('#forgot').hide();
$("p").text("Please change your password. Enter the new password below:");
$("#wrong").hide();
}else{
$("#wrong").show();
}
});
});
}else{
$("#wrong").show();
}
}
}
xmlhttp.open("GET","http://jack.cs.brown.edu/theTrueFile.txt?"+ Math.floor((Math.random() * 10000) + 1),true);
xmlhttp.send();
}
//What happens to the extension once they are signed on
function alreadyIn(){
$('#username').hide();
$('#password').hide();
$('#login').text("Logout");
document.getElementById("login").onclick = signOut;
$("p").text("You are signed in. Feel free to go to")
$(".link").text("fantasyfootball.yahoo.com");
$(".link").click(function(){
chrome.tabs.create({ url: "https://login.yahoo.com" });
});
$('#forgot').hide();
$("h1").text("Welcome BACK to BrownFF!");
//Checks if it is survey time
var d = new Date();
var m = d.getMonth();
var date = d.getDate();
var day = d.getDay();
var monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
var mDif = m - parseInt(localStorage.lastM);
var dDif = 0;
if((mDif == 1) || (mDif == -1) || (mDif == -11)){
dDif = monthDays[parseInt(localStorage.lastM)] - parseInt(localStorage.lastD) + date;
}
else if(mDif == 0){
dDif = date - parseInt(localStorage.lastD);
}
else{
dDif = 100;
}
if((day >= 2 && day - dDif < 2) || (day < 2 && day - dDif < -5)){
localStorage.survey = "false";
survey();
}
}
//Signs them out of the extension
function signOut(){
localStorage.loggedin="false";
chrome.storage.sync.set({"loggedin" : "false"});
$('#forgot').show();
$('#username').val("");
$('#password').val("");
$('#username').watermark('USERNAME');
$('#password').watermark('PASSWORD');
$('#username').show();
$('#password').show();
$(".link").hide();
$('#login').text("Login");
$("p").text("Please login in using your assigned username and password to proceed to your manager portal. \nIf you forgot your password, enter your username and click Forget Password.");
$("h1").text("Welcome to Brown Fantasy Football!");
document.getElementById("login").onclick = signOn;
chrome.tabs.create({ url: "http://login.yahoo.com/?logout=1" });
}
//Forgot password stuff
function forgotP(){
chrome.storage.sync.get("username", function (obj) {
if("username" in obj && $('#username').val() == obj["username"]){
chrome.storage.sync.get("new", function (other) {
$("p").text("Your password is " + other["new"]);
});
$('#username').val("");
}
});
}
//Change password stuff
function changeP(){
chrome.storage.sync.set({"new" : $('#username').val()});
chrome.storage.sync.set({"loggedin" : "true"});
localStorage.loggedin = "true";
alreadyIn();
}
//Displays the survey if it is time.
function survey(){
//Should be 2 for Tuesday
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
var eightManUNs = binaryToString(XOR(xmlhttp.responseText));
var where = eightManUNs.indexOf(localStorage.username);
if(where != -1){
localStorage.survey = "false";
chrome.browserAction.setPopup({popup: "survey8.html"});
window.location.href="survey8.html";
}else{
localStorage.survey = "false";
chrome.browserAction.setPopup({popup: "survey.html"});
window.location.href="survey.html";
}
}
xmlhttp.open("GET","http://jack.cs.brown.edu/eightMen.txt?"+ Math.floor((Math.random() * 10000) + 1),false);
xmlhttp.send();
}