<commit_before>/** @module playMacro */

import once           from 'lodash.once';
import {createLogger} from './Logger';
import {terminate}    from './terminate';

const log = createLogger('playMacro');

export {
  playMacro,
  registerObserver,
  unregisterObserver
};

let observers = [];
let clearSessionOnce = once(terminate);

/**
 * A wrapper for iMacros' iimPlayCode function.
 *
 * @param {string} macro iim macro to play.
 * @param {Object} opts Options map
 * @param {boolean} [opts.errorIgnore=false] Tells iMacros to ignore errors.
 *                                           The replay of macros continues even if
 *                                           one or more commands fail.
 * @param {boolean} [opts.timeoutStep=1] Timeout in seconds for command TAG to
 *                                       search for an element on a webpage before
 *                                       exiting with error.
 */
function playMacro(macro, opts) {
  let options     = opts || {};
  let errorIgnore = options.errorIgnore ? 'YES' : 'NO';
  let timeoutStep = options.timeoutStep || 1;

  macro = `
    SET !REPLAYSPEED FAST
    SET !TIMEOUT_PAGE 30
    SET !ERRORIGNORE ${errorIgnore}
    SET !TIMEOUT_STEP ${timeoutStep}
    ${macro}
  `;

  //log.debug('Macro: %s', macro);
  const code = iimPlayCode(macro);

  switch (code) {
    case 1:
      break;
    case -101:
      log.warn('Была нажата кнопка Stop.');
      clearSessionOnce();
      break;
    case -802:
      // Timeout error (failed to load web page)
      log.warn('Не удалось загрузить страницу в отведенное время. ' +
        'Останавливаем загрузку и идем дальше...');
      window.stop();
      break;
    case -910:
      log.error('Синтаксическая ошибка в исполняемом макросе: %s', macro);
      break;
    case -920:<commit_msg>Add `timeoutPage` parameter to playMacro function<commit_after>/** @module playMacro */

import once           from 'lodash.once';
import {createLogger} from './Logger';
import {terminate}    from './terminate';

const log = createLogger('playMacro');

export {
  playMacro,
  registerObserver,
  unregisterObserver
};

let observers = [];
let clearSessionOnce = once(terminate);

/**
 * A wrapper for iMacros' iimPlayCode function.
 *
 * @param {string} macro iim macro to play.
 * @param {Object} opts Options map
 * @param {boolean} [opts.errorIgnore=false] Tells iMacros to ignore errors.
 *                                           The replay of macros continues even if
 *                                           one or more commands fail.
 * @param {number} [opts.timeoutStep=1] Timeout in seconds for command TAG to
 *                                       search for an element on a webpage before
 *                                       exiting with error.
 * @param {number} [opts.timeoutPage=30] Timeout in seconds to wait for page to
 *                                       finish loading
 */
function playMacro(macro, opts) {
  const options     = opts || {};
  const errorIgnore = options.errorIgnore ? 'YES' : 'NO';
  const timeoutStep = options.timeoutStep || 1;
  const timeoutPage = options.timeoutPage || 30;

  macro = `
    SET !REPLAYSPEED FAST
    SET !TIMEOUT_PAGE ${timeoutPage}
    SET !ERRORIGNORE ${errorIgnore}
    SET !TIMEOUT_STEP ${timeoutStep}
    ${macro}
  `;

  //log.debug('Macro: %s', macro);
  const code = iimPlayCode(macro);

  switch (code) {
    case 1:
      break;
    case -101:
      log.warn('Была нажата кнопка Stop.');
      clearSessionOnce();
      break;
    case -802:
      // Timeout error (failed to load web page)
      log.warn('Не удалось загрузить страницу в отведенное время. ' +
        'Останавливаем загрузку и идем дальше...');
      window.stop();
      break;
    case -910:
      log.error('Синтаксическая ошибка в исполняемом макросе: %s', macro);
      break;
    case -920:
{'fraction_non_alphanumeric': 0.10791925465838509, 'fraction_numerical': 0.009316770186335404, 'mean_word_length': 2.2128013300083125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6152727', 'n_tokens_mistral': 1199, 'n_tokens_neox': 1164, 'n_words': 359}