PTH colour format links

Colour the links for torrents by format so they stand out

  1. // ==UserScript==
  2. // @name PTH colour format links
  3. // @version 0.9
  4. // @description Colour the links for torrents by format so they stand out
  5. // @author Chameleon
  6. // @include http*://redacted.ch/*
  7. // @grant none
  8. // @namespace https://greasyfork.org/users/87476
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. doColours();
  15. })();
  16.  
  17. function doColours()
  18. {
  19. var colours = [{format:'FLAC', colour:'#1AC8D8'},
  20. {format:'FLAC / 24bit', colour:'#196FD8', source:'Web'},
  21. {format:'FLAC / 24bit', colour:'#930DCC', source:'Vinyl'},
  22. {format:'FLAC', colour:'#A9CC0E', source:'SACD'},
  23. {format:'/', colour:'#D88B19', source:'5.1 Surround'}, // '/' as the format should select all torrents
  24. {format:'/', colour:'#D88B19', source:'5.1 Audio'},
  25. {format:'/', colour:'#D88B19', source:'Surround'},
  26. //{format:'<format here>', colour:'<colour here>'},
  27. ];
  28.  
  29. var torrents = document.getElementsByClassName('group_torrent');
  30. if(torrents.length === 0)
  31. return;
  32. var edition;
  33. for(var k=0; k<torrents.length; k++)
  34. {
  35. var t=torrents[k];
  36. if(t.getAttribute('class').indexOf(' edition ') !== -1)
  37. {
  38. edition=t;
  39. continue;
  40. }
  41. else if(t.getAttribute('class').substr(-7) === "edition")
  42. {
  43. edition=t;
  44. continue;
  45. }
  46. else if(t.getAttribute('class').indexOf(' edition_') === -1)
  47. continue;
  48.  
  49. var a=t.getElementsByTagName('a');
  50. a=a[a.length-1];
  51. for(var i=0; i<colours.length; i++)
  52. {
  53. var c=colours[i];
  54. if(a.textContent.indexOf(c.format) !== -1)
  55. {
  56. if(c.source)
  57. {
  58. if(edition.textContent.toLowerCase().indexOf(c.source.toLowerCase()) === -1)
  59. continue;
  60. }
  61.  
  62. a.setAttribute('style', 'color: '+c.colour+'; text-shadow: 0px 0px 10px;');
  63. }
  64. }
  65. }
  66.  
  67. var box=document.createElement('div');
  68. var inb=document.getElementsByClassName('sidebar')[0].firstElementChild.nextElementSibling;
  69. if(window.localStorage.hideFilter==="true")
  70. box.setAttribute('style', 'display: none;');
  71. inb.parentNode.insertBefore(box, inb);
  72. box.setAttribute('class', 'box');
  73. var h=document.createElement('div');
  74. h.innerHTML='<strong>Torrent Filter</strong>';
  75. h.setAttribute('class', 'head');
  76. box.appendChild(h);
  77. var b=document.createElement('div');
  78. b.setAttribute('class', 'body');
  79. box.appendChild(b);
  80.  
  81. var format=window.localStorage.filterFormat;
  82. if(format)
  83. format=JSON.parse(format);
  84. else
  85. format=[];
  86. var input=document.createElement('input');
  87. input.value=format.join(', ');
  88. input.placeholder='FLAC, MP3 / V0, etc';
  89. input.addEventListener('keyup', update.bind(undefined, 'filterFormat', input));
  90. b.appendChild(input);
  91.  
  92. var source=window.localStorage.filterSource;
  93. if(source)
  94. source=JSON.parse(source);
  95. else
  96. source=[];
  97. var input=document.createElement('input');
  98. input.value=source.join(', ');
  99. input.placeholder='Web, Surround, etc';
  100. input.addEventListener('keyup', update.bind(undefined, 'filterSource', input));
  101. b.appendChild(input);
  102.  
  103. var stats=document.createElement('a');
  104. stats.id='filterStats';
  105. stats.href='javascript:void(0);';
  106. stats.innerHTML='0 torrents hidden';
  107. stats.addEventListener('click', toggleHidden);
  108. stats.setAttribute('style', 'display: block; text-align: center; margin-top: 5px; font-weight: bold;');
  109. b.appendChild(document.createElement('br'));
  110. b.appendChild(stats);
  111.  
  112. hide();
  113. }
  114.  
  115. function update(localStorage, input)
  116. {
  117. window.localStorage[localStorage]=JSON.stringify(input.value.split(', '));
  118. hide();
  119. }
  120.  
  121. function hide()
  122. {
  123. var count=0;
  124. var torrents = document.getElementsByClassName('group_torrent');
  125.  
  126. for(var i=0; i<torrents.length; i++)
  127. {
  128. var t=torrents[i];
  129. if(t.getAttribute('class').indexOf('hidden') === -1)
  130. {
  131. t.style.display='';
  132. }
  133. }
  134.  
  135. var filterSource=window.localStorage.filterSource;
  136. if(filterSource)
  137. {
  138. filterSource=JSON.parse(filterSource);
  139.  
  140. var edition;
  141. var hide=false;
  142. for(var k=0; k<torrents.length; k++)
  143. {
  144. var t=torrents[k];
  145. if(t.getAttribute('class').indexOf(' edition ') !== -1)
  146. {
  147. edition=t;
  148. hide=true;
  149. for(var i=0; i<filterSource.length; i++)
  150. {
  151. var f=filterSource[i];
  152. if(t.textContent.toLowerCase().indexOf(f.toLowerCase()) !== -1)
  153. {
  154. hide=false;
  155. }
  156. }
  157. if(hide)
  158. t.style.display='none';
  159. continue;
  160. }
  161. else if(t.getAttribute('class').indexOf(' edition_') === -1)
  162. continue;
  163. if(hide)
  164. {
  165. t.style.display='none';
  166. count++;
  167. }
  168. }
  169. }
  170.  
  171. var filterFormat=window.localStorage.filterFormat;
  172. if(filterFormat)
  173. {
  174. filterFormat=JSON.parse(filterFormat);
  175.  
  176. var edition;
  177. var editionCount=0;
  178. var editionHidden=0;
  179.  
  180. for(var k=0; k<torrents.length; k++)
  181. {
  182. var t=torrents[k];
  183. if(t.getAttribute('class').indexOf(' edition ') !== -1)
  184. {
  185. if(editionCount > 0 && editionCount === editionHidden && edition)
  186. edition.style.display='none';
  187. edition=t;
  188. editionCount=0;
  189. editionHidden=0;
  190. continue;
  191. }
  192. else if(t.getAttribute('class').indexOf(' edition_') === -1)
  193. continue;
  194.  
  195. editionCount++;
  196.  
  197. var a=t.getElementsByTagName('a');
  198. a=a[a.length-1];//[0].parentNode.parentNode.lastChild.previousElementSibling;
  199. var hide=true;
  200. for(var i=0; i<filterFormat.length; i++)
  201. {
  202. var f=filterFormat[i];
  203.  
  204. if(a.textContent.toLowerCase().indexOf(f.toLowerCase()) !== -1)
  205. {
  206. hide=false;
  207. }
  208. }
  209. if(hide)
  210. {
  211. t.style.display='none';
  212. editionHidden++;
  213. count++;
  214. }
  215. }
  216. if(editionCount > 0 && editionCount === editionHidden && edition)
  217. edition.style.display='none';
  218.  
  219. var stats=document.getElementById('filterStats');
  220. if(count > 0)
  221. stats.innerHTML='Show '+count+' hidden torrents';
  222. else
  223. stats.innerHTML='0 hidden torrents';
  224. }
  225. }
  226.  
  227. function toggleHidden()
  228. {
  229. var stats=document.getElementById('filterStats');
  230. if(stats.innerHTML.indexOf('Show') === 0)
  231. {
  232. stats.innerHTML='Hide torrents';
  233.  
  234. var torrents = document.getElementsByClassName('group_torrent');
  235.  
  236. for(var i=0; i<torrents.length; i++)
  237. {
  238. var t=torrents[i];
  239. if(t.getAttribute('class').indexOf('hidden') === -1)
  240. {
  241. t.style.display='';
  242. }
  243. }
  244. }
  245. else
  246. hide();
  247. }