Stratbasher / Export PC list from Teamviewer

// ==UserScript==
// @name         Export PC list from Teamviewer
// @namespace    https://*teamviewer.com*
// @version      1.0
// @description  Adds an "Export" button in the upper-right corner of the table in the teamviewer portal to export a list of machines. Refresh if the button doesn't appear in your current view.
// @author       Stratbasher
// @match        https://login.teamviewer.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

function sleep(time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}

(function () {
  'use strict';

  var computers = [];

  sleep(3000).then(() => {
    $('div.upper-table-wrapper table.RGrid.upper-table thead.thead tr th').last().html('<button id="exportButton" class="ui-button" type="button"><span class="ConnectButtonTextDiv">Export</span></button>');

    $('#exportButton').on('click', function () {
      var continuePage = true;
      while (continuePage) {
        $('div#cc_grid.cac-r-grid div div.RGrid-wrapper div.lower-table-wrapper table.RGrid.lower-table tbody.tbody tr.tr td div.flex.flexRow').each(function () {
          console.log($(this).first().find('div.alias-container').first().html());
          computers.push($(this).first().find('div.alias-container').first().html());
        });

        var nextArrow = $('a.arrow-container').get(3);

        if ($(nextArrow).hasClass("disabled")) {
          continuePage = false;
        }
        else {
          nextArrow.click();
          sleep(300);
        }
      }

      //Computer names are gathered
      download('computers.txt', computers.join('\r\n'));
    });
  });

})();