-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
141 lines (117 loc) · 3.74 KB
/
main.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
// Initialize Firebase (ADD YOUR OWN DATA)
var config = {
apiKey: "use your own",
authDomain: "use your own",
databaseURL: "use your own",
projectId: "use your own",
storageBucket: "use your own",
messagingSenderId: "use your own"
};
firebase.initializeApp(config);
// Reference messages collection
var messagesRef = firebase.database().ref('messages');
//encryption
var text , keyy = 12;
function encrypt(text, keyy) {
keyy1 = (26 - keyy) % 26;
var textElem = document.getElementById("text");
var result = "";
for (var i = 0; i < text.length; i++) {
var c = text.charCodeAt(i);
if (65 <= c && c <= 90) result += String.fromCharCode((c - 65 + keyy1) % 26 + 65); // Uppercase
else if (97 <= c && c <= 122) result += String.fromCharCode((c - 97 + keyy1) % 26 + 97); // Lowercase
else result += text.charAt(i); // Copy
}
return result;
}
//decryption
function decrypt(text, keyy) {
keyy2=keyy;
var result = "";
for (var i = 0; i < text.length; i++) {
var c = text.charCodeAt(i);
if (65 <= c && c <= 90) result += String.fromCharCode((c - 65 + keyy2) % 26 + 65); // Uppercase
else if (97 <= c && c <= 122) result += String.fromCharCode((c - 97 + keyy2) % 26 + 97); // Lowercase
else result += text.charAt(i); // Copy
}
return result;
}
messagesRef.on('value', gotData, errData);
var str = `
<table border="1" style="max-width:auto;">
<tr>
<th>Name</th>
<th>Class </th>
<th>Email </th>
<th>Phone </th>
<th>Message</th>
</tr>`;
function gotData(data){
var messages = data.val();
var keys = Object.keys(messages);
// console.log(keys);
let list = document.getElementById('list');
list.innerHTML = "";
for (var i=0; i< keys.length; i++){
var k= keys[i];
var Namee = decrypt(messages[k].name,keyy);
var Companyy = decrypt(messages[k].company,keyy);
var Emaill = decrypt(messages[k].email,keyy);
var Phonee = messages[k].phone;
var Messagee = decrypt(messages[k].message,keyy);
//console.log(Namee, Companyy, Emaill, Phonee, Messagee);
str +=
`
<tr style="max-width:auto;">
<td id='name'>${Namee}</td>
<td id='company'>${Companyy}</td>
<td id='email'>${Emaill}</td>
<td id='phone'>${Phonee}</td>
<td id='msg'>${Messagee}</td>
</tr>
`;
}
str +='</table>';
document.getElementById('list').innerHTML = str;
}
function errData(data){
console.log('Error!');
console.log(err);
}
// Listen for form submit
document.getElementById('contactForm').addEventListener('submit', submitForm);
// Submit form
function submitForm(e){
e.preventDefault();
// Get values
var name = encrypt(getInputVal('name'),keyy) ;
var company = encrypt(getInputVal('company'),keyy);
var email = encrypt(getInputVal('email'),keyy);
var phone = getInputVal('phone');
var message = encrypt(getInputVal('message'),keyy);
// Save message
saveMessage(name, company, email, phone, message);
// Show alert
document.querySelector('.alert').style.display = 'block';
// Hide alert after 3 seconds
setTimeout(function(){
document.querySelector('.alert').style.display = 'none';
},3000);
// Clear form
document.getElementById('contactForm').reset();
}
// Function to get get form values
function getInputVal(id){
return document.getElementById(id).value;
}
// Save message to firebase
function saveMessage(name, company, email, phone, message){
var newMessageRef = messagesRef.push();
newMessageRef.set({
name: name,
company:company,
email:email,
phone:phone,
message:message
});
}