Compare commits

...

4 Commits

Author SHA1 Message Date
Jose
082e070aea fix: handle undefined chrome handling in api initialization 2026-03-18 13:57:29 +01:00
Jose
edf404542b bump: update version to 4.1 in manifest.json 2026-03-18 13:48:17 +01:00
Jose
3176af094e fix: background.js, remove: unnecessary permissions 2026-03-18 13:46:03 +01:00
Jose
e8a14e7e69 add: background.js for automatic code input 2026-03-18 12:33:21 +01:00
7 changed files with 3783 additions and 63 deletions

View File

@@ -5,5 +5,5 @@
"@/*": ["src/*"] "@/*": ["src/*"]
} }
}, },
"include": ["src"] "include": ["src", "src/background.js", "src/content.js"]
} }

3645
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,14 +1,16 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "US 2FA Autofill", "name": "US 2FA Autofill",
"version": "4.0", "version": "4.1",
"description": "Rellena automáticamente el código 2FA en la Universidad de Sevilla.", "description": "Rellena automáticamente el código 2FA en la Universidad de Sevilla.",
"permissions": [ "permissions": [
"activeTab",
"scripting",
"tabs",
"storage" "storage"
], ],
"background": {
"service_worker": "scripts/background.js",
"type": "module",
"scripts": ["scripts/background.js"]
},
"action": { "action": {
"default_popup": "index.html" "default_popup": "index.html"
}, },
@@ -17,12 +19,8 @@
}, },
"content_scripts": [ "content_scripts": [
{ {
"matches": [ "matches": ["*://sso.us.es/*"],
"https://sso.us.es/*" "js": ["scripts/content.js"],
],
"js": [
"scripts/content.js"
],
"run_at": "document_idle" "run_at": "document_idle"
} }
], ],

View File

