Simple Wait For Document Helper

A Simple helper library for wait document load

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/373524/639009/Simple%20Wait%20For%20Document%20Helper.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

/**
 * Simple Wait For Document Helper
 * by Jixun<https://jixun.moe/>
 */
var waitForDocument = (function () {
  var callbackQueue = []
  var ready = false

  function waitForDocument(callback) {
    if (ready) {
      requestAnimationFrame(callback)
      return
    }

    callbackQueue.push(callback)
  }

  function triggerCallbackQueue() {
    if (ready === true) {
      // Already processed
      return
    }

    ready = true

    // process callback queue
    callbackQueue.forEach(requestAnimationFrame)
    callbackQueue = null

    // remove those listeners
    document.removeEventListener('DOMContentLoaded', triggerCallbackQueue)
    document.removeEventListener('load', triggerCallbackQueue)
  }

  // listen for document loaded events
  document.addEventListener('DOMContentLoaded', triggerCallbackQueue)
  document.addEventListener('load', triggerCallbackQueue)

  // check if document is ready yet
  if (['interactive', 'complete'].indexOf(document.readyState) !== -1) {
    triggerCallbackQueue()
  }

  return waitForDocument
})()