Gazelle: Toggle Format Visibility

Hide formats with your discretion.

目前为 2024-12-07 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @id what-toggle-formats
  3. // @name Gazelle: Toggle Format Visibility
  4. // @namespace hateradio)))
  5. // @author hateradio
  6. // @version 4.1
  7. // @description Hide formats with your discretion.
  8. // @include https://redacted.sh/torrents.php*
  9. // @include https://redacted.sh/artist.php?id=*
  10. // @include https://redacted.sh/bookmarks.php*
  11. // @include https://redacted.sh/collages.php*
  12. // @include https://redacted.sh/top10.php*
  13.  
  14. // @include https://orpheus.network/torrents.php*
  15. // @include https://orpheus.network/artist.php?id=*
  16. // @include https://orpheus.network/bookmarks.php*
  17. // @include https://orpheus.network/collages.php*
  18. // @include https://orpheus.network/top10.php*
  19.  
  20.  
  21. // @grant none
  22. // @updated 07 DEC 2024
  23. // @since 28 OCT 2010
  24. // ==/UserScript==
  25.  
  26.  
  27.  
  28. (function () {
  29. 'use strict';
  30.  
  31. // P O L Y
  32. if (!Element.prototype.matches)
  33. Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
  34.  
  35. if (!Element.prototype.closest) {
  36. Element.prototype.closest = function (s) {
  37. let el = this;
  38. if (!document.documentElement.contains(el)) return null;
  39. do {
  40. if (el.matches(s)) return el;
  41. el = el.parentElement || el.parentNode;
  42. } while (el !== null && el.nodeType === 1);
  43. return null;
  44. };
  45. }
  46.  
  47.  
  48. // O N HELPER
  49. const on = (element, type, selector, listener) => {
  50. element.addEventListener(type, event => {
  51. const onTarget = event.target.closest(selector);
  52. if (onTarget) {
  53. event.onTarget = onTarget;
  54. listener(event);
  55. }
  56. }, false);
  57. };
  58.  
  59. // S T O R A G E HANDLE
  60. const strg = {
  61. on: (function () { try { let 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; } }()),
  62. read: function (key) { return this.on ? JSON.parse(localStorage.getItem(key)) : false; },
  63. save: function (key, dat) { return this.on ? !localStorage.setItem(key, JSON.stringify(dat)) : false; },
  64. wipe: function (key) { return this.on ? !localStorage.removeItem(key) : false; },
  65. zero: function (o) { for (let k in o) { if (o.hasOwnProperty(k)) { return false; } } return true; },
  66. grab: function (key, def) { const s = strg.read(key); return strg.zero(s) ? def : s; }
  67. };
  68.  
  69.  
  70. const hide = {
  71. loc: document.querySelector('.sidebar') || document.querySelector('.linkbox'),
  72.  
  73. typ: ['CD', 'Vinyl', 'WEB', 'SACD', 'DVD', 'DAT', 'Cassette', 'BD', 'Soundboard'],
  74. cod: ['FLAC', 'Ogg', 'AAC', 'AC3', 'DTS', 'MP3'],
  75. enc: ['192', 'APS', 'V2', 'V1', '256', 'APX', 'V0', '320', '/ Lossless', '24bit Lossless'],
  76. lch: ['Scene', 'Freeleech', 'Neutral Leech', 'Reported', 'Bad'],
  77.  
  78. hid: strg.read('togglesettings2') || [],
  79.  
  80. div: document.createElement('div'),
  81.  
  82. fra: document.createDocumentFragment(),
  83.  
  84. init: function () {
  85. const s = document.createElement('style'),
  86. css = '.hider-f { text-decoration: line-through } #format-hide { text-align: center; margin: 3px 0px } .gtfv__item { cursor: pointer }';
  87.  
  88. s.type = 'text/css';
  89. s.textContent = css;
  90. document.head.appendChild(s);
  91.  
  92. on(this.div, 'click', 'span', this.change.bind(this));
  93.  
  94. // run!
  95. this.location();
  96. this.generate();
  97. this.toggle(this.hid);
  98. this.toggle(this.hid, true);
  99. this.mark();
  100. },
  101. location: function () {
  102. this.div.id = 'format-hide';
  103. this.div.className = 'box pad box_artists';
  104. this.loc.parentNode.insertBefore(this.div, this.loc);
  105. },
  106. makeSpan: function (t) {
  107. const s = document.createElement('span');
  108. s.dataset.type = t;
  109. s.textContent = t.replace(/(?:\/|\\)/, '');
  110. s.id = 'gtfv__' + encodeURIComponent(t);
  111. s.className = 'gtfv__item';
  112. return s;
  113. },
  114. create: function (formats) {
  115. return formats.map(this.makeSpan).forEach(s => {
  116. this.fra.appendChild(s);
  117. this.fra.appendChild(document.createTextNode(' '));
  118. });
  119. },
  120. generate: function () {
  121. this.create(this.typ);
  122. this.fra.appendChild(document.createElement('br'));
  123. this.create(this.cod);
  124. this.fra.appendChild(document.createTextNode(' \u00D7 '));
  125. this.create(this.enc);
  126. this.fra.appendChild(document.createElement('br'));
  127. this.create(this.lch);
  128. this.div.appendChild(this.fra);
  129. },
  130. change: function (e) {
  131. const el = e.onTarget,
  132. type = el.dataset.type,
  133. idx = this.hid.indexOf(type),
  134. media = (this.typ.indexOf(type) !== -1);
  135.  
  136. el.classList.toggle('hider-f');
  137.  
  138. console.log('change', type, `isMedia: ${idx === -1}`);
  139. if (idx === -1) { // not found, add this type to the array of hidden formats
  140. this.hid.push(type);
  141. this.toggle(this.hid, media, false);
  142. } else { // found, remove this type
  143. this.hid.splice(idx, 1);
  144. this.toggle([type], media, true);
  145. }
  146.  
  147. console.log(this.hid);
  148. },
  149. createRegExp: function (formats, isMedia) {
  150. // console.log('createRegExp for', formats, isMedia);
  151. let reg = isMedia ? '\\b' : '';
  152. reg += '(?:' + formats.join('|') + ')\\b';
  153. reg = new RegExp(reg, 'i');
  154. return reg;
  155. },
  156. toggle: function (formats, isMedia, show) {
  157. console.debug('toggle', {formats, isMedia, show});
  158. const regex = formats.length > 0 ? this.createRegExp(formats, isMedia) : null;
  159.  
  160. let root = ''
  161.  
  162. if (regex) {
  163. strg.save('togglesettings2', this.hid);
  164.  
  165. if (processors.top10.enabled) {
  166. root = 'top10';
  167. } else if (isMedia) {
  168. root = 'media';
  169. } else {
  170. root = 'general';
  171. }
  172.  
  173. processors.$exec(root, regex, show);
  174. }
  175. },
  176. mark: function () {
  177. console.log('mark');
  178. Array.from(this.hid)
  179. .map(text => document.getElementById('gtfv__' + encodeURIComponent(text)))
  180. .forEach(el => el.classList.add('hider-f'));
  181. },
  182. click: function (el) {
  183. let evt;
  184. if (el.click) {
  185. el.click();
  186. } else {
  187. evt = document.createEvent('MouseEvents');
  188. evt.initEvent('click', true, true);
  189. el.dispatchEvent(evt);
  190. }
  191. }
  192. };
  193.  
  194.  
  195. const processors = {
  196. $exec: function (root, regex, show) {
  197. const action = show ? 'show' : 'hide';
  198.  
  199. const t = processors[root];
  200.  
  201. Array.from(t.elements())
  202. .filter(e => regex.test(e.textContent))
  203. .map(t.toElement)
  204. .forEach(t[action]);
  205. },
  206. general: {
  207. elements: () => (document.getElementById('discog_table') || document.querySelector('.torrent_table')).querySelectorAll('a[href^="torrents.php?id="],a[onclick]'),
  208. toElement: e => e.parentNode.parentNode,
  209. show: e => e.removeAttribute('style'),
  210. hide: e => e.setAttribute('style', 'display:none')
  211. },
  212. top10: {
  213. enabled: document.location.pathname === '/top10.php',
  214. elements: () => Array.from(document.querySelectorAll('.torrent_table .group_info strong')).map(e => e.nextSibling),
  215. toElement: e => e.parentElement.parentElement.parentElement
  216. },
  217. media: {
  218. elements: () => document.querySelectorAll('.edition_info > strong'),
  219. toElement: e => e.querySelector('a'),
  220. show: e => e.textContent !== '-' && hide.click(e),
  221. hide: e => e.textContent !== '+' && hide.click(e)
  222. }
  223. };
  224.  
  225. hide.init();
  226.  
  227. }());