// ==UserScript==
// @name Erkul Redundancy Filter
// @namespace https://annabunch.es
// @version 0.1
// @description Hide identical columns on erkul.games
// @copyright 2021, annabunches
// @include https://www.erkul.games/*
// @require https://code.jquery.com/jquery-latest.js
// @require https://gist.githubusercontent.com/BrockA/2625891/raw/9c97aa67ff9c5d56be34a55ad6c18a314e5eb548/waitForKeyElements.js
// @grant none
// ==/UserScript==

console.log("Erkul Redundancy Filter pre-loading.");

waitForKeyElements("div.filter", loadWidgets);

function makeButton() {
    let button = $(document.createElement("div"));
    button.html('<button _ngcontent-dae-c11="" class="mat-focus-indicator mat-stroked-button mat-button-base mat-primary" color="primary" mat-stroked-button=""><span class="mat-button-wrapper">Hide Redundant</span><span class="mat-button-ripple mat-ripple" matripple=""></span><span class="mat-button-focus-overlay"></span></button>');
    return button;
}

function loadWidgets(jNode) {
    console.log("Erkul Redundancy Filter loading.");
    let button = makeButton();
    $("div.filter").append(button);
    button.click(function() {
        // get all the rows
        let rows = $("tr.mat-row");
        let nCols = $("td", rows[0]).length;

        // for each column...
        for (let i = 0; i < nCols; i++) {
            let last = "";
            let diff = false;

            // ... iterate over every row, checking whether that column is different
            // on any row. If it is the same everywhere...
            $.each(rows, function(j, row){
                let cols = $("td", row);
                console.log("Last: " + last);
                console.log("This: " + $(cols[i]).html());
                if (last == "") {
                    last = $(cols[i]).html();
                } else if ($(cols[i]).html() != last) {
                    diff = true;
                }
            });

            // hide that column.
            if (diff === false) {
                $.each(rows, function(j, row) {
                    let cols = $("td", row);
                    $(cols[i]).css("display", "none");
                });

                // and your little header, too
                let headers = $("tr.mat-header-row th");
                $(headers[i]).css("display", "none");
            }
        }
    });

    console.log("Erkul Redundancy Filter fully loaded.");
}

console.log("Erkul Redundancy Filter pre-loaded.");