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