Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. // ==UserScript==
  2. // @name amateri-filter
  3. // @namespace amateri.cz
  4. // @include https://www.amateri.com/chat-ws/?roomid=1306280
  5. // @include https://www.amateri.com/chat-ws/?roomid=1789388
  6. // @include https://www.amateri.com/chat-ws/?roomid=1306280
  7. // @include https://www.amateri.com/chat-ws/?roomid=2284882
  8. // @version 1
  9. // @grant none
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js
  11. // ==/UserScript==
  12.  
  13. this.$ = this.jQuery = jQuery.noConflict(true);
  14.  
  15. (function () {
  16. 'use strict';
  17.  
  18. var USERS_RESOURCE = '//server.u1ca.win/chat-filter/users';
  19.  
  20. var HashMap = function () {
  21. var map = {},
  22. hash = function (string) {
  23. return string.toLowerCase();
  24. };
  25.  
  26. this.put = function (key, value) {
  27. map[hash(key)] = value;
  28. };
  29.  
  30. this.get = function (key) {
  31. return map[hash(key)];
  32. };
  33. };
  34.  
  35. var blacklistProvider = function () {
  36. var deferred = $.Deferred(),
  37. blacklist = new HashMap();
  38.  
  39. $.ajax(USERS_RESOURCE).then(function (users) {
  40. users.forEach(function (user) {
  41. blacklist.put(user.name, user);
  42. });
  43. console.info('Blacklist retrieved');
  44. deferred.resolve(blacklist);
  45. });
  46. return deferred;
  47. };
  48.  
  49. var MessageUser = function (user) {
  50.  
  51. this.myself = function () {
  52. return user.n === window.user;
  53. };
  54.  
  55. this.male = function () {
  56. return user.p === 1;
  57. };
  58.  
  59. this.name = function () {
  60. return user.n;
  61. };
  62. };
  63.  
  64. var FilterMenu = function (callback) {
  65. var button = $('<a>'),
  66. menu,
  67. createMenu = function () {
  68. menu = $('<div>');
  69. Object.keys(chatFilter.Reason).forEach(function (reason, index) {
  70. var item = $('<a>')
  71. .attr('href', '#')
  72. .html(reason.toLowerCase())
  73. .click(callback.bind(this, reason));
  74. index > 0 && menu.append(' ');
  75. menu.append(item);
  76. });
  77. menu.css('position', 'absolute');
  78. menu.css('background-color', 'rgba(255, 255, 255, 0.9)');
  79. menu.css('border', '1px solid white');
  80. button.after(menu);
  81. },
  82. destroyMenu = function () {
  83. menu.remove();
  84. menu = null;
  85. };
  86.  
  87. button.attr('href', '#');
  88. button.html('&#9776;');
  89. button.click(function () {
  90. if (menu) {
  91. destroyMenu();
  92. } else {
  93. createMenu();
  94. }
  95. });
  96. button.css('text-decoration', 'none');
  97.  
  98. this.button = function () {
  99. return button;
  100. };
  101. };
  102.  
  103. var ChatFilter = function () {
  104. var blacklistPromise = blacklistProvider(),
  105. self = this;
  106.  
  107. this.insertBefore = function (newElement, referenceElement) {
  108. var callback = Node.prototype.insertBefore.bind(this, newElement, referenceElement),
  109. groups = /parent\.showInfo\((\d+),0\)/.exec(newElement.innerHTML),
  110. user = window.users[groups[1]],
  111. messageUser = user && new MessageUser(user);
  112.  
  113. if (!messageUser) {
  114. return;
  115. }
  116. if (messageUser.male() && !messageUser.myself()) {
  117. return;
  118. }
  119. blacklistPromise.then(function (blacklist) {
  120. if (!blacklist.get(messageUser.name())) {
  121. var nameElement = $(newElement).find('b[onclick]'),
  122. filterMenu = new FilterMenu(function (reason) {
  123. self.add(messageUser.name(), reason);
  124. $(newElement).remove();
  125. });
  126. nameElement.before(filterMenu.button(), ' ');
  127. callback();
  128. }
  129. });
  130. };
  131.  
  132. this.add = function (name, reason) {
  133. var user = {
  134. name: name,
  135. reason: reason
  136. };
  137.  
  138. blacklistPromise.then(function (blacklist) {
  139. blacklist.put(user.name, user);
  140. });
  141.  
  142. $.ajax(USERS_RESOURCE, {
  143. method: 'PUT',
  144. data: JSON.stringify(user),
  145. contentType: 'application/json',
  146. success: function () {
  147. console.info('User %s added', user.name);
  148. }
  149. });
  150. };
  151.  
  152. this.remove = function (name) {
  153. $.ajax(USERS_RESOURCE + '/' + name, {
  154. method: 'DELETE',
  155. success: function (data) {
  156. console.info('Removed %d users having name %s', data, name);
  157. }
  158. });
  159. };
  160. };
  161.  
  162. ChatFilter.prototype.Reason = {
  163. DISLIKE: 'dislike',
  164. OFFTOPIC: 'offtopic',
  165. WEBCAM: 'webcam',
  166. SPAM: 'spam',
  167. PRO: 'pro',
  168. COUPLE: 'couple',
  169. MALE: 'male',
  170. FAKE: 'fake'
  171. };
  172.  
  173. $(window.chat).load(function () {
  174. window.chatFilter = new ChatFilter();
  175. $('#textPlace', window.chat.document).get(0).insertBefore = window.chatFilter.insertBefore;
  176.  
  177. window.nKick = function () {
  178. var randomSmiley = function () {
  179. return '*' + Math.ceil(Math.random() * 478) + '*';
  180. };
  181. return function (min) {
  182. var bottom = window.bottom.document;
  183. $('#zprava', bottom).get(0).value = randomSmiley();
  184. var messageButton = bottom.evaluate('//form[@id="formular"]/input[@type="submit"]', bottom, null,
  185. XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  186. messageButton.click();
  187. };
  188. }();
  189.  
  190. window.chat.document.body.onresize = function () {};
  191. });
  192. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement