From 02f766fc52e4bcb6d1bd42cc0ad6af887e125c66 Mon Sep 17 00:00:00 2001 From: varjolintu Date: Mon, 9 Apr 2018 10:47:37 +0300 Subject: [PATCH] Improved dynamic input field detection --- keepassxc-browser/keepassxc-browser.js | 43 +++++++++++++++++--------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/keepassxc-browser/keepassxc-browser.js b/keepassxc-browser/keepassxc-browser.js index c399a8c..e85a6f2 100755 --- a/keepassxc-browser/keepassxc-browser.js +++ b/keepassxc-browser/keepassxc-browser.js @@ -1140,7 +1140,34 @@ cipFields.useDefinedCredentialFields = function() { return false; }; +MutationObserver = window.MutationObserver || window.WebKitMutationObserver; +// Detects DOM changes in the document +let observer = new MutationObserver(function(mutations, observer) { + if (document.visibilityState === 'hidden') { + return; + } + + for (const mut of mutations) { + if (mut.type === 'childList') { + 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); + } + } + } +}); + +// define what element should be observed by the observer +// and what types of mutations trigger the callback +observer.observe(document, { + subtree: true, + attributes: true, + childList: true, + characterData: true +}); var cip = {}; cip.settings = {}; @@ -1152,7 +1179,7 @@ cip.credentials = []; jQuery(function() { cip.init(); - cip.detectNewActiveFields(); + cip.detectDatabaseChange(); }); cip.init = function() { @@ -1164,20 +1191,6 @@ cip.init = function() { }); }; -cip.detectNewActiveFields = function() { - const divDetect = setInterval(function() { - 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); -}; - // Switch credentials if database is changed or closed cip.detectDatabaseChange = function(response) { if (document.visibilityState !== 'hidden') {