userscripts/erkul_column_filter.user.js

113 lines
3.2 KiB
JavaScript

// ==UserScript==
// @name Erkul Column Filter
// @namespace https://annabunch.es
// @version 0.2
// @description Selectively hide columns on erkul.games
// @copyright 2021, annabunches@gmail.com
// @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==
waitForKeyElements("tr.mat-header-row", makeColumnFilter);
waitForKeyElements("div.filter", makeRedundantButton);
function makeButton(text) {
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">' + text + '</span><span class="mat-button-ripple mat-ripple" matripple=""></span><span class="mat-button-focus-overlay"></span></button>');
return button;
}
function makeColumnFilter(jNode) {
let cols = $("th", jNode);
for (let i = 0; i < $(cols).length; i++) {
let button = makeButton("Hide");
$(cols[i]).append(button);
button.click(function() {
hideColumn(i);
});
}
}
function makeRedundantButton(jNode) {
let button = makeButton("Hide Redundant");
jNode.append(button);
button.click(function() {
let headers = $("tr.mat-header-row th");
// 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);
if (last == "") {
last = $(cols[i]).html();
} else if ($(cols[i]).html() != last) {
diff = true;
}
});
// hide that column.
if (diff === false) {
hideColumn(i);
}
}
});
console.log("Erkul Column Filter Plugin fully loaded.");
}
function getColumnIndex(name) {
let i;
let found = false;
let headers = $("tr.mat-header-row th");
for (i = 0; i < headers.length; i++) {
if ($(headers[i]).text() == name) {
found = true;
break;
}
}
if (!found) return -1;
return i;
}
function hideColumnByName(name) {
let i = getColumnIndex(name);
if (i == -1) return;
hideColumn(i);
}
function hideColumn(i) {
$($("tr.mat-header-row th")[i]).hide();
$.each($("tr.mat-row"), function(j, row) {
let cols = $("td", row);
$(cols[i]).hide();
});
}
function showColumnByName(name) {
let i = getColumnIndex(name);
if (i == -1) return;
showColumn(i);
}
function showColumn(i) {
$($("tr.mat-header-row th")[i]).show();
$.each($("tr.mat-row"), function(j, row) {
let cols = $("td", row);
$(cols[i]).show();
});
}