feat: simple extension setup
This commit is contained in:
46
src/content.ts
Normal file
46
src/content.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { State, STORAGE_KEY } from "./types";
|
||||
|
||||
const KEY_BINDINGS: Record<string, string> = {
|
||||
// TODO
|
||||
};
|
||||
|
||||
let enabled = false;
|
||||
|
||||
const clickButton = (selector: string): void => {
|
||||
const el = document.querySelector<HTMLElement>(selector);
|
||||
el?.click();
|
||||
};
|
||||
|
||||
const onKeyDown = (event: KeyboardEvent): void => {
|
||||
if (!enabled) return;
|
||||
|
||||
const target = event.target as HTMLElement;
|
||||
if (
|
||||
target.tagName === "INPUT" ||
|
||||
target.tagName === "TEXTAREA" ||
|
||||
target.isContentEditable
|
||||
) return;
|
||||
|
||||
const selector = KEY_BINDINGS[event.code];
|
||||
if (selector) {
|
||||
event.preventDefault();
|
||||
clickButton(selector);
|
||||
}
|
||||
};
|
||||
|
||||
const init = async (): Promise<void> => {
|
||||
const result = await chrome.storage.local.get(STORAGE_KEY);
|
||||
const state = result[STORAGE_KEY] as State | undefined;
|
||||
enabled = state?.enabled ?? true;
|
||||
|
||||
document.addEventListener("keydown", onKeyDown);
|
||||
|
||||
chrome.storage.onChanged.addListener((changes) => {
|
||||
if (STORAGE_KEY in changes) {
|
||||
const next = changes[STORAGE_KEY].newValue as State;
|
||||
enabled = next.enabled;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
init();
|
||||
Reference in New Issue
Block a user