Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: query selector on target element #165

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading