Skip to content

Commit

Permalink
Update search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuZad authored and mulbc committed Jan 26, 2023
1 parent 1e55885 commit 81bfaac
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ async function mainLoaded() {
if (!secretList) {
secretList = [];
}
querySecrets(searchRegex);
querySecrets(searchRegex, (searchInput.value.length != 0));
}

async function querySecrets(searchString) {
async function querySecrets(searchString, manualSearch) {
resultList.textContent = '';
var promises = [];
let anyMatch = false;
notify.clear();
for (const secret of secretList) {
promises.push(
Expand All @@ -62,10 +63,9 @@ async function querySecrets(searchString) {
});
return;
}
let anyMatch = false;
for (const element of (await secretsInPath.json()).data.keys) {
var pattern = new RegExp(element);
var patternMatches = pattern.test(searchString);
var patternMatches = (pattern.test(searchString) || element.includes(searchString));
if (patternMatches) {
const urlPath = `${vaultServerAdress}/v1/secret/data/vaultPass/${secret}${element}`;
const credentials = await getCredentials(urlPath);
Expand All @@ -74,27 +74,33 @@ async function querySecrets(searchString) {
notify.clear();
}
}
if (!anyMatch) {
notify.info('No matching key found for this page.', {
removeOption: false,
});
}
})()
);
}

try {
await Promise.all(promises);
if (!anyMatch && !manualSearch) {
notify.info('No matching key found for this page.', {
removeOption: false,
});
} else if (!anyMatch) {
notify.info('No matching key found for the search', {
removeOption: false,
});
}
} catch (err) {
notify.clear().error(err.message);
}
}

const searchHandler = function (e) {
querySecrets(e.target.value);
if (e.key === 'Enter') {
mainLoaded()
}
};

searchInput.addEventListener('input', searchHandler);
searchInput.addEventListener('keypress', searchHandler);

function addCredentialsToList(credentials, credentialName, list) {
const item = document.createElement('li');
Expand Down

0 comments on commit 81bfaac

Please sign in to comment.