From 902476ee1344f45145611f1e782628d15c2bd003 Mon Sep 17 00:00:00 2001 From: CyberSphinxxx Date: Thu, 14 Nov 2024 01:29:09 +0800 Subject: [PATCH] Move Internal JS here --- .../password_manager_view_scripts.js | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 password_manager/password_manager_view/password_manager_view_scripts.js diff --git a/password_manager/password_manager_view/password_manager_view_scripts.js b/password_manager/password_manager_view/password_manager_view_scripts.js new file mode 100644 index 0000000..1174331 --- /dev/null +++ b/password_manager/password_manager_view/password_manager_view_scripts.js @@ -0,0 +1,145 @@ +function loadPasswords() { + const passwordList = document.getElementById('passwordList'); + const savedPasswords = JSON.parse(localStorage.getItem('savedPasswords') || '[]'); + passwordList.innerHTML = ''; + + savedPasswords.forEach((encryptedData, index) => { + try { + const decryptedData = atob(encryptedData); + const passwordObject = JSON.parse(decryptedData); + + const passwordListItem = document.createElement('li'); + passwordListItem.className = 'password-list-item'; + + const contentDiv = document.createElement('div'); + contentDiv.className = 'password-content'; + contentDiv.innerHTML = ` +
+
Label: ${passwordObject.label}
+
Username: ${passwordObject.username}
+ +
+ `; + + const buttonGroup = document.createElement('div'); + buttonGroup.className = 'button-group'; + + const showPasswordButton = document.createElement('button'); + showPasswordButton.textContent = 'Show Password'; + showPasswordButton.className = 'action-button'; + showPasswordButton.addEventListener('click', () => togglePassword(index)); + + const editButton = document.createElement('button'); + editButton.textContent = 'Edit'; + editButton.className = 'action-button'; + editButton.addEventListener('click', () => toggleEdit(index, passwordObject)); + + const deleteButton = document.createElement('button'); + deleteButton.textContent = 'Delete'; + deleteButton.className = 'action-button delete-button'; + deleteButton.addEventListener('click', () => deletePassword(index)); + + buttonGroup.appendChild(showPasswordButton); + buttonGroup.appendChild(editButton); + buttonGroup.appendChild(deleteButton); + + passwordListItem.appendChild(contentDiv); + passwordListItem.appendChild(buttonGroup); + passwordList.appendChild(passwordListItem); + } catch (error) { + console.error('Error decoding password:', error); + } + }); +} + +function togglePassword(index) { + const passwordField = document.querySelector(`#password-info-${index} .password-field`); + const button = passwordField.parentElement.parentElement.querySelector('button'); + + if (passwordField.style.display === 'none') { + passwordField.style.display = 'block'; + button.textContent = 'Hide Password'; + } else { + passwordField.style.display = 'none'; + button.textContent = 'Show Password'; + } +} + +function toggleEdit(index, passwordObject) { + const infoDiv = document.getElementById(`password-info-${index}`); + const isEditing = infoDiv.querySelector('input'); + + if (!isEditing) { + // Switch to edit mode + infoDiv.innerHTML = ` +
+ Label: + +
+
+ Username: + +
+
+ Password: + +
+
+ + +
+ `; + } +} + +function checkPasswordStrength(password) { + // Example criteria for a weak password + return password.length < 8; // You can expand this logic as needed +} + +function showWeakPasswordNotification() { + alert('The password you entered is weak. It is recommended to use a stronger password.'); +} + +function saveChanges(index) { + const infoDiv = document.getElementById(`password-info-${index}`); + const inputs = infoDiv.querySelectorAll('input'); + const savedPasswords = JSON.parse(localStorage.getItem('savedPasswords') || '[]'); + + const updatedPasswordObject = { + label: inputs[0].value, + username: inputs[1].value, + password: inputs[2].value + }; + + if (checkPasswordStrength(updatedPasswordObject.password)) { + showWeakPasswordNotification(); + } + + const encryptedData = btoa(JSON.stringify(updatedPasswordObject)); + savedPasswords[index] = encryptedData; + localStorage.setItem('savedPasswords', JSON.stringify(savedPasswords)); + loadPasswords(); +} + +function deletePassword(index) { + if (confirm('Are you sure you want to delete this password?')) { + const savedPasswords = JSON.parse(localStorage.getItem('savedPasswords') || '[]'); + savedPasswords.splice(index, 1); + localStorage.setItem('savedPasswords', JSON.stringify(savedPasswords)); + loadPasswords(); + } +} + +loadPasswords(); + +function getSavedPasswords() { + let savedPasswords; + try { + savedPasswords = JSON.parse(localStorage.getItem('savedPasswords') || '[]'); + } catch (error) { + console.error('Error parsing saved passwords:', error); + savedPasswords = []; + } + return savedPasswords; +} \ No newline at end of file