-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmenu.js
executable file
·310 lines (276 loc) · 8.85 KB
/
menu.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
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
303
304
305
306
307
308
309
310
////get Elements
//get forms
var allforms = [];
var loginform = document.getElementById("login");
allforms.push(loginform);
var mainMenu = document.getElementById("main_menu");
allforms.push(mainMenu);
var manualEncrypt = document.getElementById("manualEncrypt");
allforms.push(manualEncrypt);
var confirm = document.getElementById("confirm");
allforms.push(confirm);
var selInputDiv = document.getElementById("selInputDiv");
allforms.push(selInputDiv);
var header = document.getElementById("header");
allforms.push(header);
var errorDiv = document.getElementById("error");
//get buttons
var loginbtn = document.getElementById("loginbtn");
var register = document.getElementById("register");
var logout = document.getElementById("logout");
var detect = document.getElementById("detect");
var selectbtn = document.getElementById("selbtn");
var manual = document.getElementById("manual");
var selEntry = document.getElementById("selEntry");
var home = document.getElementById("home");
var manEncrypt = document.getElementById("encrypt");
var manDecrypt = document.getElementById("decrypt");
//get inputs
var username = document.getElementById("usrname");
var password = document.getElementById("password");
var confirmpass = document.getElementById("confirmpass");
var manBody = document.getElementById("manBody");
var manRecipient = document.getElementById("manRecipient");
var selRecipient = document.getElementById("selRecipient");
////Variables
var isRegistering = false;
var isLoggedin = false;
loadPage("main_menu");
//////////Goes to the main page if logged in//////////
home.addEventListener("click", function() {
loadPage("main_menu");
sendError("");
});
chrome.storage.sync.get("username", function(data) {
if (!chrome.runtime.error && data.username) {
username.setAttribute("value", data.username)
}
});
chrome.storage.sync.get("manBody", function(data) {
if (!chrome.runtime.error && data.manBody) {
manBody.value = data.manBody;
}
});
chrome.storage.sync.get("manRecipient", function(data) {
if (!chrome.runtime.error && data.manRecipient) {
manRecipient.value = data.manRecipient;
}
});
chrome.storage.sync.get("selRecipient", function(data) {
if (!chrome.runtime.error && data.selRecipient) {
selRecipient.value = data.selRecipient;
}
});
chrome.storage.sync.get("loggedin", function(data) {
if (!chrome.runtime.error && data.loggedin == true) {
isLoggedin = data.loggedin;
loadPage("main_menu");
}
});
chrome.storage.sync.get("pageid", function(data) {
if (!chrome.runtime.error && data.pageid) {
loadPage(data.pageid);
}
});
username.addEventListener("change", function() {
var d = username.value;
chrome.storage.sync.set({"username" : d}, function() {
if (chrome.runtime.error) {
console.log("Runtime error.")
}
});
})
manBody.addEventListener("change", function() {
var d = manBody.value;
chrome.storage.sync.set({"manBody" : d}, function() {
if (chrome.runtime.error) {
console.log("Runtime error.")
}
});
})
manRecipient.addEventListener("change", function() {
var d = manRecipient.value;
chrome.storage.sync.set({"manRecipient" : d}, function() {
if (chrome.runtime.error) {
console.log("Runtime error.")
}
});
})
selRecipient.addEventListener("change", function() {
var d = selRecipient.value;
chrome.storage.sync.set({"selRecipient" : d}, function() {
if (chrome.runtime.error) {
console.log("Runtime error.")
}
});
})
function login(){
console.log("logging in");
var isLoggedin = true;
chrome.storage.sync.set({"loggedin" : isLoggedin}, function() {
if (chrome.runtime.error) {
console.log("Runtime error.");
}
});
}
//////////Logs in, basic check for valid email send password and email//////////
loginbtn.addEventListener("click", function() {
var error = "";
//regex to check for basic valid email
if (/\S+@\S+\.\S+/.test(username.value)) {
if (isRegistering) {
if (password.value == confirmpass.value) {
//regiser key to server and store login
isLoggedin = registerUsr(username.value, password.value);
if (isLoggedin) login();
}
else {
error = "Passwords do not match"; //put into error div
}
confirmpass.value = "";
}
else {
//send to login check
isLoggedin = loginUsr(username.value, password.value);
if (isLoggedin) login();
}
}
else {
error = "Invalid Email"; //put into error div
}
if (isLoggedin) {
loginform.style.display = "none";
mainMenu.style.display = "block";
header.style.display = "block";
password.value = "";
}
sendError(error);
});
//////////Changes login to register//////////
register.addEventListener("click", function() {
isRegistering = !isRegistering;
if (isRegistering) {
confirm.style.display = "block";
register.setAttribute("value", "Cancel");
loginbtn.setAttribute("value", "Register");
}
else {
confirm.style.display = "none";
register.setAttribute("value", "Register");
loginbtn.setAttribute("value", "Login");
}
sendError("");
});
//////////Logs user out//////////
logout.addEventListener("click", function() {
isLoggedin = false;
//add logout function
for(var x = 0; x < allforms.length; x++) allforms[x].style.display = "none";
loginform.style.display = "block";
sendError("");
});
//////////Opens the manual encryption input//////////
manual.addEventListener("click", function() {
mainMenu.style.display = "none";
manualEncrypt.style.display = "block";
sendError("");
});
manEncrypt.addEventListener("click", function(){
if (/\S+@\S+\.\S+/.test(manRecipient.value)) {
encrypt(manRecipient.value, manBody.value);
sendError("");
} else sendError("Invalid Email")
});
manDecrypt.addEventListener("click", function(){
decrypt(username.value, manBody.value);
sendError("");
});
//////////Opens the email input for selected input//////////
selectbtn.addEventListener("click", function() {
selectbtn.style.display = "none";
selInputDiv.style.display = "block";
sendError("");
});
//////////-----Add code to detect-----//////////
detect.addEventListener("click", function() {
//detect the email body and recipient and encrypt or call function----replace values
var recipient = "[email protected]";
var message = "Replace me with email body";
var encrypted = encrypt(recipient, message);
sendError("");
});
//////////Sends email and selected text to encrypt//////////
selEntry.addEventListener("click", function(){
var message = "Replace me with selected text";
var recipient = selRecipient.value;
if (/\S+@\S+\.\S+/.test(recipient)) {
var encrypted = encrypt(recipient, message);
selectbtn.style.display = "block";
selInputDiv.style.display = "none";
selRecipient.value = "";
sendError("");
} else {
console.log("invalid");
sendError("Invalid Email");
}
});
function registerUsr(username) {
var success = true;
return success;
}
function loginUsr(username, password) {
var success = true;
return success;
}
function encrypt(recipient, message) {//move to background
//do the encryption
return "Encrypted text";
}
function decrypt(sender, message) {//move to background
//do the encryption
return "Decrypted text";
}
function sendError(error) {
if (error) errorDiv.innerHTML = "<p>" + error + "</p>";
else errorDiv.innerHTML = "";
savePage();
}
//////////Save page/////////
function savePage() {
for (var i in allforms) {
if (allforms[i].style.display == "block") {
console.log(allforms[i].id);
if (allforms[i].id != "header") {
chrome.storage.sync.set({"pageid" : allforms[i].id}, function() {
if (chrome.runtime.error) {
console.log("Runtime error.")
}
});
}
}
}
}
//////////Loads page baised on page ID//////////
//if empty string just goes to home (login before calling)
function loadPage(page) {
for (var i in allforms) {
if (page == allforms[i].id && isLoggedin) {
allforms[i].style.display = "block";
}
else {
if (page == "selInputDiv") {
mainMenu.style.display = "block";
} else
allforms[i].style.display = "none";
}
}
//check if logged in
if (isLoggedin) {
if (page == "") {
mainMenu.style.display = "block";
}
header.style.display = "block";
selectbtn.style.display = "block";
}
else loginform.style.display = "block";
}