Skip to content

Commit

Permalink
resolve os-scar#164
Browse files Browse the repository at this point in the history
  • Loading branch information
NoamAnisfeld authored Feb 18, 2024
1 parent b050730 commit 699779d
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -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();
}
});
Expand Down

0 comments on commit 699779d

Please sign in to comment.