add: background.js for automatic code input
This commit is contained in:
@@ -5,5 +5,5 @@
|
|||||||
"@/*": ["src/*"]
|
"@/*": ["src/*"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"include": ["src"]
|
"include": ["src", "public/scripts/background.js", "public/scripts/content.js"]
|
||||||
}
|
}
|
||||||
3645
package-lock.json
generated
Normal file
3645
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -9,6 +9,11 @@
|
|||||||
"tabs",
|
"tabs",
|
||||||
"storage"
|
"storage"
|
||||||
],
|
],
|
||||||
|
"background": {
|
||||||
|
"service_worker": "background.js",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": ["background.js"]
|
||||||
|
},
|
||||||
"action": {
|
"action": {
|
||||||
"default_popup": "index.html"
|
"default_popup": "index.html"
|
||||||
},
|
},
|
||||||
@@ -17,13 +22,9 @@
|
|||||||
},
|
},
|
||||||
"content_scripts": [
|
"content_scripts": [
|
||||||
{
|
{
|
||||||
"matches": [
|
"matches": ["https://sso.us.es/*"],
|
||||||
"https://sso.us.es/*"
|
"js": ["scripts/content.js"],
|
||||||
],
|
"run_at": "document_start"
|
||||||
"js": [
|
|
||||||
"scripts/content.js"
|
|
||||||
],
|
|
||||||
"run_at": "document_idle"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"host_permissions": [
|
"host_permissions": [
|
||||||
|
|||||||
23
public/scripts/background.js
Normal file
23
public/scripts/background.js
Normal 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
39
public/scripts/content.js
Normal 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
51
src/App.jsx
51
src/App.jsx
@@ -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 : chrome;
|
||||||
|
|
||||||
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,15 @@ 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 {
|
|
||||||
console.log("Entorno de desarrollo: Autofill desactivado.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -68,22 +74,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) {
|
||||||
setSecret(val);
|
api.storage.local.set({ 'shasecret': 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">
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
// eslint-disable-next-line no-undef
|
|
||||||
const api = typeof browser !== "undefined" ? browser : chrome;
|
|
||||||
|
|
||||||
api.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
|
||||||
if (request.action === "autofill") {
|
|
||||||
const input = document.getElementById('input2factor');
|
|
||||||
const button = document.getElementById('btn-login');
|
|
||||||
|
|
||||||
if (input) {
|
|
||||||
input.focus();
|
|
||||||
input.value = request.code;
|
|
||||||
|
|
||||||
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
||||||
input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
||||||
|
|
||||||
if (button) {
|
|
||||||
setTimeout(() => {
|
|
||||||
button.click();
|
|
||||||
}, 150);
|
|
||||||
}
|
|
||||||
sendResponse({ status: "bomba" });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
@@ -8,13 +8,9 @@ export default defineConfig({
|
|||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
input: [
|
input: [
|
||||||
resolve(__dirname, 'index.html'),
|
resolve(__dirname, 'index.html'),
|
||||||
resolve(__dirname, 'src/content.js')
|
|
||||||
],
|
],
|
||||||
output: {
|
output: {
|
||||||
entryFileNames: (chunkInfo) => {
|
entryFileNames: (chunkInfo) => {
|
||||||
if (chunkInfo.name === 'content') {
|
|
||||||
return 'scripts/content.js';
|
|
||||||
}
|
|
||||||
return 'assets/[name]-[hash].js';
|
return 'assets/[name]-[hash].js';
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user