Reddit Robin Filter

Filters comments. Works on usernames and messages.

当前为 2016-04-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit Robin Filter
  3. // @namespace http://kmcgurty.com
  4. // @version 2
  5. // @description Filters comments. Works on usernames and messages.
  6. // @author Kmc - admin@kmcdeals.com
  7. // @match https://www.reddit.com/robin/
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @grant GM_deleteValue
  11. // @grant GM_addStyle
  12. // ==/UserScript==
  13.  
  14. //GM_deleteValue("filter_list");
  15.  
  16. //will click the grow button and type /count every 15 seconds (to prevent abandoning)
  17. setInterval(function() {
  18. //$(".robin-chat--vote-increase").click();
  19. //$(".text-counter-input")[0].value = "/count";
  20. //$("input[type=submit]").click();
  21. }, 15000);
  22.  
  23. //setup the default filter
  24. var defaultFilter = ["voted to grow", "voted to stay", "voted to abandon"];
  25. if (GM_getValue("filter_list") === undefined || typeof GM_getValue("filter_list") !== "object") GM_setValue("filter_list", defaultFilter);
  26.  
  27. //filtery things
  28. function doFilter(div) {
  29. var filter = GM_getValue("filter_list");
  30.  
  31. for (var i = 0; i < filter.length; i++) {
  32. var messageDiv = $(div).find(".robin-message--message");
  33. var usernameDiv = messageDiv.parent().find(".robin--username");
  34.  
  35. //messageDiv[0] is occasionally undefined?
  36. if (messageDiv[0] !== undefined || usernameDiv[0] !== undefined) {
  37. var message = messageDiv[0].innerHTML.toLowerCase()
  38. var username = usernameDiv[0].innerHTML.toLowerCase();
  39. var currentFilter = filter[i].toLowerCase();
  40. var matches = message.includes(currentFilter) || username.includes(currentFilter);
  41. var isAscii = $("#remove-ascii")[0].checked && isAsciiArt(message);
  42.  
  43. if (matches || isAscii) {
  44. messageDiv.replaceWith(`
  45. <span class='deleted'>
  46. <a href="#" data-deleted="` + messageDiv[0].innerHTML + `">&#x3c;deleted&#x3e;</a>
  47. </span>
  48. `);
  49. }
  50. }
  51. }
  52. }
  53.  
  54. //checks for a new message, and then calls doFilter();
  55. createObserver();
  56. function createObserver() {
  57. var target = $('#robinChatMessageList')[0];
  58.  
  59. var observer = new MutationObserver(function(mutations) {
  60. mutations.forEach(function(mutation) {
  61. var messageDiv = mutation.addedNodes[0];
  62. doFilter(messageDiv);
  63. });
  64. });
  65.  
  66. // configuration of the observer:
  67. var config = { childList: true };
  68.  
  69. // pass in the target node, as well as the observer options
  70. observer.observe(target, config);
  71. }
  72.  
  73. //ty to fam
  74. //http://stackoverflow.com/questions/8746882/jquery-contains-selector-uppercase-and-lower-case-issue
  75. //makes :contains case-insensitive
  76. jQuery.expr[':'].Contains = function(a, i, m) { return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0; };
  77.  
  78. //also thanks to this guy
  79. //http://stackoverflow.com/questions/147824/how-to-find-whether-a-particular-string-has-unicode-characters-esp-double-byte
  80. function isAsciiArt(s) { // "art"
  81. return /[^\u0000-\u00ff]/.test(s);
  82. }
  83.  
  84. createEventListeners();
  85. function createEventListeners(){
  86. $(document.body).on("click", ".filter-img#filter-remove", function(event){
  87. var filterList = GM_getValue("filter_list");
  88. var word = event.currentTarget.getAttribute("data-word");
  89. var index = filterList.indexOf(word);
  90.  
  91. filterList.splice(index, 1);
  92. GM_setValue("filter_list", filterList);
  93.  
  94. event.currentTarget.parentElement.outerHTML = "";
  95. });
  96.  
  97. $(document.body).on("click", ".filter-img#filter-create", function(){
  98. var newWord = window.prompt("Enter a new filter to add");
  99. appendWordToList(newWord);
  100.  
  101. var newFilterList = GM_getValue("filter_list");
  102. newFilterList.push(newWord);
  103. GM_setValue("filter_list", newFilterList);
  104. });
  105. $(document.body).on("click", ".deleted a", function(event){
  106. event.preventDefault();
  107. var element = event.toElement;
  108.  
  109. element.outerHTML = $(element).attr("data-deleted");
  110. });
  111. }
  112.  
  113. //everything below is html/css stuff
  114.  
  115. createHTML();
  116. function createHTML(){
  117. var html = `
  118. <div>
  119. <div>Create word or username filters below by clicking the green +. Message /u/kmcgurty1 for help.</div>
  120. <input id="remove-ascii" type="checkbox" checked>Remove ascii art
  121. <div class="filter-list">
  122. <div class="item-wrapper">
  123. <div class="filter-img" id="filter-create"></div>
  124. </div>
  125. </div>
  126. </div>
  127. `;
  128.  
  129. $("div[role=main]").after(html);
  130.  
  131. var filterList = GM_getValue("filter_list");
  132.  
  133. for (var i = 0; i < filterList.length; i++) {
  134. appendWordToList(filterList[i]);
  135. }
  136.  
  137. createButton = ``;
  138. }
  139.  
  140. function appendWordToList(word){
  141. var html = `
  142. <div class="item-wrapper">
  143. <div class="filter-word">` + word + `</div>
  144. <div class="filter-img" id="filter-remove" data-word="` + word + `" ></div>
  145. </div>
  146. `;
  147.  
  148. $(html).insertBefore(".item-wrapper:last");
  149. }
  150.  
  151. var css = `
  152. .deleted a{
  153. color: rgba(0,0,0, .4);
  154. }
  155.  
  156. #remove-ascii{
  157. display: inline-block;
  158. vertical-align: bottom;
  159. }
  160.  
  161. .deleted:hover{
  162. text-decoration: underline;
  163. }
  164.  
  165. .filter-word{
  166. color: black;
  167. height: 25px;
  168. padding-right: 10px;
  169. }
  170.  
  171. .filter-img{
  172. cursor: pointer;
  173. width: 15px;
  174. height: 15px;
  175. background-size: contain;
  176. background-repeat: no-repeat;
  177. background-position: center;
  178. }
  179.  
  180. .filter-img#filter-create{
  181. height: 25px;
  182. width: 25px;
  183. background-image: url(https://i.imgur.com/nw1I62o.png);
  184. }
  185.  
  186. .filter-img#filter-remove{
  187. background-image: url(https://i.imgur.com/3bYSOxq.png);
  188. }
  189.  
  190. .item-wrapper{
  191. display: inline-block;
  192. background-color: rgb(235, 235, 235);
  193. border: 4px solid rgba(20, 20, 20, .7);
  194. border-radius: 5px;
  195. padding: 2px;
  196. margin: 4px;
  197. }
  198.  
  199. .item-wrapper div{
  200. vertical-align: middle;
  201. display: table-cell;
  202. }
  203.  
  204. .filter-list{
  205. font: normal small verdana,arial,helvetica,sans-serif;
  206. line-height: 1em;
  207. margin: 5px;
  208. }
  209. `;
  210.  
  211. GM_addStyle(css);