Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mysql-like filter box for instances.mastodon.xyz
// ==UserScript==
// @name xyz instance filter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Adds a filtering box to the instance list.
// Syntax is like JS or mysql, columns are:
// name,up,down,score,version,users,toots,connections,open,closed,uptime
//
// @author You
// @match https://instances.mastodon.xyz/list
// @grant none
// ==/UserScript==
function evalfilter(_cells, _qs) {
let name = _cells[2].innerHTML.trim();
let up = _cells[0].innerHTML.trim() == 'UP';
let down = !up;
let score = _cells[1].dataset.value;
let version = _cells[3].innerHTML.trim();
let users = _cells[4].innerHTML.trim();
let toots = _cells[5].innerHTML.trim();
let connections = _cells[6].innerHTML.trim();
let open = _cells[7].innerHTML.trim() == 'Yes';
let closed = !open;
let uptime = _cells[8].dataset.value;
return eval(_qs);
}
function submitFilter(e) {
e.preventDefault();
let qs = $('#query').val();
let rows = $('.table tbody tr');
qs = qs.toLowerCase()
.replace(/\band\b/g, '&&')
.replace(/\bor\b/g, '||')
.replace(/=/g, '==')
.replace(/===/g, '==')
.replace(/>==/g, '>=')
.replace(/<==/g, '<=')
.replace(/\bnot\b/g, '!');
rows.each(function() {
let cells = $(this).find('td');
$(this).toggleClass('hidden', !evalfilter(cells, qs));
});
return false;
}
$(`<style>
#query {
width: 800px;
}
.hidden {
display: none;
}
</style>`).appendTo($('head'));
$('<form id="filter"><input id="query"><input type="submit" id="apply-q" value="Apply!"></form>').insertAfter($('#fordevs'));
$('#filter').on('submit', submitFilter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment