Fill best matching password entry with Ctrl+Shift+F (#131)

This commit is contained in:
Maxim Baz
2019-04-21 10:10:55 +02:00
committed by GitHub
parent 66f43b557f
commit 0b9e1435ad
7 changed files with 301 additions and 236 deletions

View File

@@ -57,6 +57,30 @@ chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
return true;
});
// handle keyboard shortcuts
chrome.commands.onCommand.addListener(async command => {
switch (command) {
case "fillBest":
try {
const settings = await getFullSettings();
if (settings.tab.url.match(/^(chrome|about):/)) {
// only fill on real domains
return;
}
handleMessage(settings, { action: "listFiles" }, listResults => {
const logins = helpers.prepareLogins(listResults.files, settings);
const bestLogin = helpers.filterSortLogins(logins, "", true)[0];
if (bestLogin) {
handleMessage(settings, { action: "fill", login: bestLogin }, () => {});
}
});
} catch (e) {
console.log(e);
}
break;
}
});
chrome.runtime.onInstalled.addListener(onExtensionInstalled);
//----------------------------------- Function definitions ----------------------------------//
@@ -78,27 +102,18 @@ async function updateMatchingPasswordsCount(tabId) {
}
// Get tab info
let currentDomain = undefined;
try {
const tab = await chrome.tabs.get(tabId);
currentDomain = new URL(tab.url).hostname;
settings.host = new URL(tab.url).hostname;
} catch (e) {
throw new Error(`Unable to determine domain of the tab with id ${tabId}`);
}
let matchedPasswordsCount = 0;
for (var storeId in response.data.files) {
for (var key in response.data.files[storeId]) {
const login = response.data.files[storeId][key].replace(/\.gpg$/i, "");
const domain = helpers.pathToDomain(storeId + "/" + login, currentDomain);
const inCurrentDomain =
currentDomain === domain || currentDomain.endsWith("." + domain);
const recent = settings.recent[sha1(currentDomain + sha1(storeId + sha1(login)))];
if (recent || inCurrentDomain) {
matchedPasswordsCount++;
}
}
}
const logins = helpers.prepareLogins(response.data.files, settings);
const matchedPasswordsCount = logins.reduce(
(acc, login) => acc + (login.recent.count || login.inCurrentDomain ? 1 : 0),
0
);
if (matchedPasswordsCount) {
// Set badge for the current tab