viktorShmer / SaveReport

// ==UserScript==
// @name         SaveReport
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  try to take over the world!
// @author       viktorShmer
// @license      MIT
// @match        https://staff.megagroup.ru/staff/reglament/statistic/count/*
// @grant        none
// ==/UserScript==

(function () {
  'use strict';
  $(function () {
    var staffID = '';
    if (localStorage.getItem('staffID')) {
        staffID = localStorage.getItem('staffID');
    } else {
        staffID = prompt('Введите свой staff ID');
        localStorage.setItem('staffID', staffID);
    }
    var btn = $('<button class="save_report" style="background: red; color: white; font-size: 32px; position:fixed; bottom: 100px; right: 100px; z-index:9999;border: none;display: inline-block;line-height: 32px;border-radius: 4px;padding: 5px;">Сгенерировать отчёт</button>');
    $('body').append(btn);
    btn.on('click', function (event) {
      event.preventDefault();
      var str = '';
      $('table.count.table tbody > tr [data-agent-card-id="'+staffID+'"]').closest('tr').find('> td:nth-last-child(5) .staff-dropdown-show').trigger('click');
      setTimeout(function () {
        $('.staff-dropdown.dropdown-id-1.autohide .avid-scroll.with-avid-event > table > tbody > tr').each(function (index, elem) {
          str += 'https://staff.megagroup.ru' + $(elem).find(' > td.min > a').attr('href') + '\r\n';
          if ($('#rep').length === 0) {
            var link = $('<a id="rep" style="background: red; color: white; font-size: 32px; position:fixed; bottom: 150px; right: 100px; z-index:9999;text-decoration: none;display: inline-block;line-height: 32px;border-radius: 4px;padding: 5px;">Сохранить отчёт</a>');
            $('body').append(link);
          }
          download(str, 'report.txt', 'text/plain');
        })
      }, 500);
    });

    function download(text, name, type) {
      var a = document.getElementById("rep");
      var file = new Blob([text], {
        type: type
      });
      a.href = URL.createObjectURL(file);
      a.download = name;
    }
  });
})();