Thursday, September 22, 2011

What.CD: Toggle Format Visibility


// ==UserScript==
// @id             what-toggle-formats
// @name           What.CD: Toggle Format Visibility
// @namespace      hateradio)))
// @author         hateradio
// @version        2.5
// @description    Hide formats with your discretion.
// @updateURL      https://userscripts.org/scripts/source/105151.meta.js
// @include        http*://*what.cd/torrents.php*
// @include        http*://*what.cd/artist.php?id=*
// @include        http*://*what.cd/bookmarks.php*
// @include        http*://*what.cd/collages.php*

// @match          *://*.what.cd/torrents.php*
// @match          *://*.what.cd/artist.php?id=*
// @match          *://*.what.cd/bookmarks.php*
// @match          *://*.what.cd/collages.php?*

// @updated        22 SEP 2011
// @since          28 OCT 2010
// ==/UserScript==

// S T O R A G E HANDLE
var strg = {
 init:function(){ this.on = this.work(); },
 work:function(){ try { return 'localStorage' in window && window['localStorage'] !== null; } catch(e) { return false; } }, 
 read:function(key){ return this.on ? JSON.parse(window.localStorage.getItem(key)) : false; },
 save:function(key,dat){ return this.on ? !window.localStorage.setItem(key, JSON.stringify(dat)) : false; },
 wipe:function(key){ return this.on ? !window.localStorage.removeItem(key) : false; }
};
strg.init();

var hide = {
 loc:document.querySelector('.sidebar') || document.querySelector('.linkbox'),
 anc:(document.getElementById('discog_table') || document.querySelector('.torrent_table')).querySelectorAll('a[href^="torrents.php?id="],a[onclick]'), //main_column
 str:document.querySelectorAll('.edition_info > strong'),
 
 typ:['CD','Vinyl','WEB','Scene','SACD','DVD','Soundboard'],
 cod:['FLAC','Ogg','AAC','AC3','DTS','MP3'],
 enc:['V0','V2','192','320','Lossless','24bit Lossless'],
 lch:['Freeleech','Neutral Leech','Reported','Bad'],
 
 hid:strg.read('togglesettings') || [],
 
 div:document.createElement('div'),
 
 init:function(){
  var tog = this, s = document.createElement('style'), top = document.getElementsByTagName('head')[0],
  css = '.hider-f { text-decoration: line-through } #format-hide { text-align: center; margin: 3px 0px }';
  s.type = 'text/css'; s.textContent = css;  top.appendChild(s);
  // run!
  this.bond =  function(m,b){ var c = function(e){ if(b !== true && e){e = e.currentTarget;} m.call(tog,e); }; return c; };
  this.location();
  this.generate();
  this.toggle(this.hid);
  this.toggle(this.hid,true);
  this.mark();
 },
 location:function(){
  this.div.id = 'format-hide';
  this.div.className = 'box box_artists';
  this.loc.parentNode.insertBefore(this.div,this.loc);
 },
 slink:function(t,u){
  var S = document.createElement('span'), e;
  S.style.cursor = 'pointer';
  S.id = 'togformatvis_'+t.replace(' ','');
  S.appendChild(document.createTextNode(t));
  e = !u ? this.bond(this.change) : this.bond(this.changeb);
  S.addEventListener('click', e, false);
  this.div.appendChild(S);
  this.div.appendChild(document.createTextNode(' '));
 },
 generate:function(){
  var x;
  for(x=0;x<this.typ.length;x++){ this.slink(this.typ[x],true); }
  this.div.appendChild(document.createElement('br'));
  
  for(x=0;x<this.cod.length;x++){ this.slink(this.cod[x]); }
  this.div.appendChild(document.createTextNode(' \u00D7 '));
  
  for(x=0;x<this.enc.length;x++){ this.slink(this.enc[x]); }
  this.div.appendChild(document.createTextNode(' \u00D7 '));

  for(x=0;x<this.lch.length;x++){ this.slink(this.lch[x]); }
 },
 change:function(el,b) {
  var idx = this.hid.indexOf(el.textContent);
  el.className = el.className === 'hider-f' ? 'hider-o' : 'hider-f';
  if(idx === -1){
   this.hid.push(el.textContent);
   this.show = false;
   this.toggle(this.hid,b);
  } else {
   this.hid.splice(idx,1);
   this.show = true;
   this.toggle([el.textContent],b);
  }
 },
 changeb:function(el){
  this.change(el,true);
 },
 toggle:function(a,b){
  var p, q, r = a.length > 0 ? '\\b(?:'+a.join('|')+')\\b' : false, x = -1, y = !b ? this.anc : this.str, z = y.length;
  if(r){
   strg.save('togglesettings',this.hid);
   r = new RegExp(r,'i');
   while(++x<z){ q = y[x];
    if(r.test(q.textContent)){
     if(b){
      p = q.querySelector('a');
      this.show ? p.textContent === '-' ? false : this.click(p) : p.textContent === '+' ? false : this.click(p);
     }else{
      this.show ? q.parentNode.parentNode.removeAttribute('style') : q.parentNode.parentNode.setAttribute('style','display:none');
     }
    }
   }
  }
 },
 mark:function(){
  var x = -1, y = this.hid.length, z;
  while(++x<y){ z = this.hid[x];
   z = document.getElementById('togformatvis_'+z.replace(' ','')); z.className = 'hider-f';
  }
 },
 click:function(el){
  var evt;
  if(el.click){ el.click(); }
  else{ evt = document.createEvent('MouseEvents'); evt.initEvent('click', true, true); el.dispatchEvent(evt); }
 }
};

hide.init();

0 comments:

Post a Comment