Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. (function debugify_content_script(){
  2. console.log('debugify content script running');
  3. var nativeWebSocket = window.WebSocket;
  4. var requests = window.requestLog = {};
  5. var WebSocket = window.WebSocket = function(uri) {
  6. console.log('new WebSocket created', uri);
  7. this.websocket = new nativeWebSocket(uri);
  8. this.websocket.onopen = this.onOpen.bind(this);
  9. this.websocket.onmessage = this.onMessage.bind(this);
  10. this.listeners = {onmessage: null, onopen: null};
  11.  
  12. if (!window._openWebSockets) window._openWebSockets = [];
  13. window._openWebSockets.push(this);
  14. };
  15. WebSocket.prototype.send = function(msg) {
  16. console.log('>>', msg);
  17. this.websocket.send.apply(this.websocket, arguments);
  18. }
  19. WebSocket.prototype.onOpen = function(e){
  20. console.log('OPEN', arguments);
  21. this.listeners.onopen(e);
  22. }
  23. WebSocket.prototype.onMessage = function(e){
  24. console.log('<<', e.data);
  25. this.listeners.onmessage(e);
  26. }
  27. Object.defineProperty(WebSocket.prototype, 'readyState', {
  28. get: function() {
  29. return this.websocket.readyState;
  30. }
  31. });
  32. Object.defineProperty(WebSocket.prototype, 'onopen', {
  33. get: function() {
  34. return this.listeners.onopen;
  35. },
  36. set: function(fn) {
  37. this.listeners.onopen = fn;
  38. }
  39. });
  40. Object.defineProperty(WebSocket.prototype, 'onclose', {
  41. get: function() {
  42. return this.websocket.onclose;
  43. },
  44. set: function(fn) {
  45. this.websocket.onclose = fn;
  46. }
  47. });
  48. Object.defineProperty(WebSocket.prototype, 'onmessage', {
  49. get: function() {
  50. return this.listeners.onmessage;
  51. },
  52. set: function(fn) {
  53. this.listeners.onmessage = fn;
  54. }
  55. });
  56. Object.defineProperty(WebSocket.prototype, 'onerror', {
  57. get: function() {
  58. return this.websocket.onerror;
  59. },
  60. set: function(fn) {
  61. this.websocket.onerror = fn;
  62. }
  63. });
  64. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement