From 699779df7e83ba3529415962bb4a76e0f51aada2 Mon Sep 17 00:00:00 2001 From: Noam Anisfeld <99100497+NoamAnisfeld@users.noreply.github.com> Date: Sun, 18 Feb 2024 16:19:40 +0200 Subject: [PATCH 1/2] resolve https://github.com/os-scar/overlay/issues/164 --- src/utils/utils.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utils/utils.js b/src/utils/utils.js index ea4a993..2cb02ef 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -1,26 +1,26 @@ /** * Waits for element to be rendered in DOM. * @param {string} selector A valid css selector string - * @param {Node} target A DOM Node (which may be an Element) within the DOM tree to watch for changes, or to be the root of a subtree of nodes to be watched. + * @param {Node} [target=document] A DOM Node (which may be an Element) within the DOM tree to watch for changes, or to be the root of a subtree of nodes to be watched. * */ import { SECOND } from '../globals'; const waitElementTimeOot = 10 * SECOND; -const waitForElement = (selector, target) => { +const waitForElement = (selector, target = document) => { return new Promise((resolve, reject) => { setTimeout(() => { reject(new Error('Time out to wait for the element ' + selector)); }, waitElementTimeOot); - if (document.querySelector(selector)) { - return resolve(document.querySelector(selector)); + if (target.querySelector(selector)) { + return resolve(target.querySelector(selector)); } const observer = new MutationObserver(() => { - if (document.querySelector(selector)) { - resolve(document.querySelector(selector)); + if (target.querySelector(selector)) { + resolve(target.querySelector(selector)); observer.disconnect(); } }); From 92ed71f819bc91c558d58d0e22a8279a225d716c Mon Sep 17 00:00:00 2001 From: Noam Anisfeld <99100497+NoamAnisfeld@users.noreply.github.com> Date: Tue, 20 Feb 2024 17:13:25 +0200 Subject: [PATCH 2/2] remove duplicate calls for querySelector --- src/utils/utils.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/utils/utils.js b/src/utils/utils.js index 2cb02ef..90dd0c2 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -14,13 +14,15 @@ const waitForElement = (selector, target = document) => { reject(new Error('Time out to wait for the element ' + selector)); }, waitElementTimeOot); - if (target.querySelector(selector)) { - return resolve(target.querySelector(selector)); + const wishedElement = target.querySelector(selector); + if (wishedElement) { + return resolve(wishedElement); } const observer = new MutationObserver(() => { - if (target.querySelector(selector)) { - resolve(target.querySelector(selector)); + const wishedElement = target.querySelector(selector); + if (wishedElement) { + resolve(wishedElement); observer.disconnect(); } });