NoPageRefreshWhenHosped

Prevents page refresh when hospitalized on Torn.

  1. // ==UserScript==
  2. // @name NoPageRefreshWhenHosped
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Prevents page refresh when hospitalized on Torn.
  6. // @author amelia
  7. // @match https://www.torn.com/*
  8. // @run-at document-start
  9. // @grant GM_addStyle
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. "use strict";
  14.  
  15. const dataProperty = Object.getOwnPropertyDescriptor(MessageEvent.prototype, "data");
  16. const originalGetter = dataProperty.get;
  17.  
  18. dataProperty.get = hookedGetter;
  19. Object.defineProperty(MessageEvent.prototype, "data", dataProperty);
  20.  
  21. function hookedGetter() {
  22. const socket = this.currentTarget;
  23.  
  24. if (!(socket instanceof WebSocket) || socket.url.indexOf("ws-centrifugo.torn.com") === -1) {
  25. return originalGetter.call(this); // Invalid or wrong WebSocket
  26. }
  27.  
  28. const message = originalGetter.call(this);
  29. Object.defineProperty(this, "data", { value: message }); // Anti-loop
  30.  
  31. return handleMessage(message);
  32. }
  33.  
  34. function handleMessage(message) {
  35. console.log("NoPageRefreshWhenHosped: " + message);
  36. let resultMessage = message;
  37.  
  38. if (resultMessage.includes(`"onHospital":[],`)) {
  39. resultMessage = resultMessage.replace(`"onHospital":[],`, "");
  40. console.log("NoPageRefreshWhenHosped Modified: " + resultMessage);
  41. }
  42.  
  43. return resultMessage;
  44. }
  45. })();