Points Patch

Patches ws connection to hook set-word-points rpc

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Points Patch
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Patches ws connection to hook set-word-points rpc
// @author       EnergoStalin
// @match        https://meme-police.ru/bg/alias
// @icon         https://www.google.com/s2/favicons?sz=64&domain=meme-police.ru
// @license      GPL3
// @grant        none
// ==/UserScript==

(function () {
  'use strict';
  let change = 0;
  let enable = true;
  let enableOnEnd = true;
  let state = null;
  window.addEventListener('keydown', function(e) {
      if(e.keyCode === 37) { // ARROWLEFT
          change = 0;
      } else if(e.keyCode === 39) { // ARROWRIGHT
          change = 1;
      } else if(e.keyCode === 220) { // BACKSLASH
          enable = !enable;
      } else if(e.key === "|") {
          enableOnEnd = !enableOnEnd;
      }
  });
  const hookMethods = {
    'set-word-points': function(data) {
        if(!enable) return data;

        for(let i = 0; i < data.length; i++) {
            data[i].points = change;
        }

        return data;
    }
  }
  const listeners = {
    'timer-end': function(ws) {
        if(!enableOnEnd) return;

        const words = JSON.parse(JSON.stringify(state.currentWords))
        for(let i = 0; i < words.length; i++) {
            words[i].points = 0;
        }

        sendRpc(ws, 'set-word-points', words);
    },
    'state': function(_, data) {
        state = data;
    }
  }
  WebSocket.prototype.send = new Proxy(WebSocket.prototype.send, {
    apply: function (target, scope, args) {
      if(!scope.hooked) { // Apply listeners
          scope.addEventListener('message', (msg) => {
              const data = JSON.parse(msg.data);
              listeners[data.a[0]]?.(scope, data.a[1]);
          })
          scope.hooked = true;
      }

      if (typeof (args[0]) === 'string') {
        let json = JSON.parse(args[0]);
        json.a[1] = hookMethods[json.a[0]]?.(json.a[1]) ?? json.a[1];

        return target.apply(scope, [JSON.stringify(json), ...args.splice(0, 1)])
      }

      return target.apply(scope, args);
    }
  });

  const sendRpc = function(ws, method, payload) {
      ws.send(JSON.stringify({ a: { 0: method, 1: payload }, c: '/bg/alias' }))
  }
})();