awaitFor

Waits until a condition is true, executing a callback function when the condition is met.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/467272/1235249/awaitFor.js

  1. // ==UserScript==
  2. // @name awaitFor
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description Waits until a condition is true, executing a callback function when the condition is met.
  6. // @author IgnaV
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. const awaitFor = (condition, callback, maxAttempts=null, awaitTime=500, maxAwaitTime=10000, ...params) => {
  11. maxAttempts ||= maxAwaitTime / awaitTime;
  12.  
  13. let attempts = 0
  14. const intervalId = setInterval(() => {
  15. const result = condition();
  16. attempts++;
  17. if (attempts >= maxAttempts || result) {
  18. clearInterval(intervalId);
  19. }
  20. if (result) {
  21. callback(result, ...params);
  22. }
  23. }, awaitTime);
  24. };