Skip to content

Commit

Permalink
reimplement the onClick event with difrent method to prevent not firi…
Browse files Browse the repository at this point in the history
…ng event in some cases
  • Loading branch information
mostafa-kheibary committed Feb 11, 2024
1 parent e126203 commit 6d2489d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
22 changes: 22 additions & 0 deletions src/class/DomManipulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,26 @@ class DomManipulator {
url: Url = { path: "", params: {} };
eventsListener: Record<string, ((e: Event) => void)[]> = {};
observerCalls: MutationCallback[] = [];
clickMap: Record<string, Function[]> = {};

constructor(private readonly main: HTMLElement, private readonly storage: LocalStorage) {
document.addEventListener(
"click",
(e) => {
for (const selector in this.clickMap) {
const el = document.querySelector(selector);
if (el) {
const { top, left, width, height } = el.getBoundingClientRect();
const { pageX, pageY } = e;
if (pageX >= left && pageX <= left + width && pageY >= top && pageY <= top + height) {
this.clickMap[selector].forEach((callback) => callback());
}
}
}
},
{ capture: true }
);

const globalObserver = new MutationObserver((mutations) => {
this.observerCalls.forEach((callback) => {
callback(mutations, globalObserver);
Expand Down Expand Up @@ -96,6 +114,10 @@ class DomManipulator {
}
});
}
public async onClick(selector: string, callback: Function) {
if (!this.clickMap[selector]) this.clickMap[selector] = [];
this.clickMap[selector].push(callback);
}
static async getElement<T>(selector: string) {
while (document.querySelector(selector) === null) {
await new Promise((resolve) => requestAnimationFrame(resolve));
Expand Down
2 changes: 2 additions & 0 deletions src/class/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class ChatGuard {
root = selectors[window.location.hostname];
selector: Selector["selector"]["desktop"];
state = writable({ value: "", encrypted: "", submit: false, loading: false });
isTouch = false;

constructor() {
const type = getDeviceType();
if (type === "mobile") this.isTouch = true;
const selector = selectors[window.location.hostname].selector[type];
if (selector) this.selector = selector;
else this.selector = selectors[window.location.hostname].selector.desktop;
Expand Down
20 changes: 11 additions & 9 deletions src/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ import { get } from "svelte/store";
new Status({ target, props: { cipher, app, dom, name: "status" } });
});

dom.onClick(app.selector.submitButton, () => {
const state = get(app.state);
if (!state.encrypted) return;
DomManipulator.typeTo(app.selector.textField, state.encrypted);
app.state.update((prev) => ({ ...prev, value: "", encrypted: "", submit: true }));
});

dom.on(app.selector.textFieldWrapper, "input", async (event: Event) => {
app.state.update((state) => ({ ...state, value: (event.target as HTMLElement).innerText }));
app.state.update((state) => ({ ...state, value: (event.target as HTMLElement).textContent || "" }));
const state = get(app.state);

if (state.value.startsWith(config.ENCRYPT_PREFIX) && !state.submit) {
Expand All @@ -33,20 +40,15 @@ import { get } from "svelte/store";
if (encrypted) app.state.update((prev) => ({ ...prev, encrypted }));
});
dom.on(app.selector.textField, "keydown", (event) => {
app.state.update((prev) => ({ ...prev, value: (event.target as HTMLElement).innerText }));
app.state.update((prev) => ({ ...prev, value: (event.target as HTMLElement).textContent || "" }));
const state = get(app.state);
const e = event as KeyboardEvent;
if (e.key === "Enter" && state.value && !e.shiftKey && e.detail !== 11 && state.encrypted) {

if (e.key === "Enter" && state.value && !e.shiftKey && e.detail !== 11 && state.encrypted && !app.isTouch) {
DomManipulator.typeTo(app.selector.textField, state.encrypted);
app.state.update((prev) => ({ ...prev, value: "", encrypted: "", submit: true }));
}
});
dom.on(app.selector.submitButton, "click", async (e) => {
console.log("click");
const state = get(app.state);
DomManipulator.typeTo(app.selector.textField, state.encrypted);
app.state.update((prev) => ({ ...prev, value: "", encrypted: "", submit: true }));
});

dom.observerGlobal(() => {
dom.urlObserver(() => {
Expand Down

0 comments on commit 6d2489d

Please sign in to comment.