Wait For Elements

given a selector waits for elements to be inserted into the DOM and executes a callback for each match

As of 2014-10-13. See the latest version.

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.greasyfork.org/scripts/5679/21043/Wait%20For%20Elements.js

/**
* @param sel - the selector you want to wait for
* @param action - the callback that will be executed when element/s matching the given selector are found, it is passed the array of found elements
* @param stopLooking - if true the function will stop looking for more elements after the first match
*/
function waitForElems(sel, action, stopLooking) {
  var id = 'fke' + Math.floor(Math.random() * 12345);
  function findElem(sel) {
    var found = [].filter.call(document.querySelectorAll(sel), function(elem) {
      return elem.dataset[id] !== 'y';
    });
    if(found.length > 0) {
      found.forEach(function(elem) {
        elem.dataset[id] = 'y';
        action(elem);
      });
      if(stopLooking) {
        clearInterval(tick);
      }
    }
  }
  var tick = setInterval(findElem.bind(null, sel), 300);
  findElem(sel);
}