mirror of
https://github.com/twofas/2fas-browser-extension.git
synced 2024-11-22 02:00:08 +01:00
#1458 AutoSubmit WIP
This commit is contained in:
parent
7cadf2c8dd
commit
861953d26d
@ -37,7 +37,8 @@ const onMessage = (request, sender) => {
|
||||
return resolve({
|
||||
id: sender?.tab?.id,
|
||||
url: sender?.tab?.url,
|
||||
urlPath
|
||||
urlPath,
|
||||
status: sender?.tab?.status
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@
|
||||
import './styles/content_script.scss';
|
||||
const browser = require('webextension-polyfill');
|
||||
const { observe, createObserver } = require('./observer');
|
||||
const { getTabData, getInputs, addInputListener, portSetup, isInFrame } = require('./functions');
|
||||
const { getTabData, getInputs, addInputListener, portSetup, isInFrame, addFormElementsNumber, getFormElements } = require('./functions');
|
||||
const contentOnMessage = require('./events/contentOnMessage');
|
||||
const { loadFromLocalStorage, saveToLocalStorage } = require('../localStorage');
|
||||
const storeLog = require('../partials/storeLog');
|
||||
@ -59,6 +59,7 @@ const contentScriptRun = async () => {
|
||||
}
|
||||
|
||||
addInputListener(getInputs(), tabData?.id);
|
||||
addFormElementsNumber(getFormElements());
|
||||
|
||||
const mutationObserver = createObserver(tabData);
|
||||
observe(mutationObserver);
|
||||
|
38
src/content/functions/addFormElementsNumber.js
Normal file
38
src/content/functions/addFormElementsNumber.js
Normal file
@ -0,0 +1,38 @@
|
||||
//
|
||||
// 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 addFormElementsNumber = elements => {
|
||||
let i = 0;
|
||||
|
||||
if (!Array.isArray(elements) || elements?.length <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
elements.map(element => {
|
||||
if (element?.dataset?.twofasElementNumber) {
|
||||
return false;
|
||||
}
|
||||
|
||||
element.dataset.twofasElementNumber = i;
|
||||
i++;
|
||||
return element;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = addFormElementsNumber;
|
59
src/content/functions/clickSubmit.js
Normal file
59
src/content/functions/clickSubmit.js
Normal file
@ -0,0 +1,59 @@
|
||||
//
|
||||
// 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 getFormSubmitElements = require('./getFormSubmitElements');
|
||||
|
||||
const closest = (counts, goal) => {
|
||||
return counts.indexOf(
|
||||
counts.reduce((prev, curr) => {
|
||||
return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
const clickSubmit = (inputElement, siteURL) => {
|
||||
// @TODO: check URL
|
||||
|
||||
const inputNumber = parseInt(inputElement?.dataset?.twofasElementNumber || -999);
|
||||
const submits = getFormSubmitElements();
|
||||
|
||||
if (submits.length === 0) {
|
||||
return false;
|
||||
} else if (submits.length === 1) {
|
||||
try {
|
||||
submits[0].click();
|
||||
} catch (e) {}
|
||||
} else {
|
||||
const submitsNumbers = [];
|
||||
|
||||
submits.forEach(submit => {
|
||||
submitsNumbers.push(parseInt(submit?.dataset?.twofasElementNumber || -999));
|
||||
});
|
||||
|
||||
const submitElement = submits[closest(submitsNumbers, inputNumber)];
|
||||
|
||||
if (submitElement) {
|
||||
try {
|
||||
submitElement.click();
|
||||
} catch (e) {}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = clickSubmit;
|
29
src/content/functions/getFormElements.js
Normal file
29
src/content/functions/getFormElements.js
Normal file
@ -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 <https://www.gnu.org/licenses/>
|
||||
//
|
||||
|
||||
const getInputs = require('./getInputs');
|
||||
const getFormSubmitElements = require('./getFormSubmitElements');
|
||||
|
||||
const getFormElements = () => {
|
||||
const inputs = getInputs();
|
||||
const formSubmitElements = getFormSubmitElements();
|
||||
return inputs.concat(formSubmitElements);
|
||||
};
|
||||
|
||||
module.exports = getFormElements;
|
34
src/content/functions/getFormSubmitElements.js
Normal file
34
src/content/functions/getFormSubmitElements.js
Normal file
@ -0,0 +1,34 @@
|
||||
//
|
||||
// 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 getFormSubmitElements = () => {
|
||||
let submits = Array.from(
|
||||
document.querySelectorAll(require('../../partials/formSubmitSelectors')())
|
||||
);
|
||||
|
||||
if (submits.length <= 0) {
|
||||
submits = Array.from(
|
||||
document.querySelectorAll(require('../../partials/formSubmitSecondSelectors')())
|
||||
);
|
||||
}
|
||||
|
||||
return submits;
|
||||
};
|
||||
|
||||
module.exports = getFormSubmitElements;
|
@ -17,8 +17,12 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>
|
||||
//
|
||||
|
||||
exports.addFormElementsNumber = require('./addFormElementsNumber');
|
||||
exports.addInputListener = require('./addInputListener');
|
||||
exports.clickSubmit = require('./clickSubmit');
|
||||
exports.closeNotificationInfo = require('./closeNotificationInfo');
|
||||
exports.getFormElements = require('./getFormElements');
|
||||
exports.getFormSubmitElements = require('./getFormSubmitElements');
|
||||
exports.getInputs = require('./getInputs');
|
||||
exports.getTabData = require('./getTabData');
|
||||
exports.getTokenInput = require('./getTokenInput');
|
||||
|
@ -19,6 +19,8 @@
|
||||
|
||||
/* global Event, KeyboardEvent */
|
||||
const delay = require('../../partials/delay');
|
||||
const getTabData = require('./getTabData');
|
||||
const clickSubmit = require('./clickSubmit');
|
||||
|
||||
const inputToken = (request, inputElement, siteURL) => {
|
||||
return new Promise(resolve => {
|
||||
@ -69,10 +71,16 @@ const inputToken = (request, inputElement, siteURL) => {
|
||||
}
|
||||
|
||||
return Promise.all(promises)
|
||||
.then(() => {
|
||||
.then(async () => {
|
||||
clearEvent = null;
|
||||
inputEvent = null;
|
||||
|
||||
const tab = await getTabData();
|
||||
|
||||
if (tab.status === 'complete') {
|
||||
clickSubmit(inputElement, siteURL);
|
||||
}
|
||||
|
||||
return resolve({
|
||||
status: 'completed',
|
||||
url: siteURL
|
||||
|
46
src/partials/formSubmitSecondSelectors.js
Normal file
46
src/partials/formSubmitSecondSelectors.js
Normal file
@ -0,0 +1,46 @@
|
||||
//
|
||||
// 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 formSubmitSecondSelectors = () => {
|
||||
let buttonSelectors = [
|
||||
'button#btn-submit'
|
||||
];
|
||||
|
||||
const linkSelectors = [
|
||||
'a#loginButton.btn.btn-primary'
|
||||
];
|
||||
|
||||
const ignoreButtonSelectors = [
|
||||
':not([data-role="search-button"])',
|
||||
':not([data-role="search"])',
|
||||
':not(#search)'
|
||||
];
|
||||
|
||||
buttonSelectors = buttonSelectors.map(selector => {
|
||||
return selector + ignoreButtonSelectors.join('');
|
||||
});
|
||||
|
||||
if (linkSelectors.length <= 0) {
|
||||
return buttonSelectors.join(',');
|
||||
}
|
||||
|
||||
return buttonSelectors.join(',') + ',' + linkSelectors.join(',');
|
||||
};
|
||||
|
||||
module.exports = formSubmitSecondSelectors;
|
44
src/partials/formSubmitSelectors.js
Normal file
44
src/partials/formSubmitSelectors.js
Normal file
@ -0,0 +1,44 @@
|
||||
//
|
||||
// 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 formSubmitSelectors = () => {
|
||||
let buttonSelectors = [
|
||||
'button[type="submit"]'
|
||||
];
|
||||
|
||||
const linkSelectors = [];
|
||||
|
||||
const ignoreButtonSelectors = [
|
||||
':not([data-role="search-button"])',
|
||||
':not([data-role="search"])',
|
||||
':not(#search)'
|
||||
];
|
||||
|
||||
buttonSelectors = buttonSelectors.map(selector => {
|
||||
return selector + ignoreButtonSelectors.join('');
|
||||
});
|
||||
|
||||
if (linkSelectors.length <= 0) {
|
||||
return buttonSelectors.join(',');
|
||||
}
|
||||
|
||||
return buttonSelectors.join(',') + ',' + linkSelectors.join(',');
|
||||
};
|
||||
|
||||
module.exports = formSubmitSelectors;
|
@ -20,6 +20,8 @@
|
||||
exports.delay = require('./delay');
|
||||
exports.extNameUpdate = require('./extNameUpdate');
|
||||
exports.extPageOnMessage = require('./extPageOnMessage');
|
||||
exports.formSubmitSelectors = require('./formSubmitSelectors');
|
||||
exports.formSubmitSecondSelectors = require('./formSubmitSecondSelectors');
|
||||
exports.handleTargetBlank = require('./handleTargetBlank');
|
||||
exports.hidePreloader = require('./hidePreloader');
|
||||
exports.inputsSelectors = require('./inputsSelectors');
|
||||
|
@ -91,7 +91,8 @@ const storeLog = async (level, logID = 0, errObj, url = '') => {
|
||||
(storage?.browserInfo?.browser_name === 'Chrome' && storage?.browserInfo?.browser_version === '107.0.0.0' && logID === 14) ||
|
||||
(c?.errorInfo?.message.includes('FILE_ERROR_NO_SPACE')) ||
|
||||
(c?.status === 407) ||
|
||||
(c?.errorInfo?.message.includes('An unexpected error occurred'))
|
||||
(c?.errorInfo?.message.includes('An unexpected error occurred')) ||
|
||||
(c?.errorInfo?.message.includes('Refused to run the JavaScript URL'))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user