@@ -4,11 +4,24 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCircleInfo } from "@fortawesome/free-solid-svg-icons"; import { faCircleInfo } from "@fortawesome/free-solid-svg-icons";
import { ThemeToggle } from "./components/ThemeToggle"; import { ThemeToggle } from "./components/ThemeToggle";
// eslint-disable-next-line no-undef
const api = typeof browser !== "undefined" ? browser : (typeof chrome !== "undefined" ? chrome : null);
const App = () => { const App = () => {
const [secret, setSecret] = useState(localStorage.getItem('shasecret') || ''); const [secret, setSecret] = useState('');
const [token, setToken] = useState('000000'); const [token, setToken] = useState('000000');
const [remaining, setRemaining] = useState(30); const [remaining, setRemaining] = useState(30);
useEffect(() => {
if (api?.storage?.local) {
api.storage.local.get(['shasecret'], (result) => {
if (result.shasecret) {
setSecret(result.shasecret);
}
});
}
}, []);
useEffect(() => { useEffect(() => {
if (!secret) return; if (!secret) return;
@@ -26,22 +39,17 @@ const App = () => {
const newToken = totp.generate() const newToken = totp.generate()
setToken(newToken); setToken(newToken);
// eslint-disable-next-line no-undef if (api?.tabs?.query) {
const api = typeof browser !== "undefined" ? browser : chrome;
if (api && api.tabs && api.tabs.query) {
api.tabs.query({ active: true, currentWindow: true }, (tabs) => { api.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]?.id && tabs[0].url?.includes("sso.us.es")) { if (tabs[0]?.url?.includes("sso.us.es")) {
api.tabs.sendMessage(tabs[0].id, { api.tabs.sendMessage(tabs[0].id, {
action: "autofill", action: "autofill",
code: newToken code: newToken
}).catch(() => { }).catch(() => {});
console.log("Esperando inyección...");
});
} }
}); });
} else { } else {
console.log("Entorno de desarrollo: Autofill desactivado."); console.debug("DevEnv");
} }
} catch (err) { } catch (err) {
@@ -68,22 +76,29 @@ const App = () => {
const handleSaveSecret = (e) => { const handleSaveSecret = (e) => {
if (e.key === 'Enter') { if (e.key === 'Enter') {
const val = e.target.value.trim(); const val = e.target.value.trim();
localStorage.setItem('shasecret', val); if (api?.storage?.local) {
api.storage.local.set({ 'shasecret': val }, () => {
setSecret(val); setSecret(val);
console.log("Secret stored");
});
}
e.target.value = ''; e.target.value = '';
} }
}; };
const resetSecret = () => {
if (api?.storage?.local) {
api.storage.local.remove(['shasecret'], () => {
setSecret('');
setToken('000000');
});
}
};
const copyToClipboard = () => { const copyToClipboard = () => {
navigator.clipboard.writeText(token); navigator.clipboard.writeText(token);
}; };
const resetSecret = () => {
localStorage.removeItem('shasecret');
setSecret('');
setToken('000000');
};
return ( return (
<div className="container-app"> <div className="container-app">
<div className="top-actions"> <div className="top-actions">

23
src/background.js Normal file
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;
}
});

View File

@@ -1,25 +1,66 @@
// eslint-disable-next-line no-undef (async function() {
const api = typeof browser !== "undefined" ? browser : chrome; // eslint-disable-next-line no-undef
const api = typeof browser !== "undefined" ? browser : chrome;
api.runtime.onMessage.addListener((request, sender, sendResponse) => { const fillAndSubmit = (code) => {
if (request.action === "autofill") { const input = document.getElementById("input2factor");
const input = document.getElementById('input2factor'); const button = document.getElementById("notification_2factor_button_ok");
const button = document.getElementById('btn-login');
const errorMsg = document.querySelector(".ui-state-error");
const isErrorVisible = errorMsg && errorMsg.style.display !== "none";
if (!input || input.value.length > 0 || isErrorVisible) {
return;
}
console.log("Autofill");
if (input) {
input.focus(); input.focus();
input.value = request.code; input.value = code;
['input', 'change', 'keyup', 'keydown'].forEach(evt => {
input.dispatchEvent(new Event(evt, { bubbles: true }));
});
input.dispatchEvent(new Event('input', { bubbles: true }));
input.dispatchEvent(new Event('change', { bubbles: true }));
if (button) {
setTimeout(() => { setTimeout(() => {
button.click(); if (button) {
}, 150); console.log("Clicking 'Aceptar'");
const clickEvent = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
});
button.dispatchEvent(clickEvent);
} }
sendResponse({ status: "bomba" }); }, 400);
};
const tryAutofill = async () => {
const data = await api.storage.local.get(['shasecret']);
if (data.shasecret) {
api.runtime.sendMessage({ action: "GENERATE_TOKEN" }, response => {
if (response?.token) {
fillAndSubmit(response.token);
} }
});
} }
return true; };
});
const observer = new MutationObserver(() => {
if (document.getElementById("input2factor")) {
tryAutofill();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
api.runtime.onMessage.addListener((request) => {
if (request.action === "autofill") {
fillAndSubmit(request.code);
}
});
tryAutofill();
})();

View File

@@ -6,28 +6,26 @@ export default defineConfig({
plugins: [react()], plugins: [react()],
build: { build: {
rollupOptions: { rollupOptions: {
input: [ input: {
resolve(__dirname, 'index.html'), main: resolve(__dirname, 'index.html'),
resolve(__dirname, 'src/content.js') content: resolve(__dirname, 'src/content.js'),
], background: resolve(__dirname, 'src/background.js'),
},
output: { output: {
format: 'es',
entryFileNames: (chunkInfo) => { entryFileNames: (chunkInfo) => {
if (chunkInfo.name === 'content') { if (chunkInfo.name === 'content' || chunkInfo.name === 'background') {
return 'scripts/content.js'; return 'scripts/[name].js';
} }
return 'assets/[name]-[hash].js'; return 'assets/[name]-[hash].js';
}, },
manualChunks: undefined,
}, },
}, },
}, },
server: {
port: 3000,
},
resolve: { resolve: {
alias: { alias: {
'@/': '/src/', '@': resolve(__dirname, './src'),
}, },
}, },
publicDir: 'public',
}); });