www

My personal website(s)
Log | Files | Refs

addNginxFancyIndexForm.js (1183B)


      1 // addNginxFancyIndexForm.js
      2 // Add a small form to filter through the output of Nginx FancyIndex page
      3 // © 2017, Lilian Besson (Naereen) and contributors,
      4 // open-sourced under the MIT License, https://lbesson.mit-license.org/
      5 // hosted on GitHub, https://GitHub.com/Naereen/Nginx-Fancyindex-Theme
      6 var form = document.createElement('form');
      7 var input = document.createElement('input');
      8 
      9 input.name = 'filter';
     10 input.id = 'search';
     11 input.placeholder = 'Type to search...';
     12 
     13 form.appendChild(input);
     14 
     15 document.querySelector('h1').after(form);
     16 
     17 var listItems = [].slice.call(document.querySelectorAll('#list tbody tr'));
     18 
     19 input.addEventListener('keyup', function () {
     20     var i,
     21     // Word sequence _matching_ to input. All, except last, words must be _complete_.
     22     e = "(^|.*[^\\pL])" + this.value.trim().split(/\s+/).join("([^\\pL]|[^\\pL].*[^\\pL])") + ".*$",
     23     n = RegExp(e, "i");
     24     listItems.forEach(function(item) {
     25         item.removeAttribute('hidden');
     26     });
     27     listItems.filter(function(item) {
     28         i = item.querySelector('td').textContent.replace(/\s+/g, " ");
     29         return !n.test(i);
     30     }).forEach(function(item) {
     31   	    item.hidden = true;
     32     });
     33 });