From 56f559283b04192b13aaf78f939a0bd82ea20f90 Mon Sep 17 00:00:00 2001 From: GrzegorzZajac000 Date: Fri, 22 Mar 2024 14:14:02 +0100 Subject: [PATCH] #1515 Code refactor --- logsCodes.txt | 14 ++--- src/_locales/en/notifications.json | 2 - src/config.js | 4 -- src/content/functions/addInputListener.js | 45 -------------- src/content/functions/getInputs.js | 27 --------- src/content/functions/index.js | 5 -- src/content/functions/inputFocus.js | 63 -------------------- src/content/functions/isVisible.js | 32 ---------- src/content/functions/updateEventListener.js | 44 -------------- 9 files changed, 7 insertions(+), 229 deletions(-) delete mode 100644 src/content/functions/addInputListener.js delete mode 100644 src/content/functions/getInputs.js delete mode 100644 src/content/functions/inputFocus.js delete mode 100644 src/content/functions/isVisible.js delete mode 100644 src/content/functions/updateEventListener.js diff --git a/logsCodes.txt b/logsCodes.txt index 84e0e35..edffcf2 100644 --- a/logsCodes.txt +++ b/logsCodes.txt @@ -49,13 +49,13 @@ 16 - inputFocus 17 - contentOnMessage 18 - neverShowNotificationInfo -*19 - openOptionsPage [redundant] -39 - removedNodes - loadFromLocalStorage -40 - removedNodes - saveToLocalStorage -41 - hiddenNodes - loadFromLocalStorage -42 - hiddenNodes - saveToLocalStorage -43 - pageLoadComplete - loadFromLocalStorage -44 - pageLoadComplete - saveToLocalStorage +*19 - openOptionsPage [deprecated] +*39 - removedNodes - loadFromLocalStorage [deprecated] +*40 - removedNodes - saveToLocalStorage [deprecated] +*41 - hiddenNodes - loadFromLocalStorage [deprecated] +*42 - hiddenNodes - saveToLocalStorage [deprecated] +*43 - pageLoadComplete - loadFromLocalStorage [deprecated] +*44 - pageLoadComplete - saveToLocalStorage [deprecated] 46 - clickSubmit - loadFromLocalStorage // INSTALL PAGE diff --git a/src/_locales/en/notifications.json b/src/_locales/en/notifications.json index 735b7f5..352e534 100644 --- a/src/_locales/en/notifications.json +++ b/src/_locales/en/notifications.json @@ -35,8 +35,6 @@ "errorStorageIntegrityMessage": "Please reinstall browser extension or contact with 2FAS Support on Discord", "warningTooSoonTitle": "Wait a moment", "warningTooSoonMessage": "Please wait DIFF seconds before sending another request.", - "warningSelectInputTitle": "Select the text field first", - "warningSelectInputMessage": "Select the text field for the 2FA token then click the extension icon or use the chosen shortcut.", "successPushSentTitle": "Push sent", "successPushSentMessage": "Please check your phone and accept your login request.", "successPushSentClipboardTitle": "Push sent", diff --git a/src/config.js b/src/config.js index 3373dd0..3876244 100644 --- a/src/config.js +++ b/src/config.js @@ -104,10 +104,6 @@ const config = { Title: browser.i18n.getMessage('warningTooSoonTitle') || t.warningTooSoonTitle, Message: (browser.i18n.getMessage('warningTooSoonMessage') || t.warningTooSoonMessage).replace('DIFF', 10 - Math.round(diff)) } - }, - SelectInput: { - Title: browser.i18n.getMessage('warningSelectInputTitle') || t.warningSelectInputTitle, - Message: browser.i18n.getMessage('warningSelectInputMessage') || t.warningSelectInputMessage } }, Success: { diff --git a/src/content/functions/addInputListener.js b/src/content/functions/addInputListener.js deleted file mode 100644 index a554787..0000000 --- a/src/content/functions/addInputListener.js +++ /dev/null @@ -1,45 +0,0 @@ -// -// 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 updateEventListener = require('./updateEventListener'); -const inputFocus = require('./inputFocus'); - -const addInputListener = (elements, tabID) => { - if (!Array.isArray(elements) || elements?.length <= 0) { - return false; - } - - let func = event => inputFocus(event, tabID); - - elements.map(input => { - if (input?.dataset?.twofasInputListener === 'true' && input?.dataset.twofasInput) { - if (input === document.activeElement || input.matches(':focus')) { - return func({ target: input }); - } else { - return false; - } - } - - return updateEventListener(input, func); - }); - - func = null; -}; - -module.exports = addInputListener; diff --git a/src/content/functions/getInputs.js b/src/content/functions/getInputs.js deleted file mode 100644 index bce21c1..0000000 --- a/src/content/functions/getInputs.js +++ /dev/null @@ -1,27 +0,0 @@ -// -// 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 inputsSelectors = require('../../partials/inputsSelectors')(); - -const getInputs = node => { - const el = node || document; - return Array.from(el.querySelectorAll(inputsSelectors)); -}; - -module.exports = getInputs; diff --git a/src/content/functions/index.js b/src/content/functions/index.js index 97a1387..f4c6d49 100644 --- a/src/content/functions/index.js +++ b/src/content/functions/index.js @@ -18,20 +18,16 @@ // exports.addFormElementsNumber = require('./addFormElementsNumber'); -exports.addInputListener = require('./addInputListener'); exports.clearFormElementsNumber = require('./clearFormElementsNumber'); exports.clickSubmit = require('./clickSubmit'); exports.closeNotificationInfo = require('./closeNotificationInfo'); exports.getActiveElement = require('./getActiveElement'); exports.getFormElements = require('./getFormElements'); exports.getFormSubmitElements = require('./getFormSubmitElements'); -exports.getInputs = require('./getInputs'); exports.getTabData = require('./getTabData'); exports.getTokenInput = require('./getTokenInput'); -exports.inputFocus = require('./inputFocus'); exports.inputToken = require('./inputToken'); exports.isInFrame = require('./isInFrame'); -exports.isVisible = require('./isVisible'); exports.loadFonts = require('./loadFonts'); exports.neverShowNotificationInfo = require('./neverShowNotificationInfo'); exports.notification = require('./notification'); @@ -39,4 +35,3 @@ exports.openOptionsPage = require('./openOptionsPage'); exports.portSetup = require('./portSetup'); exports.showNotificationInfo = require('./showNotificationInfo'); exports.tokenNotification = require('./tokenNotification'); -exports.updateEventListener = require('./updateEventListener'); diff --git a/src/content/functions/inputFocus.js b/src/content/functions/inputFocus.js deleted file mode 100644 index 5683d95..0000000 --- a/src/content/functions/inputFocus.js +++ /dev/null @@ -1,63 +0,0 @@ -// -// 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 browser = require('webextension-polyfill'); -const { loadFromLocalStorage, saveToLocalStorage } = require('../../localStorage'); -const storeLog = require('../../partials/storeLog'); -const { v4: uuidv4 } = require('uuid'); - -const inputFocus = async (event, tabID) => { - if (!browser?.runtime?.id || !event || !event?.target) { - return false; - } - - console.log('inputFocus', event, tabID); - - let storage; - const el = event.target; - - try { - storage = await loadFromLocalStorage([`tabData-${tabID}`]); - } catch (err) { - return storeLog('error', 16, err, 'inputFocus - no URL/tabID'); - } - - try { - const tabData = storage[`tabData-${tabID}`] ? storage[`tabData-${tabID}`] : {}; - - if ( - el?.dataset?.twofasInput?.length > 0 && - (typeof el?.dataset?.twofasInput === 'string' || el?.dataset?.twofasInput instanceof String) - ) { - tabData.lastFocusedInput = el.dataset.twofasInput; - } else { - if (typeof el?.setAttribute === 'function') { - const inputUUID = uuidv4(); - el.setAttribute('data-twofas-input', inputUUID); - tabData.lastFocusedInput = inputUUID; - } - } - - return saveToLocalStorage({ [`tabData-${tabID}`]: tabData }, storage); - } catch (err) { - return storeLog('error', 16, err, storage[`tabData-${tabID}`]?.url); - } -}; - -module.exports = inputFocus; diff --git a/src/content/functions/isVisible.js b/src/content/functions/isVisible.js deleted file mode 100644 index 927a3dc..0000000 --- a/src/content/functions/isVisible.js +++ /dev/null @@ -1,32 +0,0 @@ -// -// 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 -// - -/* global IntersectionObserver */ -const isVisible = domElement => { - return new Promise(resolve => { - const o = new IntersectionObserver(([entry]) => { - resolve(entry.intersectionRatio === 1); - o.disconnect(); - }); - - o.observe(domElement); - }); -}; - -module.exports = isVisible; diff --git a/src/content/functions/updateEventListener.js b/src/content/functions/updateEventListener.js deleted file mode 100644 index 9f92a4e..0000000 --- a/src/content/functions/updateEventListener.js +++ /dev/null @@ -1,44 +0,0 @@ -// -// 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 updateEventListener = (input, func) => { - const removeListener = () => { - if (typeof input.removeEventListener === 'function') { - input.removeEventListener('focus', func); - } - }; - - removeListener(); - - if (typeof input.addEventListener === 'function') { - input.addEventListener('focus', func); - } - - if (input && input?.dataset) { - input.dataset.twofasInputListener = 'true'; - } - - if (input === document.activeElement || input.matches(':focus')) { - func({ target: input }); - } - - window.addEventListener('onbeforeunload', removeListener, { once: true }); -}; - -module.exports = updateEventListener;