warthunder.com / squadron members to CSV

Adds a button to WarThunder clan info pages allowing to export members' stats as CSV file.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        warthunder.com / squadron members to CSV
// @namespace   mimimimimi userscripts
// @match       https://warthunder.com/*/community/claninfo/*
// @match       https://warthunder.ru/ru/community/claninfo/*
// @grant       none
// @version     1.3.1
// @author      M
// @license MIT
// @description Adds a button to WarThunder clan info pages allowing to export members' stats as CSV file.
// ==/UserScript==

(function () {
    'use strict';
    let table, members, title, stat, activity;
    try {
        table = document.querySelector('div.squadrons-members__table');
        members = table.querySelectorAll('div.squadrons-members__grid-item');
        title = document.querySelector('div.squadrons-info__title').innerHTML.trim();
        stat = document.querySelector('div.squadrons-counter__count-icon--stat').nextElementSibling.innerHTML.trim();
        activity = document.querySelector('div.squadrons-counter__count-icon--activity').nextElementSibling.innerHTML.trim();
    }
    catch (err) {
        console.log("WT2CSV " + err);
        return;
    }
    let clicky = document.createElement('div');
    table.before(clicky);
    clicky.outerHTML = '<div id="clicky" class="button">Save as CSV</div>';
    document.getElementById('clicky').addEventListener('click', saveCSV);

    function saveCSV() {
        let now = new Date(),
            fname = "WT_" + title + "_(" + stat + "_" + activity + ")_members_" + now.toISOString().slice(0, 10) + ".csv";
        download(buildCSV(), fname);
        document.getElementById('clicky').style.display = 'none';
    }
    function buildCSV() {
        let dump = "", sep = ";", col = 6;
        members.forEach(function (el, i) {
            let m = el.textContent.trim();
            if ((i + 1) % col) {
                dump += m + sep;
            } else {
                dump += m + "\r\n";
            }
        });
        return dump;
    }
    function download(data, filename) {
        let a = window.document.createElement('a'),
            url = window.URL.createObjectURL(new Blob([data], {
                type: 'text/csv'
            }));
        a.href = url;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);
    }
})();