Gazelle: Toggle Format Visibility

Hide formats with your discretion.

// ==UserScript==
// @id             what-toggle-formats
// @name           Gazelle: Toggle Format Visibility
// @namespace      hateradio)))
// @author         hateradio
// @version        3.6
// @description    Hide formats with your discretion.
// @include        https://redacted.ch/torrents.php*
// @include        https://redacted.ch/artist.php?id=*
// @include        https://redacted.ch/bookmarks.php*
// @include        https://redacted.ch/collages.php*

// @include        https://orpheus.network/torrents.php*
// @include        https://orpheus.network/artist.php?id=*
// @include        https://orpheus.network/bookmarks.php*
// @include        https://orpheus.network/collages.php*


// @grant          none
// @updated        15 MAR 2020
// @since          28 OCT 2010
// ==/UserScript==

(function () {
	'use strict';

	var strg, hide;

	// S T O R A G E HANDLE
	strg = {
		on: (function () { try { var a, b = localStorage, c = Math.random().toString(16).substr(2, 8); b.setItem(c, c); a = b.getItem(c); return a === c ? !b.removeItem(c) : false; } catch (e) { return false; } }()),
		read: function (key) { return this.on ? JSON.parse(localStorage.getItem(key)) : false; },
		save: function (key, dat) { return this.on ? !localStorage.setItem(key, JSON.stringify(dat)) : false; },
		wipe: function (key) { return this.on ? !localStorage.removeItem(key) : false; },
		zero: function (o) { var k; for (k in o) { if (o.hasOwnProperty(k)) { return false; } } return true; },
		grab: function (key, def) { var s = strg.read(key); return strg.zero(s) ? def : s; }
	};

	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]'),
		str: document.querySelectorAll('.edition_info > strong'),

		typ: ['CD', 'Vinyl', 'WEB', 'SACD', 'DVD', 'DAT', 'Cassette', 'BD', 'Soundboard'],
		cod: ['FLAC', 'Ogg', 'AAC', 'AC3', 'DTS', 'MP3'],
		enc: ['192', 'APS', 'V2', 'V1', '256', 'APX', 'V0', '320', '/ Lossless', '24bit Lossless'],
		lch: ['Scene', 'Freeleech', 'Neutral Leech', 'Reported', 'Bad'],

		hid: strg.read('togglesettings2') || [],

		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.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 pad box_artists';
			this.loc.parentNode.insertBefore(this.div, this.loc);
		},
		slink: function (t) {
			var s = document.createElement('span');
			s.data = t;
			s.textContent = t.replace(/(?:\/|\\)/, '');
			s.id = 'togformatvis_' + s.textContent.replace(/(?:\s)/, '');
			s.style.cursor = 'pointer';
			s.addEventListener('click', this.change.bind(this), false);
			s.setAttribute('onmousedown', 'return false;');
			this.div.appendChild(s);
			this.div.appendChild(document.createTextNode(' '));
		},
		create: function (a, b) {
			var x = -1, y = a.length;
			while (++x < y) {
				this.slink(a[x]);
			}
			switch (b) {
			case 1:
				this.div.appendChild(document.createElement('br'));
				break;
			case 2:
				this.div.appendChild(document.createTextNode(' \u00D7 '));
				break;
			default:
				break;
			}
		},
		generate: function () {
			this.create(this.typ, 1);
			this.create(this.cod, 2);
			this.create(this.enc, 2);
			this.create(this.lch);
		},
		change: function (e) {
			var el = e.target, idx = this.hid.indexOf(el.data), idz = (this.typ.indexOf(el.data) !== -1);
			el.className = el.className === 'hider-f' ? 'hider-o' : 'hider-f';

			// console.log(idx === -1);
			if (idx === -1) {
				this.hid.push(el.data);
				this.toggle(this.hid, idz, false);
			} else {
				this.hid.splice(idx, 1);
				this.toggle([el.data], idz, true);
			}

			// console.log(this.hid);
		},
		expression: function (a, isMedia) {
			var reg = '(?:' + a.join('|') + ')\\b';
			return isMedia ? '\\b' + reg : reg;
		},
		toggle: function (a, isMedia, show) {
			// console.log(a, isMedia, show);
			var p, q, r = a.length > 0 ? this.expression(a, isMedia) : false, elements = !isMedia ? this.anc : this.str;
			if (r) {
				strg.save('togglesettings2', this.hid);
				r = new RegExp(r, 'i');
				// console.log(r, elements);

				Array.from(elements).forEach(function (e) {
					if (r.test(e.textContent)) {
						if (isMedia) {
							p = e.querySelector('a');
							show ? (p.textContent === '-' ? false : hide.click(p)) : p.textContent === '+' ? false : hide.click(p);
						} else {
							show ? e.parentNode.parentNode.removeAttribute('style') : e.parentNode.parentNode.setAttribute('style', 'display:none');
						}
					}
				});
			}
		},
		mark: function () {
			console.log('mark');
			Array.from(this.hid).forEach(function (text) {
				var el = document.getElementById('togformatvis_' + text.replace(/(?:\/|\\|\s)/g, ''));
				el.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();

}());