diff --git a/keepassxc-browser/background/event.js b/keepassxc-browser/background/event.js index 862f0d3..d13ccf8 100755 --- a/keepassxc-browser/background/event.js +++ b/keepassxc-browser/background/event.js @@ -166,6 +166,26 @@ kpxcEvent.onHTTPAuthPopup = async function(tab, data) { page.tabs[tab.id].loginList = data; browserAction.show(tab, popupData); + + const dialogData = { + width: 480, + height: 250, + left: (window.screen.width / 2) - (480 / 2), + top: (window.screen.height / 2) - (250 / 2) + }; + + const httpAuthDialog = page.tabs[tab.id].httpAuthDialog; + if (httpAuthDialog && await browser.windows.get(httpAuthDialog)) { + dialogData.focused = true; + browser.windows.update(httpAuthDialog, dialogData); + return; + } + + dialogData.type = 'popup'; + dialogData.url = `/popups/popup_httpauth.html?tab=${tab.id}`; + + const wnd = await browser.windows.create(dialogData); + page.httpAuthDialog = wnd.id; }; kpxcEvent.onUsernameFieldDetected = async function(tab, detected) { diff --git a/keepassxc-browser/background/httpauth.js b/keepassxc-browser/background/httpauth.js index a621490..1048b98 100755 --- a/keepassxc-browser/background/httpauth.js +++ b/keepassxc-browser/background/httpauth.js @@ -14,19 +14,17 @@ httpAuth.init = function() { reqType = 'asyncBlocking'; } - if (browser.webRequest.onAuthRequired.hasListener(handleReq)) { - browser.webRequest.onAuthRequired.removeListener(handleReq); - browser.webRequest.onCompleted.removeListener(httpAuth.requestCompleted); - browser.webRequest.onErrorOccurred.removeListener(httpAuth.requestCompleted); - } - // Only intercept http auth requests if the option is turned on. - if (page.settings.autoFillAndSend) { + if (page.settings.autoFillAndSend && !browser.webRequest.onAuthRequired.hasListener(handleReq)) { const opts = { urls: [ '' ] }; browser.webRequest.onAuthRequired.addListener(handleReq, opts, [ reqType ]); browser.webRequest.onCompleted.addListener(httpAuth.requestCompleted, opts); browser.webRequest.onErrorOccurred.addListener(httpAuth.requestCompleted, opts); + } else if (browser.webRequest.onAuthRequired.hasListener(handleReq)) { + browser.webRequest.onAuthRequired.removeListener(handleReq); + browser.webRequest.onCompleted.removeListener(httpAuth.requestCompleted); + browser.webRequest.onErrorOccurred.removeListener(httpAuth.requestCompleted); } }; @@ -90,7 +88,7 @@ httpAuth.loginOrShowCredentials = function(logins, details, resolve, reject) { if (page.settings.showNotifications) { showNotification(tr('multipleCredentialsDetected')); } - kpxcEvent.onHTTPAuthPopup({ 'id': details.tabId }, { 'logins': logins, 'url': details.searchUrl, 'resolve': resolve }); + kpxcEvent.onHTTPAuthPopup({ 'id': details.tabId }, { 'logins': logins, 'details': details, 'resolve': resolve, 'reject': reject }); } } else { reject({ cancel: false }); // No logins found diff --git a/keepassxc-browser/background/init.js b/keepassxc-browser/background/init.js index 7400ac8..de9a939 100644 --- a/keepassxc-browser/background/init.js +++ b/keepassxc-browser/background/init.js @@ -21,13 +21,11 @@ */ browser.tabs.onCreated.addListener((tab) => { if (tab.id > 0) { - if (tab.selected) { - page.currentTabId = tab.id; - if (!page.tabs[tab.id]) { - page.createTabEntry(tab.id); - } - page.switchTab(tab); + page.currentTabId = tab.id; + if (!page.tabs[tab.id]) { + page.createTabEntry(tab.id); } + page.switchTab(tab); } }); @@ -37,10 +35,18 @@ browser.tabs.onCreated.addListener((tab) => { * @param {object} removeInfo */ browser.tabs.onRemoved.addListener((tabId, removeInfo) => { + if (page.tabs[tabId].httpAuthDialog) { + browser.windows.remove(page.tabs[tabId].httpAuthDialog); + } + delete page.tabs[tabId]; if (page.currentTabId === tabId) { page.currentTabId = -1; } + + if (page.httpAuthDialog) { + browser.windows.remove(page.httpAuthDialog); + } }); /** @@ -53,11 +59,17 @@ browser.tabs.onActivated.addListener(async function(activeInfo) { const info = await browser.tabs.get(activeInfo.tabId); if (info && info.id) { page.currentTabId = info.id; + if (info.status === 'complete') { if (!page.tabs[info.id]) { page.createTabEntry(info.id); } + page.switchTab(info); + + if (page.httpAuthDialog) { + browser.windows.remove(page.httpAuthDialog); + } } } } catch (err) { diff --git a/keepassxc-browser/background/page.js b/keepassxc-browser/background/page.js index 0099b1e..1168eba 100755 --- a/keepassxc-browser/background/page.js +++ b/keepassxc-browser/background/page.js @@ -26,6 +26,7 @@ var page = {}; page.blockedTabs = []; page.currentRequest = {}; page.currentTabId = -1; +page.httpAuthDialog = undefined; page.loginId = -1; page.manualFill = ManualFill.NONE; page.passwordFilled = false; diff --git a/keepassxc-browser/popups/popup_httpauth.html b/keepassxc-browser/popups/popup_httpauth.html index aa0f7c6..49b9d69 100644 --- a/keepassxc-browser/popups/popup_httpauth.html +++ b/keepassxc-browser/popups/popup_httpauth.html @@ -17,20 +17,8 @@
-
- - - - -
- -
- . -
-
-
-

+

diff --git a/keepassxc-browser/popups/popup_httpauth.js b/keepassxc-browser/popups/popup_httpauth.js index b0383c6..ca72058 100644 --- a/keepassxc-browser/popups/popup_httpauth.js +++ b/keepassxc-browser/popups/popup_httpauth.js @@ -1,22 +1,29 @@ 'use strict'; +let tabId = -1; + const getLoginData = async () => { const global = await browser.runtime.getBackgroundPage(); const tabs = await browser.tabs.query({ active: true, currentWindow: true }); - return global.page.tabs[tabs[0].id].loginList; + tabId = parseInt(window.location.search.split('=')[1]) || tabs[0].id; + global.page.currentTabId = tabId; + return global.page.tabs[tabId].loginList; }; $(async () => { await initColorTheme(); - const data = await getLoginData(); const ll = document.getElementById('login-list'); + let resolve = true; + for (let i = 0; i < data.logins.length; ++i) { const a = document.createElement('a'); a.setAttribute('class', 'list-group-item'); a.textContent = data.logins[i].login + ' (' + data.logins[i].name + ')'; $(a).data('creds', data.logins[i]); + $(a).click(function() { + resolve = false; if (data.resolve) { const creds = $(this).data('creds'); data.resolve({ @@ -26,8 +33,10 @@ $(async () => { } }); } + close(); }); + ll.appendChild(a); } @@ -38,16 +47,20 @@ $(async () => { }); $('#btn-dismiss').click(async () => { - const loginData = await getLoginData(); - // Using reject won't work with every browser. So return empty credentials instead. - if (loginData.resolve) { - loginData.resolve({ - authCredentials: { - username: '', - password: '' - } - }); - } + resolve = false; + data.resolve({ cancel: false }); close(); }); + + window.addEventListener('beforeunload', async () => { + const global = await browser.runtime.getBackgroundPage(); + + if (global.page.tabs[tabId]) { + global.page.tabs[tabId].httpAuthDialog = false; + + if (resolve && data.resolve) { + data.resolve({ cancel: false }); + } + } + }, false); });