From fe2783381df51e3e413095e5a3fcbbf9102153b8 Mon Sep 17 00:00:00 2001 From: Greg Zajac Date: Tue, 9 Jul 2024 14:01:35 +0200 Subject: [PATCH] #1570 inputToken improvement --- src/content/functions/inputToken.js | 14 +++++++------- src/partials/index.js | 2 ++ src/partials/runTasksWithDelay.js | 29 +++++++++++++++++++++++++++++ src/partials/wait.js | 24 ++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 src/partials/runTasksWithDelay.js create mode 100644 src/partials/wait.js diff --git a/src/content/functions/inputToken.js b/src/content/functions/inputToken.js index 33b88be..7f8b5f7 100644 --- a/src/content/functions/inputToken.js +++ b/src/content/functions/inputToken.js @@ -18,7 +18,7 @@ // /* global Event, KeyboardEvent, InputEvent */ -const delay = require('../../partials/delay'); +const runTasksWithDelay = require('../../partials/runTasksWithDelay'); const getTabData = require('./getTabData'); const clickSubmit = require('./clickSubmit'); const clearAfterInputToken = require('./clearAfterInputToken'); @@ -35,14 +35,14 @@ const inputToken = (request, inputElement, siteURL) => { const tokenLength = request.token.length; const promises = []; - let currentToken = ''; + inputElement.value = ''; inputElement.focus(); for (let i = 0; i < tokenLength; i++) { promises.push( - delay(() => { - if (document.activeElement.value.length > 0 && document.activeElement.value !== currentToken) { + () => new Promise(resolve => { + if (document.activeElement !== inputElement) { document.activeElement.value = ''; } @@ -59,12 +59,12 @@ const inputToken = (request, inputElement, siteURL) => { document.activeElement.dispatchEvent(new KeyboardEvent('keyup', { bubbles: true, cancelable: true, charCode: 0, code: `Digit${request.token[i]}`, ctrlKey: false, key: request.token[i], keyCode: 48 + parseInt(request.token[i], 10), location: 0, metaKey: false, repeat: false, shiftKey: false, which: 48 + parseInt(request.token[i], 10) })); document.activeElement.dispatchEvent(new Event('change', { bubbles: true, cancelable: true })); - currentToken += request.token[i]; - }, 100 * i) + return resolve(); + }) ); } - return Promise.all(promises) + return runTasksWithDelay(promises, 150) .then(async () => { const tab = await getTabData(); diff --git a/src/partials/index.js b/src/partials/index.js index f64a125..507b610 100644 --- a/src/partials/index.js +++ b/src/partials/index.js @@ -32,7 +32,9 @@ exports.inputsSelectors = require('./inputsSelectors'); exports.months = require('./months'); exports.onTabFocused = require('./onTabFocused'); exports.openShortcutEdit = require('./openShortcutEdit'); +exports.runTasksWithDelay = require('./runTasksWithDelay'); exports.sendMessageToTab = require('./sendMessageToTab'); exports.storageValidation = require('./storageValidation'); exports.storeLog = require('./storeLog'); exports.uniqueOnly = require('./uniqueOnly'); +exports.wait = require('./wait'); diff --git a/src/partials/runTasksWithDelay.js b/src/partials/runTasksWithDelay.js new file mode 100644 index 0000000..ebc8393 --- /dev/null +++ b/src/partials/runTasksWithDelay.js @@ -0,0 +1,29 @@ +// +// 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 wait = require('./wait'); + +const runTasksWithDelay = async (tasks, delayTime) => { + for (const task of tasks) { + await task(); + await wait(delayTime); + } +}; + +module.exports = runTasksWithDelay; diff --git a/src/partials/wait.js b/src/partials/wait.js new file mode 100644 index 0000000..c2aee19 --- /dev/null +++ b/src/partials/wait.js @@ -0,0 +1,24 @@ +// +// 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 wait = async ms => { + return new Promise(resolve => setTimeout(resolve, ms)); +}; + +module.exports = wait;