Runy69 / eDom Donations to csv

// ==UserScript==
// @name            eDom Donations to csv
// @version         0.2.1
// @author          Runy69
// @match           www.edominations.com/*/profile/*
// @copyright       2020, Runy69 (https://openuserjs.org/users/Runy69)
// @updateURL       https://openuserjs.org/meta/Runy69/eDom_Donations_to_csv.meta.js
// @downloadURL     https://openuserjs.org/install/Runy69/eDom_Donations_to_csv.user.js
// @grant           none
// @license         GPL-3.0-or-later
// ==/UserScript==
/*jshint esversion: 6 */

(function () {
  'use strict';

  let $ = window.jQuery;
  if (!$) return;

  $(document).ready(function () {
    $('#tab-7 th').append('<div style="position: absolute; top: 40px; right: 20px;"><a id="export" class="btn btn-theme" style="font-size: 0.5em;" href="javascript:;"><span>Export</span></a></div>');

    $("#export").click(exportData);

    function exportData() {
      let rows = [];
      let day = parseInt(document.querySelector('#vs219-1').textContent.match(/\d+\,\d+/)[0].replace(',', ''));
      rows.push(['Direction', 'Citizen', 'Link', 'Amount', 'Item', 'Q', 'Time']);
      document.querySelectorAll('#tab-7 tbody tr').forEach(row => {
        let direction = row.querySelector('td:nth-child(2)').textContent.match(/^(\w+):/)[1].toUpperCase();
        let citizen = row.querySelector('td:nth-child(2) a').textContent;
        let link = row.querySelector('td:nth-child(2) a').href;
        let amount = parseInt(row.querySelector('td:nth-child(3) strong').textContent.replace(/\s/, ''));
        let item = row.querySelector('img[src^="/public/game/"]').src.match(/\/([^/]+)$/)[1].replace(/(-icon-s)?\.png/, '');
        let q = ' ';
        let time = row.querySelector('td:nth-child(2)').textContent.split(citizen)[1].trim();
        if (row.querySelector('td:nth-last-child(1) i')) q = parseInt(row.querySelector('td:nth-last-child(1) i').className.match(/\d+/)[0]);
        rows.push([direction, citizen, link, amount, item, q, time]);
      });

      let csvContent = "data:text/csv;charset=utf-8," + rows.map(e => e.join(";")).join("\n");
      let encodedUri = encodeURI(csvContent);
      let link = document.createElement("a");
      link.setAttribute("href", encodedUri);
      link.setAttribute("download", `eDom-donations-${day}.csv`);
      document.body.appendChild(link);
      link.click();
      link.remove();
    }
  });
})();