function waitForSelector(cssSelector, options = {}) {
  const settings = {
    ...defaults,
    ...options,
  };

  return new Promise((resolve, reject) => {
    let timeElapsed = 0;

    const int = setInterval(() => {
      const elm = document.querySelector(cssSelector);
      if (elm) {
        clearInterval(int);
        resolve(elm);
      }

      timeElapsed += settings.delay;

      if (timeElapsed >= settings.timeout) {
        const msg = `waitForSelector did not match CSS selector '${cssSelector}' (${settings.timeout} ms)`;

        reject(msg);
      }
    }, settings.delay);
  });
}
Waits for specified CSS selector to be located in the DOM

@param {String} cssSelector CSS selector to query for
@param {Object} options optional parameters
@param {number} options.delay delay between checks
@param {number} options.timeout timeout in milliseconds
@returns {Promise} promise which resolves to first matching element