From a00a07ae26fb353eff75337541e955ad446280cc Mon Sep 17 00:00:00 2001 From: varjolintu Date: Wed, 15 Nov 2017 12:15:19 +0200 Subject: [PATCH] Added support for credential popups with only password fields visible --- keepassxc-browser/background/browserAction.js | 2 +- keepassxc-browser/background/event.js | 4 +-- keepassxc-browser/background/httpauth.js | 2 +- keepassxc-browser/background/init.js | 2 +- keepassxc-browser/background/keepass.js | 2 +- keepassxc-browser/keepassxc-browser.js | 32 ++++++++++++++----- keepassxc-browser/manifest.json | 2 +- 7 files changed, 31 insertions(+), 15 deletions(-) diff --git a/keepassxc-browser/background/browserAction.js b/keepassxc-browser/background/browserAction.js index 34547a5..b2cdd80 100644 --- a/keepassxc-browser/background/browserAction.js +++ b/keepassxc-browser/background/browserAction.js @@ -1,4 +1,4 @@ -var browserAction = {}; +const browserAction = {}; const BLINK_TIMEOUT_DEFAULT = 7500; const BLINK_TIMEOUT_REDIRECT_THRESHOLD_TIME_DEFAULT = -1; diff --git a/keepassxc-browser/background/event.js b/keepassxc-browser/background/event.js index 275a360..9b42ca3 100644 --- a/keepassxc-browser/background/event.js +++ b/keepassxc-browser/background/event.js @@ -1,4 +1,4 @@ -var kpxcEvent = {}; +const kpxcEvent = {}; kpxcEvent.onMessage = function(request, sender, callback) { if (request.action in kpxcEvent.messageHandlers) { @@ -124,7 +124,7 @@ kpxcEvent.onLoadKeyRing = function(callback, tab) { kpxcEvent.onSaveSettings = function(callback, tab, settings) { browser.storage.local.set({'settings': settings}).then(function() { - kpxcEvent.onLoadSettings(); + kpxcEvent.onLoadSettings(callback, tab); }); }; diff --git a/keepassxc-browser/background/httpauth.js b/keepassxc-browser/background/httpauth.js index f1ad0ba..35dc99e 100644 --- a/keepassxc-browser/background/httpauth.js +++ b/keepassxc-browser/background/httpauth.js @@ -1,4 +1,4 @@ -var httpAuth = httpAuth || {}; +const httpAuth = {}; httpAuth.requests = []; httpAuth.pendingCallbacks = []; diff --git a/keepassxc-browser/background/init.js b/keepassxc-browser/background/init.js index 4d58c68..149328a 100644 --- a/keepassxc-browser/background/init.js +++ b/keepassxc-browser/background/init.js @@ -11,7 +11,7 @@ keepass.migrateKeyRing().then(() => { }); // Milliseconds for intervall (e.g. to update browserAction) -let _interval = 250; +const _interval = 250; /** diff --git a/keepassxc-browser/background/keepass.js b/keepassxc-browser/background/keepass.js index 6f2244d..c3cd936 100644 --- a/keepassxc-browser/background/keepass.js +++ b/keepassxc-browser/background/keepass.js @@ -1,6 +1,6 @@ 'use strict'; -var keepass = {}; +const keepass = {}; keepass.associated = {'value': false, 'hash': null}; keepass.keyPair = {publicKey: null, secretKey: null}; diff --git a/keepassxc-browser/keepassxc-browser.js b/keepassxc-browser/keepassxc-browser.js index 1b4b5ca..db1ab30 100644 --- a/keepassxc-browser/keepassxc-browser.js +++ b/keepassxc-browser/keepassxc-browser.js @@ -4,6 +4,9 @@ $(this.target).find('input').autocomplete(); // contains already called method names var _called = {}; +// Count of detected form fields on the page +var _detectedFields = 0; + browser.runtime.onMessage.addListener(function(req, sender, callback) { if ('action' in req) { if (req.action === 'fill_user_pass_with_specific_login') { @@ -806,6 +809,7 @@ cipFields.getAllFields = function() { } }); + _detectedFields = fields.length; return fields; }; @@ -1075,7 +1079,7 @@ cipFields.useDefinedCredentialFields = function() { } if ($found) { - var fields = { + let fields = { username: creds.username, password: creds.password, fields: creds.fields @@ -1111,16 +1115,20 @@ cip.init = function() { action: 'load_settings', }).then((response) => { cip.settings = response; - cip.initCredentialFields(); + cip.initCredentialFields(true); }); }; cip.detectNewActiveFields = function() { const divDetect = setInterval(function() { - const fields = cipFields.getAllFields(); - if (fields.length > 1) { - cip.initCredentialFields(true); - clearInterval(divDetect); + if (document.visibilityState !== 'hidden') { + const fields = cipFields.getAllFields(); + + // If only password field is shown it's enough to have one field visible for initCredentialFields + if (fields.length > (_detectedFields == 1 ? 0 : 1)) { + cip.initCredentialFields(true); + clearInterval(divDetect); + } } }, 1000); }; @@ -1290,8 +1298,16 @@ cip.preparePageForMultipleCredentials = function(credentials) { // initialize autocomplete for username fields if (cip.settings.autoCompleteUsernames) { for (const i of cipFields.combinations) { - if (_f(i.username)) { - cipAutocomplete.init(_f(i.username)); + // Both username and password fields are visible + if (_detectedFields >= 2) { + if (_f(i.username)) { + cipAutocomplete.init(_f(i.username)); + } + } else if (_detectedFields == 1) { + // If only password field is the visible one + if (_f(i.password)) { + cipAutocomplete.init(_f(i.password)); + } } } } diff --git a/keepassxc-browser/manifest.json b/keepassxc-browser/manifest.json index 9ca67fd..98a1b1a 100644 --- a/keepassxc-browser/manifest.json +++ b/keepassxc-browser/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "keepassxc-browser", - "version": "0.4.0", + "version": "0.4.1", "description": "KeePassXC integration for modern web browsers", "author": "Sami Vänttinen", "icons": {