SHARE
TWEET

IIchan catalogue search.user.js

a guest Mar 23rd, 2016 236 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         IIchan catalogue search
  3. // @namespace    localhost
  4. // @version      1.0
  5. // @description  trying to take over the world!
  6. // @author       Cirno
  7. // @match        http://iichan.hk/*/catalogue.html
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. /* jshint esnext:true */
  12. var $ = document.querySelector.bind(document);
  13. var $$ = document.querySelectorAll.bind(document);
  14.  
  15. function filterCatalog(event){
  16.     var query = event.target.value;
  17.  
  18.     var heading = '.theader';
  19.     var title = '.filetitle';
  20.     var snippet = '.cattext';
  21.  
  22.     var threads = $$('.catthreadlist a');
  23.     var count = threads.length;
  24.  
  25.     for(var thread of Array.from(threads)){
  26.         // reset
  27.         if(thread.style.display !== ''){
  28.             thread.style.display = '';
  29.         }
  30.  
  31.         // if nothing to nipah about
  32.         if(query === '' || query.match(/^\s+$/)){
  33.             continue;
  34.         }
  35.  
  36.         // but if they doesnt't fit...
  37.         var re = new RegExp(query, 'i');
  38.         if(
  39.             !(
  40.                 thread.title.match(re) || // This actually matches thread creation date
  41.                 ( (thread.querySelector(title) !== null) && thread.querySelector(title).textContent.match(re) ) ||
  42.                 ( (thread.querySelector(snippet) !== null) && thread.querySelector(snippet).textContent.match(re) )
  43.             )
  44.         ){
  45.             // ... hide 'em and reveal needed
  46.             thread.style.display = 'none';
  47.             count--;
  48.         }
  49.     }
  50.  
  51.     $(heading).innerHTML = $(heading).innerHTML.match(/\d/) ? $(heading).innerHTML.replace(/\d+/, count) : $(heading).innerHTML + ' <b>(' + count + ')</b>';
  52. }
  53.  
  54.  
  55. // ondomcontentloaded
  56.  
  57. // display search bar
  58. $('.theader').insertAdjacentHTML(
  59.     'beforebegin',
  60.     '<input type="text" title="Search" id="filterbox" placeholder="Start typing to search..." style="margin-left: 1em; width: 20%;">'
  61. );
  62. $('#filterbox').oninput = filterCatalog;
  63.  
  64.  
  65. // display date & time for each thread
  66. for(var thread of Array.from($$('.catthreadlist a'))){
  67.     var date = thread.title;
  68.  
  69.     thread.querySelector('br[clear]').insertAdjacentHTML(
  70.         'afterend',
  71.         '<span class="postertrip" style="background:cornsilk">[' + date.match(/(\d{2})\s(\W{3})(?:\W+)?\s(\d{4})/).slice(1,4).join('/') + ' ' + date.match(/..:.{5}/) + ']</span><br>'
  72.     );
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top