HWM_Forums_Filter

Фильтрация нежелательных тем на форумах ГВД

  1. // ==UserScript==
  2. // @name HWM_Forums_Filter
  3. // @namespace Рианти
  4. // @description Фильтрация нежелательных тем на форумах ГВД
  5. // @homepage http://userscripts.org/scripts/show/486129
  6. // @version 1
  7. // @include http://www.heroeswm.ru/forum_thread.php*
  8. // ==/UserScript==
  9.  
  10. var _GM_arr_key = 'HWM_Forums_Filter_Array';
  11. var _GM_appearance_key = 'HWM_Forums_Filter_Appearance';
  12. var _controls_marker1 = 'Mm';
  13. var _controls_marker2 = 'Sc';
  14.  
  15. function appendOnTop(newEl) {
  16. document.body.appendChild(newEl);
  17. newEl.style.position = 'absolute';
  18. newEl.style.top = _appendTitleY + 'px';
  19. newEl.style.right = _appendTitleX + 'px';
  20. }
  21.  
  22. function applyBlocks(fTable, stance){
  23. var rows = fTable.children, tId, rowsHidden = 0, check;
  24. var blockedThreads = loadBlockedArr(), rowStyle = 0;
  25. for(var i = 1; i < rows.length; i++){
  26. tId = rows[i].getElementsByTagName('a')[0].href.match(/tid=(\d+)/)[1];
  27. check = checkThread(blockedThreads, tId);
  28. appendControls(rows[i], tId, check);
  29. if(stance) {
  30. if (check) {
  31. rows[i].style.display = 'none';
  32. rowsHidden++;
  33. }
  34. }
  35. else{
  36. rows[i].style.display = '';
  37. }
  38. if(!stance || !check){
  39. if(rowStyle = (rowStyle + 1) % 2) rows[i].className = '';
  40. else rows[i].className = 'second';
  41. }
  42. }
  43. return rowsHidden;
  44. }
  45.  
  46. function checkThread(blockedArr, tId){
  47. if(blockedArr.indexOf(tId) > -1) return 1;
  48. return 0;
  49. }
  50.  
  51. function loadBlockedArr(){
  52. return JSON.parse(GM_getValue(_GM_arr_key, '[]'));
  53. }
  54.  
  55. function saveBlockedArr(arr){
  56. GM_setValue(_GM_arr_key, JSON.stringify(arr));
  57. }
  58.  
  59. function addBlockedThread(tId){
  60. var blockedArr = loadBlockedArr();
  61. blockedArr.push(tId);
  62. saveBlockedArr(blockedArr);
  63. }
  64.  
  65. function removeBlockedThread(tId){
  66. var blockedArr = loadBlockedArr();
  67. delete blockedArr[blockedArr.indexOf(tId)];
  68. saveBlockedArr(blockedArr);
  69. }
  70.  
  71. function appendControls(row, tId, stance){
  72. var span = document.createElement('span');
  73. span.className = _controls_marker2;
  74. if(stance){
  75. span.innerHTML = '<i style="cursor: pointer" title="Показывать">(-)</i> ';
  76. span.onclick = function(){removeBlockedThread(tId); applyAppearance()};
  77. }
  78. else{
  79. span.innerHTML = '<i style="cursor: pointer" title="Скрывать">(+)</i> ';
  80. span.onclick = function(){addBlockedThread(tId); applyAppearance()};
  81. }
  82. if(row.firstChild.firstChild.className == _controls_marker2) {
  83. row.firstChild.replaceChild(span, row.firstChild.firstChild);
  84. } else {
  85. row.firstChild.insertBefore(span, row.firstChild.firstChild);
  86. }
  87. }
  88.  
  89. function enabledAppearance(){
  90. clearTitle();
  91. var threadsHidden = applyBlocks(_table, 1);
  92. var p = document.createElement('p');
  93. var small = document.createElement('small');
  94. p.innerHTML = 'Тем скрыто: ' + threadsHidden + ' ';
  95. p.id = _controls_marker1;
  96. small.innerHTML = '(показать)';
  97. small.style.cursor = 'pointer';
  98. small.onclick = function(){toggleAppearance(); disabledAppearance()};
  99. p.appendChild(small);
  100. appendOnTop(p);
  101. }
  102.  
  103. function disabledAppearance(){
  104. clearTitle();
  105. applyBlocks(_table, 0);
  106. var p = document.createElement('p');
  107. var small = document.createElement('small');
  108. p.innerHTML = 'Показ всех тем ';
  109. p.id = _controls_marker1;
  110. small.innerHTML = '(скрыть)';
  111. small.style.cursor = 'pointer';
  112. small.onclick = function(){toggleAppearance(); enabledAppearance()};
  113. p.appendChild(small);
  114. appendOnTop(p);
  115. }
  116.  
  117. function clearTitle(){
  118. var title = document.getElementById(_controls_marker1);
  119. if(title) document.body.removeChild(title);
  120. }
  121.  
  122. function toggleAppearance(){
  123. GM_setValue(_GM_appearance_key, (getAppearance() + 1) % 2);
  124. }
  125.  
  126. function getAppearance(){
  127. return parseInt(GM_getValue(_GM_appearance_key, 1));
  128. }
  129.  
  130. function applyAppearance(){
  131. if(getAppearance()) enabledAppearance();
  132. else disabledAppearance();
  133. }
  134.  
  135. var _table = document.querySelector('.table3 > tbody');
  136. var rCell = _table.querySelector('tr:nth-child(1) > th:nth-child(5)');
  137. var offsetY = 5;
  138. var _appendTitleX = _table.getBoundingClientRect().left;
  139. var _appendTitleY = _table.getBoundingClientRect().top - rCell.offsetHeight - offsetY;
  140.  
  141. if (!this.GM_getValue || (this.GM_getValue.toString && this.GM_getValue.toString().indexOf("not supported")>-1)) {
  142. this.GM_getValue = function (key, def) { return localStorage[key] || def; }
  143. this.GM_setValue = function (key, value) { return localStorage[key] = value; }
  144. this.GM_deleteValue = function (key) { return delete localStorage[key]; }
  145. }
  146.  
  147. applyAppearance();