#1458 hiddenNodes improvement

This commit is contained in:
Grzegorz Zając 2024-01-08 12:26:50 +01:00
parent 41f0f03d29
commit 6973f63899
4 changed files with 58 additions and 26 deletions

View File

@ -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",

View File

@ -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 <https://www.gnu.org/licenses/>
//
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;

View File

@ -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;

View File

@ -19,5 +19,6 @@
exports.addedNodes = require('./addedNodes');
exports.checkChildNodes = require('./checkChildNodes');
exports.getChildNodes = require('./getChildNodes');
exports.hiddenNodes = require('./hiddenNodes');
exports.removedNodes = require('./removedNodes');