-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
90 lines (84 loc) · 2.83 KB
/
popup.js
File metadata and controls
90 lines (84 loc) · 2.83 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
document.addEventListener('DOMContentLoaded', function () {
const authSection = document.getElementById('auth-section');
const passwordManagerSection = document.getElementById('password-manager-section');
const usernameInput = document.getElementById('username');
const passwordInput = document.getElementById('password');
const registerBtn = document.getElementById('register');
const loginBtn = document.getElementById('login');
const passwordList = document.getElementById('password-list');
const addPasswordBtn = document.getElementById('add-password');
registerBtn.addEventListener('click', () => {
const username = usernameInput.value;
const password = passwordInput.value;
fetch('http://localhost:3000/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username, password })
}).then(response => {
if (response.ok) {
alert('Registered successfully');
} else {
alert('Registration failed');
}
});
});
loginBtn.addEventListener('click', () => {
const username = usernameInput.value;
const password = passwordInput.value;
fetch('http://localhost:3000/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username, password })
}).then(response => response.json())
.then(data => {
if (data.token) {
chrome.storage.sync.set({ token: data.token }, () => {
showPasswordManager();
});
} else {
alert('Login failed');
}
});
});
addPasswordBtn.addEventListener('click', () => {
const site = prompt('Enter site name:');
const password = prompt('Enter password:');
if (site && password) {
chrome.storage.sync.get(['passwords'], function(result) {
const passwords = result.passwords || [];
passwords.push({ site, password });
chrome.storage.sync.set({ passwords }, function() {
displayPasswords(passwords);
});
});
}
});
chrome.storage.sync.get(['token'], function(result) {
if (result.token) {
showPasswordManager();
} else {
authSection.style.display = 'block';
}
});
function showPasswordManager() {
authSection.style.display = 'none';
passwordManagerSection.style.display = 'block';
chrome.storage.sync.get(['passwords'], function(result) {
const passwords = result.passwords || [];
displayPasswords(passwords);
});
}
function displayPasswords(passwords) {
passwordList.innerHTML = '';
passwords.forEach((item, index) => {
const div = document.createElement('div');
div.className = 'password-item';
div.textContent = `${item.site}: ${item.password}`;
passwordList.appendChild(div);
});
}
});