Patch When Available Library

Calls a function (getExpectedFnc()) repeatedly until it gives an expected result (confirmIsAvailableFnc()). Forwards it to (doPatchFnc()).

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/428034/941202/Patch%20When%20Available%20Library.js

  1. // ==UserScript==
  2. // @name Patch When Available Library
  3. // @namespace hoehleg.userscripts.private
  4. // @version 0.1
  5. // @description Calls a function (getExpectedFnc()) repeatedly until it gives an expected result (confirmIsAvailableFnc()). Forwards it to (doPatchFnc()).
  6. // @author Gerrit Höhle
  7. // @grant none
  8. // ==/UserScript==
  9. /* jslint esnext: true */
  10. const patchWhenAvailable = ({ getExpectedFnc, doPatchFnc, confirmIsAvailableFnc = null, timeOutRetryMillis = 200, maxPeriodTryMillis = 5000 }) => {
  11. const valueOrObject = getExpectedFnc();
  12. const isAvailable = confirmIsAvailableFnc ? confirmIsAvailableFnc(valueOrObject) : !!valueOrObject;
  13. if (!isAvailable) {
  14. if (timeOutRetryMillis <= maxPeriodTryMillis) {
  15.  
  16. setTimeout(() => {
  17. maxPeriodTryMillis -= timeOutRetryMillis;
  18. patchWhenAvailable({ getExpectedFnc, doPatchFnc, confirmIsAvailableFnc, timeOutRetryMillis, maxPeriodTryMillis });
  19.  
  20. }, timeOutRetryMillis);
  21. }
  22.  
  23. return;
  24. }
  25.  
  26. doPatchFnc(valueOrObject);
  27. };