Requery form if password input has changed (#2738)

Requery form if password input has changed
This commit is contained in:
Sami Vänttinen 2025-11-01 15:27:44 +02:00 committed by GitHub
parent caf7c04a89
commit 9a32e8c0c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -245,6 +245,17 @@ kpxcFill.fillInCredentials = async function(combination, predefinedUsername, uui
skipAutoSubmit = selectedCredentials.skipAutoSubmit === 'true';
}
// Update the password field if form has been updated with a new identical element (Moodle).
// If found, replace the new input field to the combination.
if (combination?.form && combination?.password && !combination.form.contains(combination.password)) {
const formInputs = kpxcObserverHelper.getInputs(combination.form);
const newPasswordField = formInputs?.find((formInput) =>
formInput?.getLowerCaseAttribute('type') === 'password');
if (newPasswordField && areNamedNodeMapsEqual(combination.password.attributes, newPasswordField.attributes)) {
combination.password = newPasswordField;
}
}
// Fill password
if (combination.password && matchesWithNodeName(combination.password, 'INPUT')) {
// Show a notification if password length exceeds the length defined in input
@ -427,3 +438,26 @@ const showErrorNotification = async function(errorMessage, notificationType = 'e
kpxcUI.createNotification(notificationType, errorMessage);
}
};
// Checks if two NamedNodeMaps (attribute lists) are equal
const areNamedNodeMapsEqual = function(currentNodeMap, newNodeMap) {
if (!currentNodeMap || !newNodeMap) {
return false;
}
const fieldAttributes = Array.from(currentNodeMap);
const newFieldAttributes = Array.from(newNodeMap);
if (fieldAttributes.length !== newFieldAttributes.length) {
return false;
}
for (const attr of fieldAttributes) {
const newAttr = newFieldAttributes.find((newAttr) => newAttr?.name === attr?.name);
if (!newAttr || newAttr?.value !== attr?.value) {
return false;
}
}
return true;
};