Skip to content

Commit 2bef6d9

Browse files
committed
Fix Bridge - Duplicated Click Events
1 parent b122b59 commit 2bef6d9

File tree

2 files changed

+983
-981
lines changed

2 files changed

+983
-981
lines changed

bridge/webui.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,12 @@ class WebuiBridge {
245245
}
246246
};
247247
#clicksListener() {
248-
Object.keys(window).forEach((key) => {
249-
if (/^on(click)/.test(key)) {
250-
globalThis.addEventListener(key.slice(2), (event) => {
251-
if (!(event.target instanceof HTMLElement)) return;
252-
if (this.#AllEvents ||
253-
((event.target.id !== '') &&
254-
(this.#bindsList.includes(event.target?.id)))
255-
) {
256-
this.#sendClick(event.target.id);
257-
}
258-
});
248+
document.querySelectorAll<HTMLElement>("[id]").forEach(e => {
249+
if (this.#AllEvents || ((e.id !== '') && (this.#bindsList.includes(e.id)))) {
250+
if (e.id && !e.dataset.webui_click_is_set) {
251+
e.dataset.webui_click_is_set = "true";
252+
e.addEventListener("click", () => this.#sendClick(e.id));
253+
}
259254
}
260255
});
261256
}
@@ -413,10 +408,11 @@ class WebuiBridge {
413408
if (bind.trim()) {
414409
const fn = bind;
415410
if (fn.trim()) {
416-
if (fn !== '_webui_core_api') {
417-
this[fn] = (...args: DataTypes[]) => this.call(fn, ...args);
411+
if (fn !== '__webui_core_api__') {
418412
if (typeof (window as any)[fn] === 'undefined') {
413+
this[fn] = (...args: DataTypes[]) => this.call(fn, ...args);
419414
(window as any)[fn] = (...args: string[]) => this.call(fn, ...args);
415+
if (this.#log) console.log(`WebUI -> Binding backend function [${fn}]`);
420416
}
421417
}
422418
}
@@ -753,8 +749,10 @@ class WebuiBridge {
753749
if (!this.#wsIsConnected()) return Promise.reject(new Error('WebSocket is not connected'));
754750

755751
// Check binding list
756-
if (!this.#AllEvents && !this.#bindsList.includes(`${fn}`))
757-
return Promise.reject(new ReferenceError(`No binding was found for "${fn}"`));
752+
if (fn !== '__webui_core_api__') {
753+
if (!this.#AllEvents && !this.#bindsList.includes(`${fn}`))
754+
return Promise.reject(new ReferenceError(`No binding was found for "${fn}"`));
755+
}
758756

759757
// Call backend and wait for response
760758
if (this.#log) console.log(`WebUI -> Calling [${fn}(...)]`);

0 commit comments

Comments
 (0)