Skip to content

Commit

Permalink
chore: query selector on target element (#165)
Browse files Browse the repository at this point in the history
Fix #164

---------

Co-authored-by: Baruch Odem (Rothkoff) <[email protected]>
  • Loading branch information
NoamAnisfeld and baruchiro authored Feb 21, 2024
1 parent bf9c582 commit 218acc9
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
/**
* 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));
const wishedElement = target.querySelector(selector);
if (wishedElement) {
return resolve(wishedElement);
}

const observer = new MutationObserver(() => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
const wishedElement = target.querySelector(selector);
if (wishedElement) {
resolve(wishedElement);
observer.disconnect();
}
});
Expand Down

0 comments on commit 218acc9

Please sign in to comment.