Skip to content

Commit

Permalink
Make watchForElement helper promise-only
Browse files Browse the repository at this point in the history
Signed-off-by: Bayu Satiyo <itsyuukunz@gmail.com>
  • Loading branch information
kiraio-moe committed Jan 2, 2024
1 parent 1578def commit 6684710
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/helpers/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,22 @@ export function awaitElement(elem, func) {
}

/**
* Watch for an element to appear on the page and then call a function immediately. This is used to prevent the script from running if the element doesn't exist. Improved version of waitForElement.
* Watch for an element to appear on the page. This is used to prevent the script from running if the element doesn't exist. Improved version of waitForElement.
* @param {string} e - The element to watch.
* @param {function} f - The function to call.
* @returns {Promise<any>} A promise that resolves with the element.
*/
export function watchForElement (selector, callback) {
export function watchForElement (selector) {
return new Promise(resolve => {
const element = document.querySelector(selector)
if (element) {
console.log(`[FastForward] Element found: ${element} | Selector: ${selector}`)
callback(element)
resolve(element)
return
}

const observer = new MutationObserver(() => {
Array.from(document.querySelectorAll(selector)).forEach(el => {
console.log(`[FastForward] Element found: ${el} | Selector: ${selector}`)
callback(el)
resolve(el)
observer.disconnect()
})
Expand Down

0 comments on commit 6684710

Please sign in to comment.