Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name amateri-filter
- // @namespace amateri.cz
- // @include https://www.amateri.com/chat-ws/?roomid=1306280
- // @include https://www.amateri.com/chat-ws/?roomid=1789388
- // @include https://www.amateri.com/chat-ws/?roomid=1306280
- // @include https://www.amateri.com/chat-ws/?roomid=2284882
- // @version 1
- // @grant none
- // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js
- // ==/UserScript==
- this.$ = this.jQuery = jQuery.noConflict(true);
- (function () {
- 'use strict';
- var USERS_RESOURCE = '//server.u1ca.win/chat-filter/users';
- var HashMap = function () {
- var map = {},
- hash = function (string) {
- return string.toLowerCase();
- };
- this.put = function (key, value) {
- map[hash(key)] = value;
- };
- this.get = function (key) {
- return map[hash(key)];
- };
- };
- var blacklistProvider = function () {
- var deferred = $.Deferred(),
- blacklist = new HashMap();
- $.ajax(USERS_RESOURCE).then(function (users) {
- users.forEach(function (user) {
- blacklist.put(user.name, user);
- });
- console.info('Blacklist retrieved');
- deferred.resolve(blacklist);
- });
- return deferred;
- };
- var MessageUser = function (user) {
- this.myself = function () {
- return user.n === window.user;
- };
- this.male = function () {
- return user.p === 1;
- };
- this.name = function () {
- return user.n;
- };
- };
- var FilterMenu = function (callback) {
- var button = $('<a>'),
- menu,
- createMenu = function () {
- menu = $('<div>');
- Object.keys(chatFilter.Reason).forEach(function (reason, index) {
- var item = $('<a>')
- .attr('href', '#')
- .html(reason.toLowerCase())
- .click(callback.bind(this, reason));
- index > 0 && menu.append(' ');
- menu.append(item);
- });
- menu.css('position', 'absolute');
- menu.css('background-color', 'rgba(255, 255, 255, 0.9)');
- menu.css('border', '1px solid white');
- button.after(menu);
- },
- destroyMenu = function () {
- menu.remove();
- menu = null;
- };
- button.attr('href', '#');
- button.html('☰');
- button.click(function () {
- if (menu) {
- destroyMenu();
- } else {
- createMenu();
- }
- });
- button.css('text-decoration', 'none');
- this.button = function () {
- return button;
- };
- };
- var ChatFilter = function () {
- var blacklistPromise = blacklistProvider(),
- self = this;
- this.insertBefore = function (newElement, referenceElement) {
- var callback = Node.prototype.insertBefore.bind(this, newElement, referenceElement),
- groups = /parent\.showInfo\((\d+),0\)/.exec(newElement.innerHTML),
- user = window.users[groups[1]],
- messageUser = user && new MessageUser(user);
- if (!messageUser) {
- return;
- }
- if (messageUser.male() && !messageUser.myself()) {
- return;
- }
- blacklistPromise.then(function (blacklist) {
- if (!blacklist.get(messageUser.name())) {
- var nameElement = $(newElement).find('b[onclick]'),
- filterMenu = new FilterMenu(function (reason) {
- self.add(messageUser.name(), reason);
- $(newElement).remove();
- });
- nameElement.before(filterMenu.button(), ' ');
- callback();
- }
- });
- };
- this.add = function (name, reason) {
- var user = {
- name: name,
- reason: reason
- };
- blacklistPromise.then(function (blacklist) {
- blacklist.put(user.name, user);
- });
- $.ajax(USERS_RESOURCE, {
- method: 'PUT',
- data: JSON.stringify(user),
- contentType: 'application/json',
- success: function () {
- console.info('User %s added', user.name);
- }
- });
- };
- this.remove = function (name) {
- $.ajax(USERS_RESOURCE + '/' + name, {
- method: 'DELETE',
- success: function (data) {
- console.info('Removed %d users having name %s', data, name);
- }
- });
- };
- };
- ChatFilter.prototype.Reason = {
- DISLIKE: 'dislike',
- OFFTOPIC: 'offtopic',
- WEBCAM: 'webcam',
- SPAM: 'spam',
- PRO: 'pro',
- COUPLE: 'couple',
- MALE: 'male',
- FAKE: 'fake'
- };
- $(window.chat).load(function () {
- window.chatFilter = new ChatFilter();
- $('#textPlace', window.chat.document).get(0).insertBefore = window.chatFilter.insertBefore;
- window.nKick = function () {
- var randomSmiley = function () {
- return '*' + Math.ceil(Math.random() * 478) + '*';
- };
- return function (min) {
- var bottom = window.bottom.document;
- $('#zprava', bottom).get(0).value = randomSmiley();
- var messageButton = bottom.evaluate('//form[@id="formular"]/input[@type="submit"]', bottom, null,
- XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
- messageButton.click();
- };
- }();
- window.chat.document.body.onresize = function () {};
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement