From d528513205dd8d48b9796f15fd41694729ddf8af Mon Sep 17 00:00:00 2001 From: Ivan Dimitrov Date: Wed, 15 Feb 2023 15:29:23 +0000 Subject: [PATCH] adding a keyboard shortcut to use last used login --- manifests/chrome-manifest-dev.json | 6 ++++++ web-extension/background.js | 23 +++++++++++++++++++++++ web-extension/generic.js | 1 + 3 files changed, 30 insertions(+) diff --git a/manifests/chrome-manifest-dev.json b/manifests/chrome-manifest-dev.json index 2650284..1b2bd5b 100644 --- a/manifests/chrome-manifest-dev.json +++ b/manifests/chrome-manifest-dev.json @@ -60,6 +60,12 @@ "suggested_key": { "default": "Ctrl+Shift+U" } + }, + "doLogin": { + "suggested_key": { + "default": "Ctrl+Shift+L" + }, + "description": "Do Login" } }, diff --git a/web-extension/background.js b/web-extension/background.js index c2784af..5851990 100644 --- a/web-extension/background.js +++ b/web-extension/background.js @@ -11,6 +11,7 @@ function _processLoginTabMessage(entry, tab) { if (response.username === urlDomain(tab.url)) { throw new Error(i18n.getMessage('couldNotDetermineUsernameMessage')); } + setLocalStorageKey(`${LAST_LOGIN_USED_PREFIX}${tab.url}`, entry); return browser.tabs .sendMessage(tab.id, { @@ -212,8 +213,30 @@ function _resolveCurrentAuthRequest(result, senderUrl) { } } +function doInCurrentTab(tabCallback) { + chrome.tabs.query({ currentWindow: true, active: true }, function (tabArray) { + tabCallback(tabArray[0]); + }); +} + +function doLogin() { + doInCurrentTab(async (tab) => { + _processLoginTabMessage(await getLocalStorageKey(`${LAST_LOGIN_USED_PREFIX}${tab.url}`), tab); + }); +} + +function initCommands() { + const commandsMap = { + doLogin: doLogin, + }; + chrome.commands.onCommand.addListener((command) => { + commandsMap[command](); + }); +} + function initBackground() { browser.runtime.onMessage.addListener(processMessageAndCatch); + initCommands(); browser.webRequest.onAuthRequired.addListener( _onAuthRequired, diff --git a/web-extension/generic.js b/web-extension/generic.js index 1c4103c..5275d82 100644 --- a/web-extension/generic.js +++ b/web-extension/generic.js @@ -13,6 +13,7 @@ const DEFAULT_SETTINGS = { const LAST_DOMAIN_SEARCH_PREFIX = 'LAST_DOMAIN_SEARCH_'; const LAST_DETAIL_VIEW_PREFIX = 'LAST_DETAIL_VIEW_'; +const LAST_LOGIN_USED_PREFIX = 'LAST_LOGIN_USED_'; const REQUIRED_GOPASS_VERSION = [1, 8, 5]; let versionOK = undefined;