diff --git a/package.json b/package.json index c9fb693..b358358 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "css-minimizer-webpack-plugin": "^5.0.1", "dotenv": "^16.3.1", "dotenv-webpack": "^8.0.1", - "eslint": "8.55.0", + "eslint": "8.56.0", "eslint-config-standard": "^17.1.0", "eslint-friendly-formatter": "^4.0.1", "eslint-plugin-import": "^2.29.0", diff --git a/src/content/observer/observerFunctions/getChildNodes.js b/src/content/observer/observerFunctions/getChildNodes.js new file mode 100644 index 0000000..541b1c8 --- /dev/null +++ b/src/content/observer/observerFunctions/getChildNodes.js @@ -0,0 +1,36 @@ +// +// This file is part of the 2FAS Browser Extension (https://github.com/twofas/2fas-browser-extension) +// Copyright © 2023 Two Factor Authentication Service, Inc. +// Contributed by Grzegorz Zając. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// + +const getChildNodes = childNodes => { + const cN = Array.from(childNodes); + + if (cN && cN.length > 0) { + return cN.map(childNode => { + if (childNode?.childNodes && childNode?.childNodes?.length > 0) { + return getChildNodes(childNode.childNodes).flat(); + } + + return childNode; + }); + } else { + return []; + } +}; + +module.exports = getChildNodes; diff --git a/src/content/observer/observerFunctions/hiddenNodes.js b/src/content/observer/observerFunctions/hiddenNodes.js index b79c874..40ca287 100644 --- a/src/content/observer/observerFunctions/hiddenNodes.js +++ b/src/content/observer/observerFunctions/hiddenNodes.js @@ -18,32 +18,14 @@ // const isVisible = require('../../functions/isVisible'); -const significantInputs = require('../observerConstants/significantInputs'); +const findSignificantChanges = require('./findSignificantChanges'); +const getChildNodes = require('./getChildNodes'); const { loadFromLocalStorage, saveToLocalStorage } = require('../../../localStorage'); const storeLog = require('../../../partials/storeLog'); const hiddenNodes = async (mutation, tabData) => { let storage; - const node = mutation.target; - const nodeName = node.nodeName.toLowerCase(); - - if (!significantInputs.includes(nodeName)) { - return false; - } - - const twofasInput = node.getAttribute('data-twofas-input'); - - if (!twofasInput) { - return false; - } - - const visible = await isVisible(node); - - if (visible) { - return false; - } - try { storage = await loadFromLocalStorage([`tabData-${tabData?.id}`]); } catch (err) { @@ -54,12 +36,25 @@ const hiddenNodes = async (mutation, tabData) => { return false; } - if (twofasInput === storage[`tabData-${tabData?.id}`].lastFocusedInput) { - delete storage[`tabData-${tabData?.id}`].lastFocusedInput; - } + let hiddenInputs = [mutation.target, ...getChildNodes(mutation.target.childNodes).flat()]; + hiddenInputs = hiddenInputs.filter(node => findSignificantChanges(node) && node.getAttribute('data-twofas-input')); - return saveToLocalStorage({ [`tabData-${tabData?.id}`]: storage[`tabData-${tabData?.id}`] }) - .catch(err => storeLog('error', 42, err, tabData?.url)); + return hiddenInputs.map(async node => { + const visible = await isVisible(node); + + if (node.getAttribute('data-twofas-input') === storage[`tabData-${tabData?.id}`].lastFocusedInput && !visible) { + delete storage[`tabData-${tabData?.id}`].lastFocusedInput; + + if (document?.activeElement && document?.activeElement?.getAttribute('data-twofas-input')) { + storage[`tabData-${tabData?.id}`].lastFocusedInput = document.activeElement.getAttribute('data-twofas-input'); + } + + return saveToLocalStorage({ [`tabData-${tabData?.id}`]: storage[`tabData-${tabData?.id}`] }) + .catch(err => storeLog('error', 42, err, tabData?.url)); + } + + return false; + }); }; module.exports = hiddenNodes; diff --git a/src/content/observer/observerFunctions/index.js b/src/content/observer/observerFunctions/index.js index 3194002..55608d8 100644 --- a/src/content/observer/observerFunctions/index.js +++ b/src/content/observer/observerFunctions/index.js @@ -19,5 +19,6 @@ exports.addedNodes = require('./addedNodes'); exports.checkChildNodes = require('./checkChildNodes'); +exports.getChildNodes = require('./getChildNodes'); exports.hiddenNodes = require('./hiddenNodes'); exports.removedNodes = require('./removedNodes');