Fixed updating credentials from the context menu

This commit is contained in:
varjolintu 2018-08-06 09:10:25 +03:00 committed by Jonathan White
parent bfa5548715
commit 0ad4475fa8

View file

@ -5,17 +5,22 @@ var _tab;
function _initialize(tab) {
_tab = tab;
// no credentials set or credentials already cleared
if (!_tab.credentials.username) {
// No credentials set or credentials already cleared
if (!_tab.credentials.username && !_tab.credentials.password) {
_close();
return;
}
// no existing credentials to update --> disable update-button
// No existing credentials to update --> disable Update button
if (_tab.credentials.list.length === 0) {
$('#btn-update').attr('disabled', true).removeClass('btn-warning');
}
// No username available. This might be because of trigger from context menu --> disable New button
if (!_tab.credentials.username && _tab.credentials.password) {
$('#btn-new').attr('disabled', true).removeClass('btn-success');
}
let url = _tab.credentials.url;
url = (url.length > 50) ? url.substring(0, 50) + '...' : url;
$('.information-url:first span:first').text(url);
@ -31,8 +36,13 @@ function _initialize(tab) {
$('#btn-update').click(function(e) {
e.preventDefault();
// only one entry which could be updated
// Only one entry which could be updated
if(_tab.credentials.list.length === 1) {
// Use the current username if it's empty
if (!_tab.credentials.username) {
_tab.credentials.username = _tab.credentials.list[0].login;
}
browser.runtime.sendMessage({
action: 'update_credentials',
args: [_tab.credentials.list[0].uuid, _tab.credentials.username, _tab.credentials.password, _tab.credentials.url]
@ -58,10 +68,35 @@ function _initialize(tab) {
.data('entryId', i)
.click(function(e) {
e.preventDefault();
const entryId = $(this).data('entryId');
// Use the current username if it's empty
if (!_tab.credentials.username) {
_tab.credentials.username = _tab.credentials.list[entryId].login;
}
// Check if the password has changed for the updated credentials
browser.runtime.sendMessage({
action: 'update_credentials',
args: [_tab.credentials.list[$(this).data('entryId')].uuid, _tab.credentials.username, _tab.credentials.password, _tab.credentials.url]
}).then(_verifyResult);
action: 'retrieve_credentials',
args: [ url, '', false, true ]
}).then((credentials) => {
if (!credentials || credentials.length !== _tab.credentials.list.length) {
_verifyResult('error');
return;
}
// Show a notification if the user tries to update credentials using the old password
if (credentials[entryId].password === _tab.credentials.password) {
showNotification('Error: Credentials not updated. The password has not been changed.');
_close();
return;
}
browser.runtime.sendMessage({
action: 'update_credentials',
args: [_tab.credentials.list[entryId].uuid, _tab.credentials.username, _tab.credentials.password, _tab.credentials.url]
}).then(_verifyResult);
});
});
if (_tab.credentials.usernameExists && _tab.credentials.username === _tab.credentials.list[i].login) {