Skip to content

Instantly share code, notes, and snippets.

@dzaima
Created December 19, 2018 16:28
Show Gist options
  • Save dzaima/896f678c19d354177dc36fb8fa2ee4c4 to your computer and use it in GitHub Desktop.
Save dzaima/896f678c19d354177dc36fb8fa2ee4c4 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name TIO fancy search & enter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description sort languages fancily
// @author dzaima
// @match https://tio.run/
// @grant none
// ==/UserScript==
window.addEventListener('load', function() {
'use strict';
var winningLanguage;
function myFilter(event) {
var search = $("#search").value.toLowerCase();
var categories = ["$."];
iterate($$("#categories input:checked"), function(element) {
categories.push(element.id);
});
var rCategories = RegExp(categories.join("|"));
var found = [];
iterate($$("#results div"), function(element) {
if (~element.title.toLowerCase().indexOf(search) && rCategories.test(element.dataset.categories)) {
element.classList.remove("hidden");
found.push(element);
} else
element.classList.add("hidden");
});
function pel(el) {
var str = el.title.toLowerCase();
return {len: str.replace(/ *\([^()]+\)/g, "").length, start: str.startsWith(search), str: str};
}
found.sort((a, b) => {
var ap = pel(a);
var bp = pel(b);
if (search.length != 0) {
if (ap.start && !bp.start) return -1;
if (bp.start && !ap.start) return 1;
var lendiff = ap.len - bp.len;
if (lendiff != 0 && (ap.len < 5 || bp.len < 5)) return lendiff;
}
return ap.str>bp.str;
});
winningLanguage = found[0];
iterate(found, function(c) {
c.parentNode.appendChild(c);
});
var count = $$("#results div:not(.hidden)").length;
var counter = $("#result-count");
counter.textContent = pluralization(count, "language");
counter.title = pluralization(count, "programming language");
}
$("#search").oninput = myFilter;
iterate($$("input[type=checkbox]", $("#categories")), function(element) {
element.onchange = myFilter;
});
$("#search").onkeypress = function(e) {
if (e.keyCode == 13) winningLanguage.click();
if ($("#search").oninput != myFilter) $("#search").oninput = myFilter;
}
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment