Elethor WebSocket override for window hook

Overrides the WebSocket class and hooks any new instance of a WebSocket to a window.socket reference

  1. // ==UserScript==
  2. // @name Elethor WebSocket override for window hook
  3. // @description Overrides the WebSocket class and hooks any new instance of a WebSocket to a window.socket reference
  4. // @namespace https://www.elethor.com/
  5. // @version 1.0.0
  6. // @author Xortrox
  7. // @match https://elethor.com/*
  8. // @match https://www.elethor.com/*
  9. // @run-at document-start
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function(){
  14. const moduleName = 'Elethor WebSocket Hook';
  15. console.log(`[${moduleName}] Loaded.`);
  16.  
  17. Hook();
  18.  
  19. /**
  20. * Replaces the class reference of "WebSocket" with a function that binds any new instance to "window.socket"
  21. * This has no impact on the WebSocket class itself.
  22. */
  23. function Hook() {
  24. const OldSocket = WebSocket;
  25.  
  26. window.WebSocket = function () {
  27. console.log(`[${moduleName}] WebSocket hook successful.`);
  28. const socket = new OldSocket(...arguments);
  29. window.socket = socket;
  30.  
  31. return socket;
  32. }
  33. };
  34. })();