PTH colour format links

Colour the links for torrents by format so they stand out

目前为 2017-09-25 提交的版本。查看 最新版本

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