add: background.js for automatic code input

This commit is contained in:
2026-03-18 12:33:21 +01:00
parent becc4f9445
commit e8a14e7e69
8 changed files with 3748 additions and 56 deletions

View File

@@ -0,0 +1,23 @@
import * as OTPAuth from 'otpauth';
// eslint-disable-next-line no-undef
const api = typeof browser !== "undefined" ? browser : chrome;
api.runtime.onMessage.addListener((request, _sender, sendResponse) => {
if (request.action === "GENERATE_TOKEN") {
api.storage.local.get(['shasecret'], (data) => {
if (data.shasecret) {
const totp = new OTPAuth.TOTP({
issuer: 'US',
label: '2FA',
algorithm: 'SHA1',
digits: 6,
period: 30,
secret: OTPAuth.Secret.fromBase32(data.shasecret.trim().toUpperCase()),
});
sendResponse({ token: totp.generate() });
}
});
return true;
}
});

39
public/scripts/content.js Normal file
View File

@@ -0,0 +1,39 @@
(async function() {
// eslint-disable-next-line no-undef
const api = typeof browser !== "undefined" ? browser : chrome;
const fillCode = (code) => {
const input = document.getElementById("input2factor");
const button = document.getElementById('btn-login') || document.querySelector("#notification_2factor_button_ok");
const error = document.querySelector("#otp_authn_wrong_code")?.offsetHeight > 0;
if (!input || error) return;
input.value = code;
input.dispatchEvent(new Event('input', { bubbles: true }));
setTimeout(() => {
if (button) button.click();
}, 200);
console.log("Autofill");
};
api.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "autofill") {
fillCode(request.code);
sendResponse({ status: "ok" });
}
});
const data = await api.storage.local.get(['shasecret']);
if (data.shasecret) {
const input = document.getElementById("input2factor");
if (input && input.value.length === 0) {
api.runtime.sendMessage({ action: "GENERATE_TOKEN" }, response => {
if (response?.token) {
fillCode(response.token);
}
});
}
}
})();