Points Patch

Patches ws connection to hook set-word-points rpc

目前为 2023-05-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Points Patch
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Patches ws connection to hook set-word-points rpc
  6. // @author EnergoStalin
  7. // @match https://meme-police.ru/bg/alias
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=meme-police.ru
  9. // @license GPL3
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15. let change = 0;
  16. let enable = true;
  17. let enableOnEnd = true;
  18. let state = null;
  19. window.addEventListener('keydown', function(e) {
  20. if(e.keyCode === 37) { // ARROWLEFT
  21. change = 0;
  22. } else if(e.keyCode === 39) { // ARROWRIGHT
  23. change = 1;
  24. } else if(e.keyCode === 220) { // BACKSLASH
  25. enable = !enable;
  26. } else if(e.key === "|") {
  27. enableOnEnd = !enableOnEnd;
  28. }
  29. });
  30. const hookMethods = {
  31. 'set-word-points': function(data) {
  32. if(!enable) return data;
  33.  
  34. for(let i = 0; i < data.length; i++) {
  35. data[i].points = change;
  36. }
  37.  
  38. return data;
  39. }
  40. }
  41. const listeners = {
  42. 'timer-end': function(ws) {
  43. if(!enableOnEnd) return;
  44.  
  45. const words = JSON.parse(JSON.stringify(state.currentWords))
  46. for(let i = 0; i < words.length; i++) {
  47. words[i].points = 0;
  48. }
  49.  
  50. sendRpc(ws, 'set-word-points', words);
  51. },
  52. 'state': function(_, data) {
  53. state = data;
  54. }
  55. }
  56. WebSocket.prototype.send = new Proxy(WebSocket.prototype.send, {
  57. apply: function (target, scope, args) {
  58. if(!scope.hooked) { // Apply listeners
  59. scope.addEventListener('message', (msg) => {
  60. const data = JSON.parse(msg.data);
  61. listeners[data.a[0]]?.(scope, data.a[1]);
  62. })
  63. scope.hooked = true;
  64. }
  65.  
  66. if (typeof (args[0]) === 'string') {
  67. let json = JSON.parse(args[0]);
  68. json.a[1] = hookMethods[json.a[0]]?.(json.a[1]) ?? json.a[1];
  69.  
  70. return target.apply(scope, [JSON.stringify(json), ...args.splice(0, 1)])
  71. }
  72.  
  73. return target.apply(scope, args);
  74. }
  75. });
  76.  
  77. const sendRpc = function(ws, method, payload) {
  78. ws.send(JSON.stringify({ a: { 0: method, 1: payload }, c: '/bg/alias' }))
  79. }
  80. })();