stfu

silence of the spam

  1. // ==UserScript==
  2. // @name stfu
  3. // @namespace 2
  4. // @description silence of the spam
  5. // @version 2.06
  6. // @include http://blogs.crikey.com.au/*
  7. // @include http://www.crikey.com.au/*
  8. // @include https://blogs.crikey.com.au/*
  9. // @include https://www.crikey.com.au/*
  10. // @require https://greasyfork.org/scripts/1884-gm-config/code/GM_config.js?version=4836
  11. // https://greasyfork.org/en/scripts/18683-stfu
  12. // ==/UserScript==
  13. // Ver 2.01
  14. // New major version to deal with New, souped-up Crikey
  15. // The first rule of stfu: stfu about stfu
  16. // Ver 2.02
  17. // Settings optionally at the bottom
  18. // Ver 2.03
  19. // Fixed issue with cccp v5.22
  20. // Ver 2.04
  21. // Fixed issues caused by Crikey updates
  22. // Ver 2.05
  23. // Removed debug code
  24. // Ver 2.06
  25. // Fixed https URLs in @include
  26. var scriptVer = '2.06';
  27. ////////////////////////////////////////////////////////////////////////////////
  28. /*jslint browser: true */
  29. /*global GM_config, GM_registerMenuCommand */
  30. ////////////////////////////////////////////////////////////////////////////////
  31. //don't run in iframes
  32. if (window.top !== window.self) {return;}
  33. ////////////////////////////////////////////////////////////////////////////////
  34. // Config settings dialog
  35. GM_config.storage = 'stfu';
  36. GM_config.init('stfu - Ver ' + scriptVer,
  37. {
  38. authorsFilter: {
  39. label: 'Author filter (use the format: \'bob1234|L. Ron Hubbard|Joseph Smith, Jr.\')',
  40. type: 'text',
  41. 'default': 'bob1234',
  42. size:50
  43. },
  44. wordsFilter: {
  45. label: 'Word/phrase filter (use the format: \'programmatic specificity|leadership rumblings\')',
  46. type: 'text',
  47. 'default': '',
  48. size:50
  49. },
  50. settingsBottom: {
  51. label: 'Place Settings bar at the bottom',
  52. type: 'checkbox',
  53. 'default': false
  54. }
  55. },
  56. {
  57. save: function() { location.reload(); } // reload the page when configuration was changed
  58. }
  59. );
  60. ////////////////////////////////////////////////////////////////////////////////
  61. function showConfigSTFU() {GM_config.open();}
  62. ////////////////////////////////////////////////////////////////////////////////
  63. ////////////////////////////////////////////////////////////////////////////////
  64. // Declare Global Hashtable
  65. var gh = [];
  66. ////////////////////////////////////////////////////////////////////////////////
  67. // Run if DOM is ready, otherwise add a listerner to wait
  68. if (document.readyState == "complete" || document.readyState == "loaded" || document.readyState == "interactive") {main();}
  69. else {window.addEventListener('DOMContentLoaded',function(e){main();});}
  70. ////////////////////////////////////////////////////////////////////////////////
  71. // Init Global Hash Variables
  72. function initGlobalHash() {
  73. // Filters
  74. gh["flt.authors"] = GM_config.get('authorsFilter').toLowerCase();
  75. gh["flt.words"] = GM_config.get('wordsFilter').toLowerCase();
  76. }
  77. ////////////////////////////////////////////////////////////////////////////////
  78.  
  79. function main() {
  80. console.log('stfu: main()');
  81. initGlobalHash();
  82. stfuNavBar();
  83. stfuFilters();
  84. }
  85. ////////////////////////////////////////////////////////////////////////////////
  86. // Create a new stfu_nav <div> with space for settings
  87. function stfuNavBar() {
  88. var comments = document.getElementById("comments");
  89. if (! comments) {return;}
  90.  
  91. var stfu_nav = document.createElement('div');
  92. stfu_nav.id = "stfu_nav";
  93. if (GM_config.get('settingsBottom')) {
  94. comments.appendChild(stfu_nav);
  95. } else {
  96. comments.insertBefore(stfu_nav,comments.firstChild);
  97. }
  98.  
  99. var nav = ["hlp","set","rec"];
  100. var alg = ["left","center","right"];
  101.  
  102. var tbl = document.createElement('table');
  103. stfu_nav.appendChild(tbl);
  104. var row = document.createElement('tr');
  105. tbl.appendChild(row);
  106.  
  107. for (var i = 0; i < nav.length; i++) {
  108. var td = document.createElement('td');
  109. td.width = '33%';
  110. row.appendChild(td);
  111. var dv = document.createElement('div');
  112. dv.id = 'stfu_' + nav[i];
  113. dv.style = "text-align: " + alg[i] + ";";
  114. td.appendChild(dv);
  115. }
  116. // Add the stfu Settings Link
  117. var set = document.createElement("a");
  118. document.getElementById("stfu_set").appendChild(set);
  119. set.innerHTML = "stfu Settings";
  120. set.addEventListener("click", showConfigSTFU, false);
  121. }
  122. ////////////////////////////////////////////////////////////////////////////////
  123. function toggleComment(){
  124. var elem = this.parentNode.nextSibling;
  125. if (elem.style.display == 'none') {
  126. elem.style.display = 'block';
  127. this.innerHTML = this.innerHTML.replace(/Show Comment/g,'Hide Comment');
  128. } else {
  129. elem.style.display = 'none';
  130. this.innerHTML = this.innerHTML.replace(/Hide Comment/g,'Show Comment');
  131. }
  132. }
  133. ////////////////////////////////////////////////////////////////////////////////
  134. function stfuFilters() {
  135. console.log('stfu: stfuFilters()');
  136. if ( document.getElementsByClassName("comment-list").length === 0) {return;}
  137. var afon = ( gh["flt.authors"] !== null && gh["flt.authors"].replace(/\s*/,'').length > 0 );
  138. var wfon = ( gh["flt.words" ] !== null && gh["flt.words" ].replace(/\s*/,'').length > 0 );
  139. if ( ! afon && ! wfon ) {return;}
  140. var af = '^' + gh["flt.authors"].replace(/\|/g,'$|^') + '$';
  141. var wf = gh["flt.words"];
  142.  
  143. var ol = document.getElementsByClassName("comment-list")[0];
  144. var cl = ol.getElementsByTagName("li");
  145. // console.log('stfu: cl.length = ' + cl.length);
  146. for ( var i = 0 ; i < cl.length; i++ ) {
  147. var author = getAuthor(cl[i].firstChild);
  148. // console.log('stfu: author = ' + author);
  149. if (afon && author.match(af)) {stfuFilter(cl[i], author, 'Author');}
  150. if ( ! wfon) {continue;}
  151. var text = cl[i].getElementsByClassName('comment-content')[0].innerHTML.toLowerCase();
  152. if (text.match(wf)) {stfuFilter(cl[i], author, 'Words');}
  153.  
  154. }
  155. }
  156. //----------------------------------------------------------------------------//
  157. function getAuthor(e) {
  158. var author;
  159. var a = e.parentNode.getElementsByClassName('fn')[0];
  160. if ( a.firstChild.innerHTML === undefined ) {
  161. author = a.innerHTML;
  162. } else {
  163. author = a.firstChild.innerHTML;
  164. }
  165. return author.toLowerCase();
  166. }
  167. //----------------------------------------------------------------------------//
  168. function stfuFilter(e, author, reason) {
  169. if ( e.style.display == 'none') {return;}
  170. e.style.display = 'none';
  171. var p = document.createElement('p');
  172. var a = document.createElement('a');
  173. a.addEventListener("click", toggleComment, true);
  174. p.appendChild(a);
  175. e.parentNode.insertBefore(p,e);
  176. a.innerHTML = author + ' - Show Comment (' + reason + ')';
  177. }