Skip to content

Instantly share code, notes, and snippets.

@RossRogers
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RossRogers/b8f82302842b2ad72eec to your computer and use it in GitHub Desktop.
Save RossRogers/b8f82302842b2ad72eec to your computer and use it in GitHub Desktop.
// ==UserScript==
// @match http://ankiweb.net/*
// @match https://ankiweb.net/*
// ==/UserScript==
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
// the guts of this userscript
function main() {
// Note, jQ replaces $ to avoid conflicts.
$(document).ready(function(){setTimeout(function(){
$('#list').prepend(
'<div class="form-group">'+
'<label class="control-label" for="list_filter">Filter</label>&nbsp;'+
'<input name="list_filter" id="list_filter"></input>'+
'</div>')
var input_counter = 0
$('#list_filter').on('input',function() {
var my_input_event = ++input_counter
setTimeout(function(){
if (my_input_event === input_counter) {
var input_toks = $('#list_filter').val().replace(/(^\s+|\s+$)/,'').split()
$('#list tr').each(function(idx,tr) {
if (idx === 0) { // first result is header
return
}
for (var i=0; i < input_toks.length; ++i) {
if (input_toks[i] && !RegExp(input_toks[i],'i').test(tr.innerText)) {
tr.style.display = 'none'
return
}
}
tr.style.display = 'table-row'
})
}
},300)
})
},500)})
}
// load jQuery and execute the main function
addJQuery(main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment