Elethor WebSocket override for window hook

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

当前为 2021-01-08 提交的版本,查看 最新版本

  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 0.0.4
  6. // @author Anders Morgan Larsen (Xortrox)
  7. // @match https://elethor.com/
  8. // @match https://www.elethor.com/
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function(){
  13. const moduleName = 'Elethor WebSocket Hook';
  14. console.log(`[${moduleName}] Loaded.`);
  15.  
  16. Hook();
  17.  
  18. /**
  19. * Replaces the class reference of "WebSocket" with a function that binds any new instance to "window.socket"
  20. * This has no impact on the WebSocket class itself.
  21. */
  22. function Hook() {
  23. const OldSocket = WebSocket;
  24.  
  25. window.WebSocket = function () {
  26. console.log(`[${moduleName}] WebSocket hook successful.`);
  27. const socket = new OldSocket(...arguments);
  28. window.socket = socket;
  29.  
  30. return socket;
  31. }
  32. };
  33. })();