Twitch OfficialParagonBot filter

hides all chat except that of the OfficialParagonBot

  1. // ==UserScript==
  2. // @name Twitch OfficialParagonBot filter
  3. // @description hides all chat except that of the OfficialParagonBot
  4. // @author Trylobot
  5. // @namespace twitch
  6. // @include /^https?://www\.twitch\.tv/[a-zA-Z0-9_]+$/
  7. // @version 1.0.1
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. window.onload = function() {
  12. /* helper functions */
  13. var uniq = function(str) {
  14. var seen = {};
  15. return str.filter(function(item) {
  16. return seen.hasOwnProperty(item) ? false : (seen[item] = true);
  17. });
  18. };
  19. /* objects */
  20. /* Bucket */
  21. var Bucket = function(size) {
  22. this.maxSize = size;
  23. this.currentSize = 0;
  24. this.array = [];
  25. };
  26. Bucket.prototype.add = function(elem) {
  27. if (this.currentSize < this.maxSize) {
  28. this.array.push(elem);
  29. this.currentSize++;
  30. return;
  31. }
  32. this.array.shift();
  33. this.array.push(elem);
  34. };
  35. Bucket.prototype.contains = function(elem) {
  36. return this.array.indexOf(elem) != -1;
  37. };
  38. /* * Message */
  39. var Message = function(object) {
  40. this.object = object;
  41. this.parse(object);
  42. };
  43. Message.prototype.parse = function(object) {
  44. this.from = $(".from", object).text();
  45. this.content = $(".message", object).text().trim();
  46. this.emoticons = this.parseEmoticons($(".emoticon", object));
  47. this.badges = this.parseBadges($(".badge", object));
  48. };
  49. Message.prototype.parseEmoticons = function(object) {
  50. var parsed = [];
  51. object.each(function() {
  52. parsed.push($(this).attr("alt"));
  53. });
  54. return parsed;
  55. };
  56. Message.prototype.parseBadges = function(object) {
  57. var parsed = [];
  58. object.each(function() {
  59. parsed.push($(this).attr("class").split(" ").slice(3)[0]);
  60. });
  61. return parsed;
  62. };
  63. Message.prototype.show = function() {
  64. $(this.object).addClass("filtered");
  65. return $(this.object).show();
  66. };
  67. Message.prototype.remove = function() {
  68. return $(this.object).remove();
  69. };
  70. /* * MessageHandler */
  71. var MessageHandler = function() {
  72. this.filtered = 0;
  73. this.kept = 0;
  74. this.filteredBox = null;
  75. this.filterBoxActive = false;
  76. this.whiteHandlers = []; // the message is shown if one hadnler returns true
  77. this.blackHandlers = []; // the message is filtered if one handler returns false
  78. };
  79. MessageHandler.prototype.init = function() {
  80.  
  81. };
  82. MessageHandler.prototype.handleMessage = function(message) {
  83. for (var i = 0, max = this.whiteHandlers.length; i < max; i++) {
  84. if (this.whiteHandlers[i](message) === true) {
  85. return true;
  86. }
  87. }
  88. for (i = 0, max = this.blackHandlers.length; i < max; i++) {
  89. if (this.blackHandlers[i](message) === false) {
  90. return false;
  91. }
  92. }
  93. return true;
  94. };
  95. MessageHandler.prototype.registerHandler = function(type, handler) {
  96. if (!this.hasOwnProperty(type)) {
  97. return false;
  98. }
  99. this[type].push(handler);
  100. return true;
  101. };
  102. function renderFilterBox(f,k) {
  103. return (f ? (''+f+' filtered <br>') : '')+
  104. (k ? ('<b>'+k+' kept</b>') : '');
  105. }
  106. MessageHandler.prototype.createFilterBox = function() {
  107. this.filteredBox = $("<p class='filterbox' title='spam count'></p>");
  108. if ($("body").append(this.filteredBox)) {
  109. this.filteredBox[0].innerHTML = renderFilterBox(this.filtered, this.kept);
  110. this.filterBoxActive = true;
  111. }
  112. };
  113. MessageHandler.prototype.incFilterCount = function() {
  114. if (!this.filterBoxActive)
  115. this.createFilterBox();
  116. this.filtered++;
  117. this.filteredBox[0].innerHTML = renderFilterBox(this.filtered, this.kept);
  118. };
  119. MessageHandler.prototype.incKeptCount = function() {
  120. if (!this.filterBoxActive)
  121. this.createFilterBox();
  122. this.kept++;
  123. this.filteredBox[0].innerHTML = renderFilterBox(this.filtered, this.kept);
  124. };
  125. /* whiteHandlers */
  126. var whitelist = [
  127. function() { return $(".js-username").text(); }
  128. ];
  129. var whitelistHandler = function(message) {
  130. for (var i = 0, max = whitelist.length; i < max; i++) {
  131. if (message.from == (typeof whitelist[i] === "function" ? whitelist[i]() : whitelist[i])) {
  132. return true;
  133. }
  134. }
  135. if (message.from === "") { // admin messages leave username blank
  136. return true;
  137. }
  138. return false;
  139. };
  140. /* blackHandlers */
  141. var isOfficialParagonBotHandler = function(message) {
  142. return (message.from == "OfficialParagonBot");
  143. };
  144. /* code */
  145. var hideMessages = function() {
  146. $("head").append("<style>.chat-line { display:none !important; }</style>");
  147. $("head").append("<style>.chat-line.filtered { display:list-item !important; }</style>");
  148. $("head").append("<style>.filterbox { position:absolute; top:0px; right:10px; line-height:1em; z-index:100; mix-blend-mode: difference; color: rgb(0,255,255); font-weight: lighter;} </style>");
  149. };
  150. var messageHandler = new MessageHandler();
  151. var whiteHandlers = [
  152. whitelistHandler
  153. ];
  154. var blackHandlers = [
  155. isOfficialParagonBotHandler
  156. ];
  157. messageHandler.init();
  158. for (var i = 0, max = whiteHandlers.length; i < max; i++) {
  159. messageHandler.registerHandler("whiteHandlers", whiteHandlers[i]);
  160. }
  161. for (i = 0, max = blackHandlers.length; i < max; i++) {
  162. messageHandler.registerHandler("blackHandlers", blackHandlers[i]);
  163. }
  164. hideMessages();
  165. setInterval(function() {
  166. var messages = $(".chat-line").not(".filtered");
  167. messages.each(function() {
  168. var message = new Message(this);
  169. if (messageHandler.handleMessage(message)) {
  170. message.show();
  171. messageHandler.incKeptCount();
  172. } else {
  173. message.remove();
  174. messageHandler.incFilterCount();
  175. }
  176. });
  177. }, 200);
  178. };

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址